diff options
author | Rich Hickey <richhickey@gmail.com> | 2007-09-27 01:41:35 +0000 |
---|---|---|
committer | Rich Hickey <richhickey@gmail.com> | 2007-09-27 01:41:35 +0000 |
commit | a255c8e8bc00b9adddd02c0e9714d862dacf8709 (patch) | |
tree | 544d89737d7712cf15ed0be6f5206f26b6c580f7 /src | |
parent | 95e04a8d55631a9e83f44f94a95a2ed527741faa (diff) |
more functional libs
Diffstat (limited to 'src')
-rw-r--r-- | src/boot.clj | 65 |
1 files changed, 42 insertions, 23 deletions
diff --git a/src/boot.clj b/src/boot.clj index 9beacf4c..d2e2e309 100644 --- a/src/boot.clj +++ b/src/boot.clj @@ -318,6 +318,25 @@ `(. clojure.lang.LockingTransaction (runInTransaction (fn [] ~@body)))) +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; fn stuff ;;;;;;;;;;;;;;;; +(defn comp [& fs] + (let [fs (reverse fs)] + (fn [& args] + (loop [ret (apply (first fs) args) fs (rest fs)] + (if fs + (recur ((first fs) ret) (rest fs)) + ret))))) + +(defn curry + ([f arg1] + (fn [& args] (apply f arg1 args))) + ([f arg1 arg2] + (fn [& args] (apply f arg1 arg2 args))) + ([f arg1 arg2 arg3] + (fn [& args] (apply f arg1 arg2 arg3 args))) + ([f arg1 arg2 arg3 & more] + (fn [& args] (apply f arg1 arg2 arg3 (concat more args))))) + ;;;;;;;;;;;;;;;;;;; sequence fns ;;;;;;;;;;;;;;;;;;;;;;; (defn every [pred coll] @@ -326,6 +345,14 @@ (recur pred (rest coll))) t)) +(def not-every (comp not every)) + +(defn any [pred coll] + (when (seq coll) + (or (pred (first coll)) (recur pred (rest coll))))) + +(def not-any (comp not any)) + (defn map ([f coll] (when (seq coll) @@ -335,6 +362,9 @@ (lazy-cons (apply f (first coll) (map first colls)) (apply map f (rest coll) (map rest colls)))))) +(defn mapcat [f & colls] + (apply concat (apply map f colls))) + (defn reduce ([f coll] (if (seq coll) @@ -345,15 +375,19 @@ (recur f (rest coll) (f val (first coll))) val))) +(defn filter [pred coll] + (when (seq coll) + (if (pred (first coll)) + (lazy-cons (first coll) (filter pred (rest coll))) + (recur pred (rest coll))))) + (defn take [n coll] (when (and (pos? n) (seq coll)) (lazy-cons (first coll) (take (dec n) (rest coll))))) (defn take-while [pred coll] - (when (seq coll) - (if (pred (first coll)) - (lazy-cons (first coll) (take-while pred (rest coll))) - (recur pred (rest coll))))) + (when (and (seq coll) (pred (first coll))) + (lazy-cons (first coll) (take-while pred (rest coll))))) (defn drop [n coll] (if (and (pos? n) (seq coll)) @@ -377,9 +411,12 @@ (when (seq coll) (cycle-rep (seq coll) (seq coll)))) -(defn split [n coll] +(defn split-at [n coll] [(take n coll) (drop n coll)]) +(defn split-with [pred coll] + [(take-while pred coll) (drop-while pred coll)]) + (defn repeat [x] (lazy-cons x (repeat x))) @@ -390,21 +427,3 @@ (let [v (f x)] (lazy-cons v (iterate f v)))) -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; fn stuff ;;;;;;;;;;;;;;;; -(defn comp [& fs] - (let [fs (reverse fs)] - (fn [& args] - (loop [ret (apply (first fs) args) fs (rest fs)] - (if fs - (recur ((first fs) ret) (rest fs)) - ret))))) - -(defn curry - ([f arg1] - (fn [& args] (apply f arg1 args))) - ([f arg1 arg2] - (fn [& args] (apply f arg1 arg2 args))) - ([f arg1 arg2 arg3] - (fn [& args] (apply f arg1 arg2 arg3 args))) - ([f arg1 arg2 arg3 & more] - (fn [& args] (apply f arg1 arg2 arg3 (concat more args)))))
\ No newline at end of file |