Piping data through arbitrary functions in Clojure

前端 未结 3 1949
长发绾君心
长发绾君心 2021-01-07 22:10

I know that the -> form can be used to pass the results of one function result to another:

(f1 (f2 (f3 x))) 
(-> x f3 f2 f1) ; equivalent          


        
3条回答
  •  一整个雨季
    2021-01-07 22:57

    I must admit I'm really new to clojure and I might be missing the point here completely, but can't this just be done using comp and apply?

    user> (defn fn1 [x] (+ 2 x))
    user> (defn fn2 [x] (/ x 3))
    user> (defn fn3 [x] (* 1.2 x))
    user> (defn pipe [initial-data my-functions] ((apply comp my-functions) initial-data))
    user> (pipe 2 [fn1 fn2 fn3])
    2.8
    

提交回复
热议问题