Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 1779de4a563707c9e47117bd234112d8d8ddc3e9 Takahiro Hozumi committed Oct 23, 2010
Showing with 83 additions and 0 deletions.
  1. +5 −0 .gitignore
  2. +18 −0 README.md
  3. +6 −0 project.clj
  4. +43 −0 src/hozumi/reduce+.clj
  5. +11 −0 test/hozumi/test_reduce+.clj
@@ -0,0 +1,5 @@
+pom.xml
+*jar
+*~
+lib
+classes
@@ -0,0 +1,18 @@
+# clj-reduce+
+
+Clj-reduce+ can take more than two sequence as arg.
+
+## Usage
+
+ (use 'hozumi.reduce+)
+
+ (reduce+ assoc {} [:a :b :c] [1 2 3]) ;take 2 sequence.
+ ;=> {:c 3, :b 2, :a 1}
+
+ (reduce+ #(assoc %1 %2 (/ %3 %4)) {} [:a :b :c] [1 2 3] [4 5 6])
+ ;=> {:c 1/2, :b 2/5, :a 1/4} ;take 3 sequence.
+
+## Installation
+
+Leiningen
+ [org.clojars.hozumi/clj-reduce+ "1.0.0-SNAPSHOT"]
@@ -0,0 +1,6 @@
+(defproject org.clojars.hozumi/clj-reduce+ "1.0.0-SNAPSHOT"
+ :description "FIXME: write"
+ :dependencies [[org.clojure/clojure "1.2.0"]
+ [org.clojure/clojure-contrib "1.2.0"]]
+ :dev-dependencies [[swank-clojure "1.3.0-SNAPSHOT"]
+ [lein-clojars "0.6.0"]])
@@ -0,0 +1,43 @@
+(ns hozumi.reduce+)
+
+(defn- helper [f val colls]
+ (let [s1 (seq (first colls))]
+ (if s1
+ (recur f
+ (apply f val (map first colls))
+ (map next colls))
+ val)))
+
+(defn reduce+
+ ([f coll]
+ (let [s (seq coll)]
+ (if s
+ (reduce+ f (first s) (next s))
+ (f))))
+ ([f val coll]
+ (let [s (seq coll)]
+ (if s
+ (if (chunked-seq? s)
+ (recur f
+ (.reduce (chunk-first s) f val)
+ (chunk-next s))
+ (recur f (f val (first s)) (next s)))
+ val)))
+ ([f val c1 c2]
+ (let [s1 (seq c1)
+ s2 (seq c2)]
+ (if s1
+ (recur f (f val (first s1) (first s2)) (next s1) (next s2))
+ val)))
+ ([f val c1 c2 & colls]
+ (let [s1 (seq c1)
+ s2 (seq c2)
+ scolls (map seq colls)]
+ (if s1
+ (helper f
+ (apply f val (first s1) (first s2) (map first scolls))
+ (cons (next s1)
+ (cons (next s2)
+ (map next scolls))))
+ val))))
+
@@ -0,0 +1,11 @@
+(ns hozumi.test_reduce+
+ (:use [hozumi.reduce+] :reload)
+ (:use [clojure.test]))
+
+(deftest test-reduce+
+ (is (= 6 (reduce+ + [1 2 3])))
+ (is (= 7 (reduce+ + 1 [1 2 3])))
+ (is (= {:c 3, :b 2, :a 1}
+ (reduce+ assoc {} [:a :b :c] [1 2 3])))
+ (is (= {:c 1/2, :b 2/5, :a 1/4}
+ (reduce+ #(assoc %1 %2 (/ %3 %4)) {} [:a :b :c] [1 2 3] [4 5 6]))))

0 comments on commit 1779de4

Please sign in to comment.