Skip to content

Commit 2430b7a

Browse files
committed
finish flatten refactoring
1 parent aa7ece0 commit 2430b7a

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

src/clj/clojure/core/reducers.clj

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,21 @@
199199
[pred coll]
200200
(filter (complement pred) coll))
201201

202+
(defcurried flatten
203+
"Takes any nested combination of sequential things (lists, vectors,
204+
etc.) and returns their contents as a single, flat foldable
205+
collection."
206+
{:added "1.5"}
207+
[coll]
208+
(folder coll
209+
(fn [f1]
210+
(fn
211+
([] (f1))
212+
([ret v]
213+
(if (sequential? v)
214+
(clojure.core.protocols/coll-reduce (flatten v) f1 ret)
215+
(f1 ret v)))))))
216+
202217
(defcurried take-while
203218
"Ends the reduction of coll when (pred val) returns logical false."
204219
{:added "1.5"}
@@ -239,27 +254,6 @@
239254
(f1 ret k v)
240255
ret)))))))
241256

242-
(defcurried flatten
243-
"Takes any nested combination of sequential things (lists, vectors,
244-
etc.) and returns their contents as a single, flat foldable
245-
collection."
246-
{:added "1.5"}
247-
[coll]
248-
(let [rf (fn [f1]
249-
(fn
250-
([] (f1))
251-
([ret v]
252-
(if (sequential? v)
253-
(clojure.core.protocols/coll-reduce (flatten v) f1 ret)
254-
(f1 ret v)))))]
255-
(reify
256-
clojure.core.protocols/CollReduce
257-
(coll-reduce [this f1] (clojure.core.protocols/coll-reduce this f1 (f1)))
258-
(coll-reduce [_ f1 init] (clojure.core.protocols/coll-reduce coll (rf f1) init))
259-
260-
CollFold
261-
(coll-fold [_ n combinef reducef] (coll-fold coll n combinef (rf reducef))))))
262-
263257
;;do not construct this directly, use cat
264258
(deftype Cat [cnt left right]
265259
clojure.lang.Counted

0 commit comments

Comments
 (0)