Say I have an association:
a = <|"a" -> "albatross", "b" -> "barbecue", "c" -> "champagne"|>
And say I want a function with named slots to transform this association to a list:
In: a // {#a, #b, #a, #c, #a <> "-" <> #c} &
Out: {"albatross", "barbecue", "albatross", "champagne", "albatross-champagne"}
I want to programmatically create a function like this, rather than write it out explicitly in code. One hurdle I've come across is working with the Slot function. The following inputs all give the same, desired output:
In: a // {#a, #b} &
In: a // {#["a"], #["b"]} &
In: a // {Slot["a"], Slot["b"]} &
In: a // ToExpression["{#a, #b}&"]
In: a // Function[assoc, assoc[[#]] & /@ {"a", "b"}]
Out: {"albatross", "barbecue"}
I could roll with the fifth option. But I'm surprised the following code doesn't return what I expect:
In: a // (Slot /@ {"a", "b"}) &
Out: {#a, #b}
I am surprised this one returns neither what I expect, nor agrees with the above code:
In: a // Function[assoc, assoc[Slot /@ {"a", "b"}]]
Out: Missing["KeyAbsent", {#a, #b}]
Reading this led me to try the following option— no luck:
In: a // With[{v1 = "a", v2 = "b"}, Slot /@ {v1, v2} &]
Out: {#a, #b}
I want to make this code work, or at least understand why the above code doesn't work. What am I missing in my understanding of the Slot function?


a // Evaluate[Slot /@ {"a", "b"}] &. You can useTraceto get what's wrong. Moreover your input seems to be an association and key names, why not usingLookup/Queryetc? – Kuba Oct 20 '15 at 14:25a // (Slot /@ {"a", "b"}) &, Apply evaluates before Slot. But ina // Evaluate[Slot /@ {"a", "b"}] &, using Evaluate forces Slot to be evaluated before Apply. Correct? – akrodha Oct 20 '15 at 16:14Slot["a"]is precisely the same thing as#a. There is absolutely no difference. Once Mathematica reads either, it uses the very same internal representation. – Szabolcs Oct 20 '15 at 22:10