Skip to content

Commit

Permalink
[feat #45] - filter (#86)
Browse files Browse the repository at this point in the history
closes #45
  • Loading branch information
brandonstubbs authored Aug 19, 2022
1 parent df694c5 commit 72a7223
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
10 changes: 10 additions & 0 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,16 @@ export function map(f, coll) {
return ret;
}

export function filter(pred, coll) {
let ret = [];
for (const x of iterable(coll)) {
if (pred(x)) {
ret.push(x);
}
}
return ret;
}

export function map_indexed(f, coll) {
let ret = [];
let i = 0;
Expand Down
2 changes: 1 addition & 1 deletion resources/clava/core.edn
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#{seq reduce first dissoc atom dec map assoc_in reset_BANG_ rest PROTOCOL_SENTINEL _PLUS_ reduced_QMARK_ range disj seqable_QMARK_ iterable conj get disj_BANG_ vec second prn odd_QMARK_ assoc_BANG_ println Atom nth nil_QMARK_ assoc_in_BANG_ dissoc_BANG_ not pr_str swap_BANG_ vector mapv subvec reduced even_QMARK_ inc str apply map_indexed deref assoc complement constantly conj_BANG_ re_matches}
#{seq reduce first dissoc atom dec map assoc_in reset_BANG_ rest PROTOCOL_SENTINEL _PLUS_ reduced_QMARK_ range disj seqable_QMARK_ iterable conj get disj_BANG_ vec second prn odd_QMARK_ assoc_BANG_ println Atom nth nil_QMARK_ assoc_in_BANG_ dissoc_BANG_ not pr_str swap_BANG_ vector mapv subvec reduced even_QMARK_ inc filter str apply map_indexed deref assoc complement constantly conj_BANG_ re_matches}
9 changes: 9 additions & 0 deletions test/clava/compiler_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,15 @@
(is (eq () (jsv! '(map inc nil))))
(is (eq () (jsv! '(map inc js/undefined))))))

(deftest filter-test
(is (eq [2 4 6 8] (jsv! '(filter even? [1 2 3 4 5 6 7 8 9]))))
(is (every? (set (jsv! '(filter even? #{1 2 3 4 5 6 7 8 9})))
[2 4 6 8]))
(is (eq [[:a 1]] (jsv! '(filter #(= :a (first %)) {:a 1 :b 2}))))
(testing "nil"
(is (eq () (jsv! '(filter even? nil))))
(is (eq () (jsv! '(filter even? js/undefined))))))

(deftest map-indexed-test
(is (eq [[0 0] [1 1] [2 2] [3 3] [4 4]]
(jsv! '(map-indexed vector [0 1 2 3 4]))))
Expand Down

0 comments on commit 72a7223

Please sign in to comment.