On Sun, Apr 14, 2013 at 1:03 PM, Marko Topolnik <marko.topol...@gmail.com>wrote:
> On Sunday, April 14, 2013 7:51:10 PM UTC+2, Matthew Hill wrote: > >> Function composition is done via comp. Using -> and ->> is like function >> composition in reverse order (though there's a difference between how the >> two thread return values), and often it reads more naturally. > > > -> applies the functions immediately whereas comp returns a new function > that is the composition of its arguments. > > -> works with functions of any arity; comp only with unary functions. > > As pointed out above, -> merely combines the unevaluated forms it is > given, and only if they happen to be function application forms will the > result be similar to function composition. > Even that's somewhat misleading. user=> (defn make-adder [n] (fn [x] (+ n x))) #'user/make-adder user=> ((comp println (make-adder 4)) 3) 7 nil user=> (-> 3 (make-adder 4) println) ArityException Wrong number of args (2) passed to: user$make-adder clojure.lang.AFn.throwArity (AFn.java:437) user=> (-> 3 ((make-adder 4)) println) 7 nil (make-adder 4) is a "function application form" but the result isn't similar to function composition because the threading operators rewrite their arguments, so we end up with (make-adder 3 4). If it were expected that all arguments but the first would be functions, rather than lists corresponding to function-invocations-with-one-argument-deleted, then -> *could* be equivalent to function composition (and could be written as a regular function): user=> (defn ->* [a & cs] ((apply comp (reverse cs)) a)) #'user/->* user=> (->* 3 (make-adder 4) println) 7 nil user=> (->* [1 2 3] (partial map inc) set #(contains? % 4)) true -- Ben Wolfson "Human kind has used its intelligence to vary the flavour of drinks, which may be sweet, aromatic, fermented or spirit-based. ... Family and social life also offer numerous other occasions to consume drinks for pleasure." [Larousse, "Drink" entry] -- -- 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.