Fork me on GitHub
#code-reviews
<
2022-11-17
>
dumrat13:11:08

What's the reason for this seperate comp implementation in malli and why are the clj and cljs versions different?

(defn -comp
  ([] identity)
  ([f] f)
  ([f g] (fn [x] (f (g x))))
  ([f g h] (fn [x] (f (g (h x)))))
  #?@(:clj  [([f1 f2 f3 f4] (fn [x] (-> x f4 f3 f2 f1)))
             ([f1 f2 f3 f4 f5] (fn [x] (-> x f5 f4 f3 f2 f1)))
             ([f1 f2 f3 f4 f5 f6] (fn [x] (-> x f6 f5 f4 f3 f2 f1)))
             ([f1 f2 f3 f4 f5 f6 f7] (fn [x] (-> x f7 f6 f5 f4 f3 f2 f1)))
             ([f1 f2 f3 f4 f5 f6 f7 f8] (fn [x] (-> x f8 f7 f6 f5 f4 f3 f2 f1)))
             ([f1 f2 f3 f4 f5 f6 f7 f8 & fs]
              (-comp
               (apply -comp fs)
               (fn [x] (-> x f8 f7 f6 f5 f4 f3 f2 f1))))]
      :cljs [([f1 f2 f3 & fs]
              (-comp
               (apply -comp fs)
               (fn [x] (-> x f3 f2 f1))))]))
https://github.com/metosin/malli/blob/a75bffcdf5443323a9e5491e9f8c2cd578b368d6/src/malli/core.cljc#L174

hiredman17:11:17

mali's -comp is slightly different from clojure.core/comp, the function it builds only takes 1 argument ever

hiredman17:11:33

it is unrolled more, so calling -comp would be maybe slightly faster

hiredman17:11:13

(calling -comp is distinct from calling the function it produces)