clojure.core/comp has not been changed at all. It's just the nature of how
transducers work. Here's a another example where function composition seems
to compose left-to-right (the second example, `comp2`):
(defn add-3 [x] (+ 3 x))
(defn mul-2 [y] (* 2 y))
(defn sub-1 [z] (- 1 z))
(def comp1 (comp add-3 mul-2 sub-1))
;; (comp1 2)
;; (add-3 (mul-2 (sub-1 2)))
;; ((fn [x] (+ x 3)) ((fn [y] (* y 2)) (fn [z] (- 1 z)) 2))
;; ((fn [x] (+ x 3)) ((fn [y] (* y 2)) (- 1 2)))
;; ((fn [x] (+ x 3)) (* (- 1 2) 2))
;; (+ (* (- 1 2) 2) 3)
;; 1
(defn add-3 [f]
(fn [x]
(f (+ 3 x))))
(defn mul-2 [f]
(fn [x]
(f (* 2 x))))
(defn sub-1 [f]
(fn [x]
(f (- 1 x))))
(def comp2 ((comp add-3 mul-2 sub-1) identity))
;; (comp2 2)
;; ((add-3 (mul-2 (sub-1 identity))) 2)
;; ((add-3 (mul-2 (fn [x] (identity (- 1 x))))) 2)
;; ((add-3 (fn [x] ((fn [x] (identity (- 1 x))) (* 2 x)))) 2)
;; ((fn [x] ((fn [x] ((fn [x] (identity (- 1 x))) (* 2 x))) (+ 3 x))) 2)
;; ((fn [x] ((fn [x] (identity (- 1 x))) (* 2 x))) (+ 3 2))
;; ((fn [x] (identity (- 1 x))) (* 2 (+ 3 2)))
;; (identity (- 1 (* 2 (+ 3 2))))
;; (- 1 (* 2 (+ 3 2)))
;; -9
which is the same as with ->>
(->> 2
(+ 3)
(* 2)
(- 1))
;; -9
On Thursday, October 30, 2014 5:44:48 PM UTC+2, Mars0i wrote:
>
> Caveat: I am still feeling around in the dark in my understanding of
> transducers. What I write below may just convey how clueless I am.
>
> (Meta-caveat: I'm probably spitting into the wind. I should no doubt use
> my time more wisely.)
>
>
> Normal function composition is done starting from the right. This is
> familiar from mathematics, other Lisps, most languages, and it's how
> Clojure's function application and 'comp' work.
>
> Sometimes it's easier to understand composition going from left to right,
> as in many natural languages and as in unix pipes, and Clojure provides
> '->' and '->>' to do that. That's good. Best of both worlds. One thing I
> like about these operators is that their name clearly indicates the
> direction of function application.
>
> Transducers allow function composition with potential efficiency gains,
> but apply functions starting from left to right. But *it does this using
> the name 'comp'*, which otherwise applies functions from right to left.
> What?? Doesn't that seem like a Bad Thing? Why not use a different name?
> (It's like overloading the minus sign so that in some contexts, it
> subtracts the first argument from the second.)
>
> (Is Clojure is getting too popular? Its essential features--prefix
> notation, parentheses, purely functional operations, and laziness--aren't
> doing enough to scare away Java programmers? :-)
>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/d/optout.