Skip to content

Commit

Permalink
drop-while, fixes #54
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Aug 29, 2022
1 parent 755f8da commit f9baf95
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
20 changes: 19 additions & 1 deletion core.js
Original file line number Diff line number Diff line change
Expand Up @@ -824,10 +824,28 @@ export function cycle(coll) {

export function drop(n, xs) {
return new LazyIterable(function* () {
let iter = _iterator(xs);
let iter = _iterator(iterable(xs));
for (let x = 0; x < n; x++) {
iter.next();
}
yield* iter;
});
}

export function drop_while(pred, xs) {
return new LazyIterable(function* () {
let iter = _iterator(iterable(xs));
while (true) {
let nextItem = iter.next();
if (nextItem.done) {
break;
}
let value = nextItem.value;
if (!pred(value)) {
yield value;
break;
}
}
yield* iter;
});
}
1 change: 1 addition & 0 deletions resources/clava/core.edn
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
dissoc
dissoc_BANG_
drop
drop_while
empty
es6_iterator
even_QMARK_
Expand Down
10 changes: 9 additions & 1 deletion test/clava/compiler_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,15 @@
(let [dropped (jsv! '(drop 3 (range 6)))]
(is (eq [3 4 5] (vec dropped)))
;; iterating over dropped second time, still works:
(is (eq [3 4 5] (vec dropped)))))
(is (eq [3 4 5] (vec dropped))))
(is (eq (drop 2 nil) (vec (jsv! '(drop 2 nil))))))

(deftest drop-while-test
(let [dropped (jsv! '(drop-while odd? [1 1 1 2 3 4]))]
(is (eq (drop-while odd? [1 1 1 2 3 4]) (vec dropped)))
;; iterating over dropped second time, still works:
(is (eq (drop-while odd? [1 1 1 2 3 4]) (vec dropped))))
(is (eq (drop-while odd? nil) (vec (jsv! '(drop-while odd? nil))))))

(defn init []
(cljs.test/run-tests 'clava.compiler-test 'clava.jsx-test))

0 comments on commit f9baf95

Please sign in to comment.