Skip to content

Commit f8b97e0

Browse files
sgroveswannodette
authored andcommitted
CLJS-1033: take a drop accept nil as n argument
Require n to pass number? predicate when passed to drop or take
1 parent 3bc902b commit f8b97e0

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/main/cljs/cljs/core.cljs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4216,6 +4216,7 @@ reduces them without incurring seq initialization"
42164216
there are fewer than n. Returns a stateful transducer when
42174217
no collection is provided."
42184218
([n]
4219+
{:pre [(number? n)]}
42194220
(fn [rf]
42204221
(let [na (volatile! n)]
42214222
(fn
@@ -4231,6 +4232,7 @@ reduces them without incurring seq initialization"
42314232
(ensure-reduced result)
42324233
result)))))))
42334234
([n coll]
4235+
{:pre [(number? n)]}
42344236
(lazy-seq
42354237
(when (pos? n)
42364238
(when-let [s (seq coll)]
@@ -4240,6 +4242,7 @@ reduces them without incurring seq initialization"
42404242
"Returns a lazy sequence of all but the first n items in coll.
42414243
Returns a stateful transducer when no collection is provided."
42424244
([n]
4245+
{:pre [(number? n)]}
42434246
(fn [rf]
42444247
(let [na (volatile! n)]
42454248
(fn
@@ -4252,6 +4255,7 @@ reduces them without incurring seq initialization"
42524255
result
42534256
(rf result input))))))))
42544257
([n coll]
4258+
{:pre [(number? n)]}
42554259
(let [step (fn [n coll]
42564260
(let [s (seq coll)]
42574261
(if (and (pos? n) s)
@@ -8370,6 +8374,7 @@ reduces them without incurring seq initialization"
83708374
"Returns a lazy seq of every nth item in coll. Returns a stateful
83718375
transducer when no collection is provided."
83728376
([n]
8377+
{:pre [(number? n)]}
83738378
(fn [rf]
83748379
(let [ia (volatile! -1)]
83758380
(fn
@@ -8381,6 +8386,7 @@ reduces them without incurring seq initialization"
83818386
(rf result input)
83828387
result)))))))
83838388
([n coll]
8389+
{:pre [(number? n)]}
83848390
(lazy-seq
83858391
(when-let [s (seq coll)]
83868392
(cons (first s) (take-nth n (drop n s)))))))

src/test/cljs/cljs/core_test.cljs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,13 @@
698698
(is (= (hash 'foo) (hash (symbol "foo"))))
699699
(is (= (hash 'foo/bar) (hash (symbol "foo" "bar"))))
700700
(is (= (lazy-cat [1] [2] [3]) '(1 2 3)))
701-
))
701+
;; Make sure take/drop raise an error when given nil as an argument
702+
(is (try (do (take nil [1 2 3]) false)
703+
(catch js/Error e true)))
704+
(is (try (do (drop nil [1 2 3]) false)
705+
(catch js/Error e true)))
706+
(is (try (do (take-nth nil [1 2 3]) false)
707+
(catch js/Error e true)))))
702708

703709
(deftest test-booleans
704710
(testing "Testing boolean predicates"

0 commit comments

Comments
 (0)