Skip to content

Commit

Permalink
Make mapcat be a transducer if no collections are provided (#608)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasseglare authored Feb 11, 2025
1 parent ec378ae commit bb0a52e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
## Unreleased

- Fix [#605](https://github.com/squint-cljs/squint/issues/605): merge command line `--paths` with `squint.edn` config properly
- Fix [#607](https://github.com/squint-cljs/squint/issues/607): make `mapcat` return a transducer if no collections are provided

## v0.8.132 (2024-12-29)

Expand Down
8 changes: 6 additions & 2 deletions src/squint/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1165,8 +1165,12 @@ concat[IApply__apply] = (colls) => {
};

export function mapcat(f, ...colls) {
const mapped = map(f, ...colls);
return concat1(mapped);
if (colls.length === 0) {
return comp(map(f), cat);
} else {
const mapped = map(f, ...colls);
return concat1(mapped);
}
}

export function identity(x) {
Expand Down
4 changes: 3 additions & 1 deletion test/squint/compiler_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,9 @@
(is (eq [0 1 2 3 4 5 6 7 8 9] (jsv! '(vec (mapcat identity [[0 1 2 3] [4 5 6] [7 8 9]])))))
(is (eq ["a" "b" "c" "d"] (jsv! '(vec (mapcat identity {"a" "b" "c" "d"})))))
(testing "multiple colls"
(is (eq ["a" 1 "b" 2] (jsv! '(vec (mapcat list [:a :b :c] [1 2])))))))
(is (eq ["a" 1 "b" 2] (jsv! '(vec (mapcat list [:a :b :c] [1 2]))))))
(testing "transducer"
(is (eq [1 1001 2 1002 3 1003] (into [] (mapcat (fn [x] [x (+ 1000 x)])) [1 2 3])))))

(deftest laziness-test
(is (eq ["right" "up" "left" "left" "down" "down" "right" "right" "right" "up" "up" "up" "left" "left" "left" "left" "down" "down" "down" "down"]
Expand Down

0 comments on commit bb0a52e

Please sign in to comment.