Skip to content

Commit

Permalink
Initial commit. Also version 1.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
technomancy committed Aug 6, 2010
0 parents commit 2adfdfc
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pom.xml
*jar
lib
classes
29 changes: 29 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# reducate

reducate is a new fancy shiny reduce-like macro! You'll love it!

## Usage

Ever get tired of function literals in your reduce? When you get tired
of them with map, you can switch to a for macro, but no such luck with
reduce ... until now!

(def words [["delicious" "coffee"]
["scrumptious" "scones"]
["hungry" "hackers"]])

;; the old way
(reduce (fn [acc [adj noun]] (conj acc adj))
[] words)
;; => ["delicious" "scrumptious" "hungry"]

;; but look!
(reducate [acc []] [[adj noun] words]
(conj acc adj))
;; => ["delicious" "scrumptious" "hungry"]

## License

Copyright (C) 2010 Seajure

Distributed under the Eclipse Public License, the same as Clojure.
5 changes: 5 additions & 0 deletions project.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(defproject reducate "1.0.0"
:description "A shiny new reduce macro"
:dev-dependencies [[org.clojure/clojure "1.2.0-beta1"]
[org.clojure/clojure-contrib "1.2.0-beta1"]
[swank-clojure "1.3.0-SNAPSHOT"]])
10 changes: 10 additions & 0 deletions src/reducate/core.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(ns reducate.core)

(defmacro reducate
"map:for :: reduce:reducate"
([accum bindings body]
`(reduce (fn ~[(first accum) (first bindings)] ~body)
~(second accum) ~(second bindings)))
([[name value] body]
`(let [[init# & more#] ~value]
(reducate [~'% init#] [~name more#] ~body))))
28 changes: 28 additions & 0 deletions test/reducate/test/core.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(ns reducate.test.core
(:use [reducate.core] :reload)
(:use [clojure.test]))

(def words [["delicious" "coffee"]
["scrumptious" "scones"]
["hungry" "hackers"]])

(deftest test-reducate-three-arg
(is (= 12 (reducate [acc 0]
[n [1 2 3]]
(+ acc (* n 2)))))
(is (= {:sea :jure, :zoka :coffee}
(reducate [acc {}]
[pair [[:sea :jure] [:zoka :coffee]]]
(conj acc pair))))
(is (= " delicious scrumptious hungry"
(reducate [acc ""]
[[adj noun] words]
(str acc " " adj)))))

;; Case 1
;; (reducate [foo bar] (do-something foo bar))
;; (reducate [accum 0] [foo [:bar] x [:y]] (do-something))

(deftest test-reducate-two-arg
(is (= 12 (reducate [n [0 1 2 3]]
(+ % (* n 2))))))

0 comments on commit 2adfdfc

Please sign in to comment.