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
-~----------~----~----~----~------~----~------~--~---

Reply via email to