Skip to content

Commit

Permalink
simplify map1 and mapcat1
Browse files Browse the repository at this point in the history
  • Loading branch information
bitsai committed Aug 23, 2011
1 parent 44c0cdf commit 24d5d5d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 51 deletions.
26 changes: 9 additions & 17 deletions Land of Lisp (in Clojure)/ch18/lazy.clj
@@ -1,26 +1,18 @@
(ns lazy)

;; From The Joy of Clojure
(defn seq1
"Like seq, but returns an unchunked sequence."
[coll]
;; Inspired by Joy of Clojure's seq1
(defn- map-join [join f colls]
(lazy-seq
(when-let [[x] (seq coll)]
(cons x (seq1 (rest coll))))))
(when (every? seq colls)
(join (apply f (map first colls))
(map-join join f (map rest colls))))))

(defn map1
"Like map, but applies f one-at-a-time. Returns an unchunked sequence."
"Like map, but returns a de-chunked/1-at-a-time lazy sequence."
[f & colls]
(apply map f (map seq1 colls)))

(defn join
"Like concat, but works on coll of colls. Returns an unchunked sequence."
[colls]
(lazy-seq
(when-let [[x] (seq colls)]
(concat x (join (rest colls))))))
(map-join cons f colls))

(defn mapcat1
"Like mapcat, but applies f one-at-a-time. Returns an unchunked sequence."
"Like mapcat, but returns a de-chunked/1-at-a-time lazy sequence."
[f & colls]
(join (apply map1 f colls)))
(map-join concat f colls))
26 changes: 9 additions & 17 deletions Land of Lisp (in Clojure)/ch19/lazy.clj
@@ -1,26 +1,18 @@
(ns lazy)

;; From The Joy of Clojure
(defn seq1
"Like seq, but returns an unchunked sequence."
[coll]
;; Inspired by Joy of Clojure's seq1
(defn- map-join [join f colls]
(lazy-seq
(when-let [[x] (seq coll)]
(cons x (seq1 (rest coll))))))
(when (every? seq colls)
(join (apply f (map first colls))
(map-join join f (map rest colls))))))

(defn map1
"Like map, but applies f one-at-a-time. Returns an unchunked sequence."
"Like map, but returns a de-chunked/1-at-a-time lazy sequence."
[f & colls]
(apply map f (map seq1 colls)))

(defn join
"Like concat, but works on coll of colls. Returns an unchunked sequence."
[colls]
(lazy-seq
(when-let [[x] (seq colls)]
(concat x (join (rest colls))))))
(map-join cons f colls))

(defn mapcat1
"Like mapcat, but applies f one-at-a-time. Returns an unchunked sequence."
"Like mapcat, but returns a de-chunked/1-at-a-time lazy sequence."
[f & colls]
(join (apply map1 f colls)))
(map-join concat f colls))
26 changes: 9 additions & 17 deletions Land of Lisp (in Clojure)/ch20/lazy.clj
@@ -1,26 +1,18 @@
(ns lazy)

;; From The Joy of Clojure
(defn seq1
"Like seq, but returns an unchunked sequence."
[coll]
;; Inspired by Joy of Clojure's seq1
(defn- map-join [join f colls]
(lazy-seq
(when-let [[x] (seq coll)]
(cons x (seq1 (rest coll))))))
(when (every? seq colls)
(join (apply f (map first colls))
(map-join join f (map rest colls))))))

(defn map1
"Like map, but applies f one-at-a-time. Returns an unchunked sequence."
"Like map, but returns a de-chunked/1-at-a-time lazy sequence."
[f & colls]
(apply map f (map seq1 colls)))

(defn join
"Like concat, but works on coll of colls. Returns an unchunked sequence."
[colls]
(lazy-seq
(when-let [[x] (seq colls)]
(concat x (join (rest colls))))))
(map-join cons f colls))

(defn mapcat1
"Like mapcat, but applies f one-at-a-time. Returns an unchunked sequence."
"Like mapcat, but returns a de-chunked/1-at-a-time lazy sequence."
[f & colls]
(join (apply map1 f colls)))
(map-join concat f colls))

0 comments on commit 24d5d5d

Please sign in to comment.