As an exercise, I was trying to convert the following Clojure code to Mathematica:
user=> (->> (range) (map #(* % %)) (filter even?) (take 10) (reduce +)) 1140
I got partway with this code in which I wanted to use the postfix version of the corresponding functions in order to have the same reading of the functions as with Clojure's threading operator:
Range@20-1 // Map[#*# &] // Select[EvenQ[#] &]
I was stuck with the
reduce because in trying to apply the equivalent
Fold I found out there's no operator form of these functions and so I couldn't use them in postfix form. Is there some alternative way to get the same left-to-right sequence of functions as with Clojure?
For those not familiar with Clojure, here's a brief explanation of the threading operator
->> where I obtained the sample above. You can also find help and examples for the other functions at that link. Note that other than
Fold in Mathematica) and
Select in Mathematica), the other Clojure functions have the same name as their Mathematica equivalents.
#(* % %) is a lambda function (pure function as Mathematica calls them) and is equivalent to