Skip to content

Commit c72f833

Browse files
committed
CLJ-2772 Fix regression in drop nthrest nthnext on n != positive integer
1 parent 21150ff commit c72f833

File tree

4 files changed

+63
-14
lines changed

4 files changed

+63
-14
lines changed

src/clj/clojure/core.clj

+11-3
Original file line numberDiff line numberDiff line change
@@ -2942,7 +2942,11 @@
29422942
(rf result input))))))))
29432943
([n coll]
29442944
(if (instance? clojure.lang.IDrop coll)
2945-
(or (.drop ^clojure.lang.IDrop coll n) ())
2945+
(or
2946+
(if (pos? n)
2947+
(.drop ^clojure.lang.IDrop coll (if (int? n) n (Math/ceil n)))
2948+
(seq coll))
2949+
())
29462950
(let [step (fn [n coll]
29472951
(let [s (seq coll)]
29482952
(if (and (pos? n) s)
@@ -3170,7 +3174,9 @@
31703174
:static true}
31713175
[coll n]
31723176
(if (instance? clojure.lang.IDrop coll)
3173-
(.drop ^clojure.lang.IDrop coll n)
3177+
(if (pos? n)
3178+
(.drop ^clojure.lang.IDrop coll (if (int? n) n (Math/ceil n)))
3179+
(seq coll))
31743180
(loop [n n xs (seq coll)]
31753181
(if (and xs (pos? n))
31763182
(recur (dec n) (next xs))
@@ -3182,7 +3188,9 @@
31823188
:static true}
31833189
[coll n]
31843190
(if (instance? clojure.lang.IDrop coll)
3185-
(or (.drop ^clojure.lang.IDrop coll n) ())
3191+
(if (pos? n)
3192+
(or (.drop ^clojure.lang.IDrop coll (if (int? n) n (Math/ceil n))) ())
3193+
coll)
31863194
(loop [n n xs coll]
31873195
(if-let [xs (and (pos? n) (seq xs))]
31883196
(recur (dec n) (rest xs))

src/jvm/clojure/lang/IDrop.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@ public interface IDrop{
2020
* useful if the returned coll implements IDrop for subsequent use in a
2121
* partition-like scenario.
2222
*
23-
* If n is <= 0, return this.
24-
* If n drops to or past the end of the collection, return null.
25-
*
26-
* @param n Items to drop
27-
* @return Collection that is Sequential, ISeq, and IReduceInit
23+
* @param n Items to drop, must be > 0
24+
* @return Collection that is Sequential, ISeq, and IReduceInit, or null if past the end
2825
*/
2926
Sequential drop(int n);
3027
}

src/jvm/clojure/lang/PersistentVector.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,7 @@ public Object kvreduce(IFn f, Object init){
366366
}
367367

368368
public Sequential drop(int n) {
369-
if(n < 0) {
370-
return this;
371-
} else if(n < cnt) {
369+
if(n < cnt) {
372370
int offset = n%32;
373371
return new ChunkedSeq(this, this.arrayFor(n), n-offset, offset);
374372
} else {
@@ -617,7 +615,7 @@ public int count(){
617615
ensureEditable();
618616
return cnt;
619617
}
620-
618+
621619
Node ensureEditable(Node node){
622620
if(node.edit == root.edit)
623621
return node;

test/clojure/test_clojure/sequences.clj

+48-2
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,9 @@
855855

856856
(take 0 [1 2 3 4 5]) ()
857857
(take -1 [1 2 3 4 5]) ()
858-
(take -2 [1 2 3 4 5]) () ))
858+
(take -2 [1 2 3 4 5]) ()
859+
860+
(take 1/4 [1 2 3 4 5]) '(1) ))
859861

860862

861863
(deftest test-drop
@@ -867,8 +869,52 @@
867869

868870
(drop 0 [1 2 3 4 5]) '(1 2 3 4 5)
869871
(drop -1 [1 2 3 4 5]) '(1 2 3 4 5)
870-
(drop -2 [1 2 3 4 5]) '(1 2 3 4 5) ))
872+
(drop -2 [1 2 3 4 5]) '(1 2 3 4 5)
873+
874+
(drop 1/4 [1 2 3 4 5]) '(2 3 4 5) )
875+
876+
(are [coll] (= (drop 4 coll) (drop -2 (drop 4 coll)))
877+
[0 1 2 3 4 5]
878+
(seq [0 1 2 3 4 5])
879+
(range 6)
880+
(repeat 6 :x))
881+
)
882+
883+
(deftest test-nthrest
884+
(are [x y] (= x y)
885+
(nthrest [1 2 3 4 5] 1) '(2 3 4 5)
886+
(nthrest [1 2 3 4 5] 3) '(4 5)
887+
(nthrest [1 2 3 4 5] 5) ()
888+
(nthrest [1 2 3 4 5] 9) ()
889+
890+
(nthrest [1 2 3 4 5] 0) '(1 2 3 4 5)
891+
(nthrest [1 2 3 4 5] -1) '(1 2 3 4 5)
892+
(nthrest [1 2 3 4 5] -2) '(1 2 3 4 5)
893+
894+
(nthrest [1 2 3 4 5] 1/4) '(2 3 4 5)
895+
(nthrest [1 2 3 4 5] 1.2) '(3 4 5) )
896+
897+
;; (nthrest coll 0) should return coll
898+
(are [coll] (let [r (nthrest coll 0)] (and (= coll r) (= (class coll) (class r))))
899+
[1 2 3]
900+
(seq [1 2 3])
901+
(range 10)
902+
(repeat 10 :x)
903+
(seq "abc") ))
904+
905+
(deftest test-nthnext
906+
(are [x y] (= x y)
907+
(nthnext [1 2 3 4 5] 1) '(2 3 4 5)
908+
(nthnext [1 2 3 4 5] 3) '(4 5)
909+
(nthnext [1 2 3 4 5] 5) nil
910+
(nthnext [1 2 3 4 5] 9) nil
911+
912+
(nthnext [1 2 3 4 5] 0) '(1 2 3 4 5)
913+
(nthnext [1 2 3 4 5] -1) '(1 2 3 4 5)
914+
(nthnext [1 2 3 4 5] -2) '(1 2 3 4 5)
871915

916+
(nthnext [1 2 3 4 5] 1/4) '(2 3 4 5)
917+
(nthnext [1 2 3 4 5] 1.2) '(3 4 5) ))
872918

873919
(deftest test-take-nth
874920
(are [x y] (= x y)

0 commit comments

Comments
 (0)