0

Suppose you have a peculiar map where each key might represent a Clojure core function:

{:inc 1 :dec 2 :identity "three"}

What would you write to apply each key (as a function) to its own value?

Output should yield something like:

(2 1 "three")

This code fails to produce what I expect:

user=> (def mm {:inc 1 :dec 2 :identity "three"})
user=> (map #((symbol (first %)) (get % 1)) mm)
(nil nil nil)
silian-rail
  • 135
  • 10
  • 1
    Or this: [What happens when I pass arguments to a Clojure symbol?](https://stackoverflow.com/questions/8219305/what-happens-when-i-pass-arguments-to-a-clojure-symbol) – Martin Půda Feb 19 '23 at 05:17
  • @MartinPůda This gives somewhat clearer explanation: https://stackoverflow.com/a/63208807/4903731 – silian-rail Feb 19 '23 at 05:33

1 Answers1

1

Wrapping the symbol with resolve solves this problem:

(resolve (symbol (...)))

More detailed explanation here: https://stackoverflow.com/a/63208807/4903731

Any of the following produce the desired output:

(def mm {:inc 1 :dec 2 :identity "three"})

(map (fn [%] ((resolve (symbol (key %))) (val %))) mm)
(map #((resolve (symbol (key %))) (val %)) mm)
(map #((resolve (symbol (first %))) (get % 1)) mm)
silian-rail
  • 135
  • 10