What's wrong with (foo :able) => "Adelicious!" and (:able foo) => 
"Adelicious!"?

On Thursday, May 11, 2017 at 9:20:19 AM UTC+2, Alan Thompson wrote:
>
> A recent question on StackOverflow raised the question of the best way to 
> automatically generate functions. Suppose you want to automate the creation 
> of code like this: 
>
>
>
> (def foo
>   {:able    "Adelicious!"
>    :baker   "Barbrallicious!"
>    :charlie "Charlizable"})
> (def bar
>   {:able    "Apple"
>    :baker   "Berry"
>    :charlie "Kumquat"})
>
> (defn manual-my-foo [item] (get foo item))
> (defn manual-my-bar [item] (get bar item))
>
> (manual-my-foo :able) => "Adelicious!"
> (manual-my-bar :charlie) => "Kumquat"
>
>
> You could write a macro to generate one of these at a time, but you can't 
> pass a macro to a higher-order function like `map`, so while this would 
> work:
>
>
> (generate-fn :foo)  ;=> creates `my-foo` w/o hand-writing it
>
>
> this wouldn't work:
>
>
> (map generate-fn [:foo :bar :baz])  
>
> While one could write a 2nd macro to replace `map`, this is a symptom of 
> the "Turtles All the Way Down" problem. One workaround is to avoid macros 
> altogether and use only functions to generate the required `my-foo` and 
> `my-bar` functions.  The trick is to make use of the built-in Clojure 
> function `intern`  both to save the newly generated functions into the 
> global environment and to retrieve the pre-existing maps `foo` and `bar`.  
> Full details are available Q&A-style at the StackOverflow post 
> <http://stackoverflow.com/questions/43904628/how-to-create-clojure-defn-functions-automatically/43904717#43904717>
> .
>
> Enjoy,
> Alan
>
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to