Skip to content

Commit b74131f

Browse files
committed
keep transducer
1 parent cd4bd3c commit b74131f

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/squint/core.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,8 +1742,26 @@ export function not_every_QMARK_(pred, coll) {
17421742
return !every_QMARK_(pred, coll);
17431743
}
17441744

1745+
function keep1(pred) {
1746+
return (rf) => {
1747+
return (...args) => {
1748+
const al = args.length;
1749+
if (al === 0) return rf();
1750+
if (al === 1) return rf(args[0]);
1751+
if (al === 2) {
1752+
const result = args[0];
1753+
const input = args[1];
1754+
const v = pred(input);
1755+
if (v == null) return result;
1756+
return rf(result, v);
1757+
}
1758+
};
1759+
};
1760+
}
1761+
17451762
export function keep(pred, coll) {
17461763
pred = toFn(pred);
1764+
if (arguments.length === 1) return keep1(pred);
17471765
return lazy(function* () {
17481766
for (const o of iterable(coll)) {
17491767
const res = pred(o);

0 commit comments

Comments
 (0)