Skip to content

Commit

Permalink
Port clojure.core/reductions
Browse files Browse the repository at this point in the history
  • Loading branch information
PEZ committed Dec 6, 2023
1 parent 1200c3e commit 3967757
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions resources/squint/core.edn
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
reduce_kv
reduced
reduced_QMARK_
reductions
regexp_QMARK_
remove
remove_watch
Expand Down
26 changes: 26 additions & 0 deletions src/squint/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,32 @@ export function reduce(f, arg1, arg2) {
return val;
}

function _reductions(f, arg1, arg2) {
const [init, coll] = arg2 === undefined ? [undefined, arg1]: [arg1, arg2];
const s = seq(coll);
if (init === undefined) {
// (reductions f coll)
return new LazySeq(function () {
return s ? _reductions(f, first(s), rest(s)) : list(f());
});
} else {
// (reductions f val coll)
if (reduced_QMARK_(init)) {
return list(init.value);
}
return cons(init, new LazySeq(function () {
if (s) {
return _reductions(f, f(init, first(s)), rest(s));
}
}));
}
}

export function reductions(f, arg1, arg2) {
f = toFn(f);
return _reductions(f, arg1, arg2);
}

var tolr = false;
export function warn_on_lazy_reusage_BANG_() {
tolr = true;
Expand Down

0 comments on commit 3967757

Please sign in to comment.