Permalink
Browse files

Add collude. I'm tired of lusting after Haskell's ***

  • Loading branch information...
amalloy committed Nov 3, 2011
1 parent 70e12c8 commit b6d70726aba068bf6591331430d6ba90007030f8
Showing with 12 additions and 0 deletions.
  1. +8 −0 src/useful/fn.clj
  2. +4 −0 test/useful/fn_test.clj
View
@@ -63,6 +63,14 @@
(fn [& args]
(every? #(apply % args) preds)))
+(defn collude
+ "Takes a list of functions (f1 f2 ... fn) and returns a new function F. F takes
+ n arguments (x1 x2 ... xn) and returns the list ((f1 x1) (f2 x2) ... (fn xn)).
+ Similar to Haskell's ***, and a nice complement to juxt (which is Haskell's &&&)."
+ [& fs]
+ (fn [& args]
+ (map #(% %2) fs args)))
+
(defn thrush
"Takes the first argument and applies the remaining arguments to it as functions from left to right.
This tiny implementation was written by Chris Houser. http://blog.fogus.me/2010/09/28/thrush-in-clojure-redux"
View
@@ -43,6 +43,10 @@
#(zero? (rem % 3)))
(range 11)))))
+(deftest test-collude
+ (is (= [5 \t 9]
+ ((collude inc last #(* 3 %)) 4 "last" 3))))
+
(deftest test-thrush
(is (= 5 (thrush 1 inc inc inc inc))))

0 comments on commit b6d7072

Please sign in to comment.