-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2adfdfc
Showing
5 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pom.xml | ||
*jar | ||
lib | ||
classes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))))) |