What does -> do in clojure?

前端 未结 6 1284
既然无缘
既然无缘 2021-02-01 04:02

I have seen the clojure symbol -> used in many places, but I am unsure as to what this symbol is called and does, or even whether it is part of standard clojure. Could someone e

6条回答
  •  日久生厌
    2021-02-01 04:47

    I did not fully get what -> (thrush or thread) did until I visualized it like this:

    (-> expr f1 f2 f3)  ;same as (f3 (f2 (f1 expr)))
    
    (-> expr            ;same as form above
        f1              ;just a different visual layout
        f2
        f3)
    
    ;this form is equivalant and shows the lists for f1, f2, f3.
    (->         expr     ; expr treaded into first form
            (f1     )    ;  |   result threaded into next form
        (f2          )   ;  |   and so on...
    (f3               )) ;  V   the lists (f1
    
    (f3 (f2 (f1 expr)))  ;the result is the same as this  
    

    Here are some examples:

    (-> 41 inc dec inc)   ;same as (inc (dec (inc 41)))
    42
    
    (->            41     ;same as above but more readable
              (inc   )
         (dec         )
    (inc               ))
    42
    
    (inc (dec (inc 41)))  ;easier to see equivalence with above form.
    42
    
    (-> 4 (* 4 3) (- 6))  ;same as (- (* 4 3 4) 6)
    42 
    
    (->   4               ;      4
       (*   3 4)          ;   (* 4 3 4)
    (-           6))      ;(- (* 4 3 4) 6)
    42
    
    (- (* 4 3 4) 6)       ;easier to see equivalence with above form.
    42
    

提交回复
热议问题