Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

ground cases for maps of zero colls #20

Open
wants to merge 1 commit into from

2 participants

MenTaLguY Colin Taylor
MenTaLguY

The pattern (apply map f colls) appears in a lot of Clojure code (including clojure.core, in the implementation of mapcat), but it currently requires extra logic to not be buggy if colls is an empty sequence, since there is no single-argument variant of map. It's easy enough to extend map to usefully cover a single argument, though; (map f) can simply return (), analogous to the way that (+) returns 0.

This branch adds such a ground case to map, mapv, and pmap, and adds a test for mapcat, which already accepted a single argument (but which had been broken for the reasons given above).

MenTaLguY

After some soul-searching about the CA process, I'm releasing this patch into the public domain.

Colin Taylor

i like this but it needs to be done via dev mailing list and jira post CA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Mar 24, 2012
MenTaLguY mental ground cases for maps of zero colls 93aa45d
This page is out of date. Refresh to see the latest.
3  src/clj/clojure/core.clj
@@ -2421,6 +2421,7 @@
2421 2421 f should accept number-of-colls arguments."
2422 2422 {:added "1.0"
2423 2423 :static true}
  2424 + ([f] ())
2424 2425 ([f coll]
2425 2426 (lazy-seq
2426 2427 (when-let [s (seq coll)]
@@ -6050,6 +6051,7 @@
6050 6051 f should accept number-of-colls arguments."
6051 6052 {:added "1.4"
6052 6053 :static true}
  6054 + ([f] [])
6053 6055 ([f coll]
6054 6056 (-> (reduce (fn [v o] (conj! v (f o))) (transient []) coll)
6055 6057 persistent!))
@@ -6164,6 +6166,7 @@
6164 6166 the coordination overhead."
6165 6167 {:added "1.0"
6166 6168 :static true}
  6169 + ([f] ())
6167 6170 ([f coll]
6168 6171 (let [n (+ 2 (.. Runtime getRuntime availableProcessors))
6169 6172 rets (map #(future (f %)) coll)
4 test/clojure/test_clojure/parallel.clj
@@ -23,6 +23,10 @@
23 23
24 24 ;; pmap
25 25 ;;
  26 +;;
  27 +(deftest pmap-no-colls
  28 + (is (= () (pmap +))))
  29 +
26 30 (deftest pmap-does-its-thing
27 31 ;; regression fixed in r1218; was OutOfMemoryError
28 32 (is (= '(1) (pmap inc [0]))))
6 test/clojure/test_clojure/sequences.clj
@@ -18,6 +18,12 @@
18 18 ; apply, map, filter, remove
19 19 ; and more...
20 20
  21 +(deftest test-map
  22 + (is (= () (map +))))
  23 +
  24 +(deftest test-mapcat
  25 + (is (= () (mapcat +))))
  26 +
21 27 (deftest test-reduce-from-chunked-into-unchunked
22 28 (= [1 2 \a \b] (into [] (concat [1 2] "ab"))))
23 29
2  test/clojure/test_clojure/vectors.clj
@@ -357,6 +357,8 @@
357 357 [expected v] (and (vector? v) (= expected v)))
358 358
359 359 (deftest test-mapv
  360 + (are [r] (=vec r (mapv +))
  361 + [])
360 362 (are [r c1] (=vec r (mapv + c1))
361 363 [1 2 3] [1 2 3])
362 364 (are [r c1 c2] (=vec r (mapv + c1 c2))

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.