I have an association that has parameters for a function. These parameters represent two cases that need to be applied to the function. Some parameter values are common to both cases but some are unique to each case.

I need to execute the function for both cases of the parameters. I get the parameters for both cases in a single list per event. I've represented this below for a function f[a,b,c] where parameters a and b are common to both cases but for parameter c case 1 uses c1 and case 2 uses c2.

assoc = AssociationThread[{"a", "b", "c1", "c2"}, #] & /@ RandomInteger[20, {3, 4}]; 

So far I've managed to get the two list of the function calls but the parameter c in the function call is not evaluating to the value of the association; see below. I've not been able to figure out how to get the Key function to return the value of the key when it is mapped.

Transpose[(Function[{k}, f[#a, #b, Key[k]]] /@ {"c1", "c2"}) & /@ assoc]
 {{f[13, 7, Key["c1"]], f[5, 11, Key["c1"]], f[6, 16, Key["c1"]]}, 
  {f[13, 7, Key["c2"]], f[5, 11, Key["c2"]], f[6, 16, Key["c2"]]}}

How do I proceed? I've thought of using MapIndexed but it seems like a lot of mapping and remapping to get the result. I think there must be a cleaner way to get this. Yes?


closed as off-topic by Mr.Wizard Apr 22 '15 at 16:39

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Mr.Wizard
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $\begingroup$ Use #[[Key[k]]]]. Btw it's hard to understand what you are trying to do. $\endgroup$ – C. E. Apr 22 '15 at 15:03
  • $\begingroup$ What is your desired output? Your description is very hard to follow... $\endgroup$ – Stefan R Apr 22 '15 at 15:34
  • $\begingroup$ @StefanR I've updated the question to be more clear. $\endgroup$ – Edmund Apr 22 '15 at 15:53

You can replace Key[k] with #[[Key[k]]].

  • 2
    $\begingroup$ With this I was able to realize that I can replace Key[k] with just #[k] since it is the association that is being mapped in. For some reason I was thinking that each value was being mapped in. I think I've been looking at the screen for too long. :) $\endgroup$ – Edmund Apr 22 '15 at 16:25
  • $\begingroup$ @Edmund Shall I close this as "simple mistake" in that case? $\endgroup$ – Mr.Wizard Apr 22 '15 at 16:34
  • $\begingroup$ @Mr.Wizard Sure. $\endgroup$ – Edmund Apr 22 '15 at 16:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.