在 Haskell 中,我们有Data.Function.on:
Data.Function.on
。on :: (b -> b -> c) -> (a -> b) -> a -> a -> c
(.*.) `on` f = \x y -> f x .*. f y
在Clojure中,我希望能够像下面这样定义一个字谜谓词:(defn anagram? [word other-word]
(and (not= word other-word)
((on = sort) word other-word)))
它很容易实现:
(defn on [g f] (fn [x y] (g (f x) (f y))))
但是有没有内置函数能够实现相同的目标呢?我似乎找不到。
(defn on [g f] #(apply g (map f %&)))
是Clojure中支持变长参数的精神。 - noisesmith