Skip to main content
5 events
when toggle format what by license comment
Apr 27, 2018 at 16:37 comment added Alan @LBogaardt Can you explain your dissatisfaction? If it is the reference to a global, you could close over that: With[{as = Association["linear" -> x, "square" -> x^2]}, f[xx_] := (as /. {x -> xx}) ]. However, I would probably g[xx_] := Map[With[{x = xx}, #] &, Association["linear" -> x, "square" -> x^2]]
Apr 27, 2018 at 15:27 comment added LBogaardt I think the effect is the same though. It would re-evaluate the Association every time. In my case, that's the computational bottleneck which I am trying to avoid. The answer by MarcoB solves this, though I am not 100% satisfied with the method.
Apr 26, 2018 at 20:16 comment added Alan @LBogaardt Yes, atomicity is important. So is the order of evaluation. See the code in my answer: the two down values are identical except for the Evaluate. I made no use of SetDelayed.
Apr 26, 2018 at 8:08 comment added LBogaardt True, but this explains the difference between Set and SetDelayed. It does not address why Associations do not allow for a pattern variable (the Atomic reason by Carl Woll). In fact, change Association to List in your example, and the problem vanishes.
Apr 24, 2018 at 15:38 history answered Alan CC BY-SA 3.0