Skip to content

Commit

Permalink
Bring c.c.reducers up-to-date with changes in Clojure
Browse files Browse the repository at this point in the history
  • Loading branch information
michalmarczyk authored and David Nolen committed May 11, 2012
1 parent 67165b0 commit 2469b7c
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions src/cljs/clojure/core/reducers.cljs
Expand Up @@ -13,7 +13,7 @@
dependency info."
:author "Rich Hickey"}
clojure.core.reducers
(:refer-clojure :exclude [reduce map filter remove take take-while drop flatten])
(:refer-clojure :exclude [reduce map mapcat filter remove take take-while drop flatten])
(:require [clojure.walk :as walk]
[cljs.core :as core]))

Expand Down Expand Up @@ -92,6 +92,17 @@
([ret k v]
(f1 ret (f k v)))))))

(defcurried mapcat
"Applies f to every value in the reduction of coll, concatenating the result
colls of (f val). Foldable."
{}
[f coll]
(folder coll
(fn [f1]
(rfn [f1 k]
([ret k v]
(reduce f1 ret (f k v)))))))

(defcurried filter
"Retains values in the reduction of coll for which (pred val)
returns logical true. Foldable."
Expand All @@ -105,6 +116,21 @@
(f1 ret k v)
ret))))))

(defcurried flatten
"Takes any nested combination of sequential things (lists, vectors,
etc.) and returns their contents as a single, flat foldable
collection."
{}
[coll]
(folder coll
(fn [f1]
(fn
([] (f1))
([ret v]
(if (sequential? v)
(-reduce (flatten v) f1 ret)
(f1 ret v)))))))

(defcurried remove
"Removes values in the reduction of coll for which (pred val)
returns logical true. Foldable."
Expand Down Expand Up @@ -152,29 +178,6 @@
(f1 ret k v)
ret)))))))

(defcurried flatten
"Takes any nested combination of sequential things (lists, vectors,
etc.) and returns their contents as a single, flat foldable
collection."
{}
[coll]
(let [rf (fn [f1]
(fn
([] (f1))
([ret v]
(if (sequential? v)
(-reduce (flatten v) f1 ret)
(f1 ret v)))))]
(reify
cljs.core/IReduce
(-reduce [this f1] (-reduce this f1 (f1)))
(-reduce [_ f1 init] (-reduce coll (rf f1) init))

#_
CollFold
#_
(coll-fold [_ n combinef reducef] (coll-fold coll n combinef (rf reducef))))))

;;do not construct this directly, use cat
(deftype Cat [cnt left right]
clojure.lang.Counted
Expand Down

0 comments on commit 2469b7c

Please sign in to comment.