Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] - filter #86

Merged
merged 1 commit into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -660,6 +660,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