The problem is that even though "some" and "filter" are lazy, "alt" is still not, so calling "(alt (sub-function1 c) ...)" in the meta-meta- function still evaluates (sub-function1 c), etc. It could be shown in the REPL:
Clojure user=> (defn alt [& functions] (fn [tokens] (some #(% tokens) functions))) #'user/alt user=> (defn sub-function1 [c] (println "1:" c) (fn [c] false)) #'user/sub-function1 user=> (defn sub-function2 [c] (println "2:" c) (fn [c] true)) #'user/sub-function2 user=> (defn sub-function3 [c] (println "3:" c) (fn [c] false)) #'user/sub-function3 user=> (defn a-meta-meta-function [c] (alt (sub-function1 c) (sub-function2 c) (sub-function3 c))) #'user/a-meta-meta-function user=> ((a-meta-meta-function "CONTEXT") [:a :b :c]) 1: CONTEXT 2: CONTEXT 3: CONTEXT true What I wish is for calling '((a-meta-meta-function "CONTEXT") [:a :b :c])' to output: 1: CONTEXT 2: CONTEXT ...only, which means that the sub-functions are called only until a sub-function's created function returns a true value. The root of the problem is that in Clojure's rules (I think), the arguments of a regular function are all evaluated before being plugged into the function. That's why I'm wondering if a macro could solve my problem. Thanks for your answers! On Jan 11, 3:02 pm, Stuart Sierra <the.stuart.sie...@gmail.com> wrote: > "some" is already lazy, so you may not need to change anything at > all. You might also be able to use "filter", which will not do > anything until you consume the output sequence. > -Stuart Sierra > > On Jan 11, 4:44 pm, samppi <rbysam...@gmail.com> wrote: > > > Let's say I have a function, alt: > > > (defn alt [& functions] > > (fn [tokens] > > (some #(% tokens) functions))) > > > It creates a function from a bunch of sub-functions that accepts one > > collection of tokens and figures out which sub-function returns a true > > value when the tokens are plugged into it. > > > Is there a way to change alt—maybe to a macro—that lazily evaluates > > the functions? This is so that with: > > > (defn a-meta-meta-function [c] > > (alt (sub-function1 c) (sub-function2 c) (sub-function3 c))) > > > ...calling ((a-meta-meta-function foo) some-tokens) doesn't evaluate > > (sub-function 2 c) and (sub-function3 c) until needed to receive some- > > tokens. --~--~---------~--~----~------------~-------~--~----~ 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 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 -~----------~----~----~----~------~----~------~--~---