Skip to content

Commit

Permalink
Support toFn in group-by
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Dec 9, 2023
1 parent 6760f97 commit adae713
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/squint/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1512,6 +1512,7 @@ export function update_BANG_(m, k, f, ...args) {
}

export function group_by(f, coll) {
f = toFn(f);
const res = {};
for (const o of iterable(coll)) {
const key = f(o);
Expand Down
6 changes: 5 additions & 1 deletion test/squint/compiler_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1531,7 +1531,11 @@
(deftest group-by-test
(is (eq [1 3] (jsv! '(get (group-by odd? [1 2 3 4]) true))))
(is (eq [2 4] (jsv! '(get (group-by odd? [1 2 3 4]) false))))
(is (eq [[1 [1 2 3]]] (jsv! '(get (group-by second [[1 [1 2 3]] [2 [3 4 5]]]) [1 2 3])))))
(is (eq [[1 [1 2 3]]] (jsv! '(get (group-by second [[1 [1 2 3]] [2 [3 4 5]]]) [1 2 3]))))
(is (eq #js [#js {:weight 1000, :name "John"} #js {:weight 1000, :name "Jim"}]
(jsv! '(get (group-by
:weight
[{:weight 1000 :name :John} {:weight 1000 :name :Jim} {:weight 999 :name :Mary}]) 1000)))))

(deftest frequencies-test
(is (eq 3 (jsv! '(get (frequencies [:a :a :b :b :b]) :b)))))
Expand Down

0 comments on commit adae713

Please sign in to comment.