4clojure #59 Juxtaposition

4clojure #59 Juxtaposition

一連の関数(1)を引数(funcs)にとり、新しい関数(2)を返す関数(3)を作る。 関数(2)は引数(args)を、一連の関数(1orfuncs)へ左から右へ apply した 結果をふくむ seq を返す。

juxt 使用禁止。

(= [21 6 1] ((__ + max min) 2 3 5 1 6 4))
(= ["HELLO" 5] ((__ #(.toUpperCase %) count) "hello"))
(= [2 6 4] ((__ :a :c :b) {:a 2, :b 4, :c 6, :d 8 :e 10}))

外側の関数(my-jux)は一連の関数(= funcs)を引数にして 関数(my-jux-in)を生成するだけでよい。 関数(my-jux-in)は、引数にargsをとる。 関数(my-jux-in)は、 args を funcsapply したseqを返す。

(((fn my-jux [& funcs]
    (fn my-jux-in [& args]
        (map #(apply % args) funcs)))
 + max min) 2 3 5 1 6 4)
参考 #58の答え
(((fn comb [& funcs]
  (fn [& args]
    (first
      (reduce #(list (apply %2 %1)) args (reverse funcs)))))
        #(.toUpperCase %) #(apply str %) take) 5 "hello world"))