Thank you for the explanation; I understand it a lot better now. The reason that I decided to use Delays was that I thought I would need to change less. Now that I've actually changed everything to Delays, it seems that they take much more time (the opposite of what I was trying to do :(. But when I checked using your and Konrad's explanation, the time was shorter than both of them.
--- 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=> (time ((a-meta-meta-function "CONTEXT") [:a :b :c])) 1: CONTEXT 2: CONTEXT 3: CONTEXT "Elapsed time: 1.062 msecs" true user=> (time ((a-meta-meta-function "CONTEXT") [:a :b :c]) ) 1: CONTEXT 2: CONTEXT 3: CONTEXT "Elapsed time: 0.444 msecs" true --- Clojure user=> (defn alt [& funcs] (fn [tokens] (some #((%) tokens) funcs))) #'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 mmf [c] (alt #(sub-one c) #(sub-two c) #(sub-three c))) java.lang.Exception: Unable to resolve symbol: sub-one in this context (NO_SOURCE_FILE:5) user=> (time (defn mmf [c] (alt #(sub-function1 c) #(sub-function2 c) # (sub-function3 c))) ) "Elapsed time: 0.167 msecs" #'user/mmf user=> (time ((mmf "context") [:a :b :c])) 1: context 2: context "Elapsed time: 1.011 msecs" true user=> (time ((mmf "context") [:a :b :c])) 1: context 2: context "Elapsed time: 0.295 msecs" true user=> (time ((mmf "context") [:a :b :c])) 1: context 2: context "Elapsed time: 0.208 msecs" true --- I didn't consider that suggestion enough, and now I'm going to pay for it by refactoring everything again, this time to using meta-functions. Oh, well—thanks to everyone for their help. :) On Jan 13, 12:51 pm, Perry Trolard <trol...@gmail.com> wrote: > Hi sampii, > > The problem, as I see it (& as Konrad suggested above), is that you're > not passing *functions* to (alt); you're passing values returned from > function calls, even though in the case of the sub-functions you > define those returned values are functions. Functions evaluate to > themselves, so if you can write your code to pass functions to (alt), > e.g. > > (alt #(sub-function1 c) #(sub-function2 c) #(sub-fuction3 c)) > > you should be golden. > > Just to show it can be done, if you make the args to alt be > > (defn mmf [c] (alt #(sub-one c) #(sub-two c) #(sub-three c))) > > (note the #() reader macro)), & if you modify the definition of alt to > be > > (defn alt [& funcs] (fn [tokens] (some #((%) tokens) funcs))) > > (note the extra set of parens around % -- so that the #(sub-one c) > funcs get called, then their return values likewise get called), you > get > > user=> ((mmf "context") [:a :b :c]) > 1 context > 2 context > true > > Of course it would be more straightforward to drop the intermediate > step of the anonymous functions -- #(sub-one c) -- which return yet > another function -- (fn [c] true) -- say, by passing the equivalent of > (fn [c] true) directly to alt, but I don't know your actual use case. > > My recommendation -- to passing function -- is simple enough that I > feel like I might be missing something important from your question, > but in case I'm not I didn't want you to think your only recourse was > the full machinery of delay/force. Transcript follows. > > Best, > Perry > > user=> (defn alt [& funcs] (fn [tokens] (some #((%) tokens) funcs))) > #'user/alt > user=> (defn sub-one [c] (println 1 c) (fn [x] false)) > #'user/sub-one > user=> (defn sub-two [c] (println 2 c) (fn [x] true)) > #'user/sub-two > user=> (defn sub-three [c] (println 3 c) (fn [x] false)) > #'user/sub-three > user=> (defn mmf [c] (alt #(sub-one c) #(sub-two c) #(sub-three c))) > #'user/mmf > user=> ((mmf "context") [:a :b :c]) > 1 context > 2 context > true --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---