Why is clojure thread last (- >>) a macro?

The only advantage that I see is that it avoids calls to partial .

 (defn foldl [f acc xs] (loop [acc acc xs xs] (if (empty? xs) acc (recur (f (first xs) acc) (rest xs))))) (defn $ [fx] (fx)) (defn thread-last [x & rest] (foldl $ x rest)) 

which gives:

 (thread-last (range 10) (partial map inc) (partial filter odd?) (partial reduce +)) => 25 (->> (range 10) (map inc) (filter odd?) (reduce +)) => 25 

Are there any cases where the functional / explicit version fails?

+5
source share
1 answer

First, note that your foldl is just reduce . Our language is not much worse than Haskell!

Secondly, not all forms are function calls, but ->> can rewrite all forms. For example, you can use ->> to implement something like Haskell where clauses:

 (defn mapcat' [f xs] (->> (apply concat mapped) (let [mapped (map f xs)]))) 

Not exactly the style that is popular with Clojure programmers, but it serves as an example of something that ->> can do it partial can not.

+12
source

Source: https://habr.com/ru/post/1261883/


All Articles