Permalink
Browse files

Merge branch 'master' of git://github.com/ninjudd/lamina into mapcat-…

…merge

Conflicts:
	src/lamina/core.clj
  • Loading branch information...
2 parents a387ed5 + 5e1f4af commit d72109311e7bc0ffaa806d1ea527b1660d55726a @ztellman committed Dec 17, 2011
Showing with 27 additions and 11 deletions.
  1. +1 −0 src/lamina/core.clj
  2. +9 −11 src/lamina/core/seq.clj
  3. +17 −0 test/lamina/test/channel.clj
View
@@ -77,6 +77,7 @@
ch))
(import-fn seq/fork)
+(import-fn seq/mapcat*)
(import-fn seq/map*)
(import-fn seq/filter*)
(import-fn seq/remove*)
View
@@ -245,30 +245,28 @@
(constant-channel)
(channel)))
-(defn map*
- "Returns a channel which will consume all messages from 'ch', and emit (f msg)."
+(defn mapcat*
+ "Returns a channel which will consume all messages from 'ch', and emit each message in (f msg)."
[f ch]
(let [f (unwrap-fn f)
ch* (dst-channel ch)]
(siphon ch
{ch* #(if (and (drained? ch) (= [nil] %))
%
- (map f %))})
+ (mapcat f %))})
(on-drained ch #(close ch*))
ch*))
+(defn map*
+ "Returns a channel which will consume all messages from 'ch', and emit (f msg)."
+ [f ch]
+ (mapcat* (comp list f) ch))
+
(defn filter*
"Returns a channel which will consume all messages from 'ch', but only emit messages
for which (f msg) is true."
[f ch]
- (let [f (unwrap-fn f)
- ch* (dst-channel ch)]
- (siphon ch
- {ch* #(if (and (drained? ch) (= [nil] %))
- %
- (filter f %))})
- (on-drained ch #(close ch*))
- ch*))
+ (mapcat* #(when (f %) (list %)) ch))
(defn remove*
"Returns a channel which will consume all messages from 'ch', but only emit messages
@@ -332,6 +332,23 @@
(is (drained? ch*))
(is (drained? ch)))))
+(deftest test-mapcat*
+ (let [s [1 2 3]
+ f range]
+
+ (let [ch (apply closed-channel s)
+ ch* (mapcat* f ch)]
+ (is (= (mapcat f s) (channel-seq ch*)))
+ (is (drained? ch))
+ (is (drained? ch*)))
+
+ (let [ch (channel)
+ ch* (mapcat* f ch)]
+ (async-enqueue ch s true)
+ (is (= (mapcat f s) (channel-seq ch* 2500)))
+ (is (drained? ch))
+ (is (drained? ch*)))))
+
(deftest test-map*
(let [s (range 10)
f #(* % 2)]

0 comments on commit d721093

Please sign in to comment.