Your maybe-> does almost the same thing as Clojure 1.5's some-> but without support for naked fns (like `(some-> 1 inc)`). It also evaluates each "step" but the last twice (once for the `if`, once when inserted after `op`).
If you don't want to switch to some->, I'd recommend you use when-let to avoid the double evaluation. On Mar 23, 2013 4:12 AM, "Gary Verhaegen" <gary.verhae...@gmail.com> wrote: > I've recently had a need for something like that in my own code. The > "real" solution to that problem in the functional programming world is > known as the maybe monad. Since I just needed a quick and dirty > solution and I have not wrapped my head around monads yet, here's what > I did : > > (defmacro maybe-> > "Sort of like a maybe monad, but without a monad. Takes an initial value > and > a list of functions with their arguments, and returns the result of > applying > each function to the result of the preceding one as long as there is no > nil > value. Returns nil if there ever is a nil in the chain of values." > [init & fns] > (reduce (fn [acc [op & args]] > `(if-not (nil? ~acc) > (~op ~acc ~@args))) > init fns)) > > I did not have a need for "as->"-like functionality. > -- -- 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/groups/opt_out.