From 3967757ebd08837b230c308135f9244deb2f4807 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Str=C3=B6mberg?= Date: Wed, 6 Dec 2023 23:37:51 +0100 Subject: [PATCH] Port `clojure.core/reductions` * Fixes #418 --- resources/squint/core.edn | 1 + src/squint/core.js | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/resources/squint/core.edn b/resources/squint/core.edn index efd9df59..0896605f 100644 --- a/resources/squint/core.edn +++ b/resources/squint/core.edn @@ -142,6 +142,7 @@ reduce_kv reduced reduced_QMARK_ + reductions regexp_QMARK_ remove remove_watch diff --git a/src/squint/core.js b/src/squint/core.js index 4a187aa4..b94284a3 100644 --- a/src/squint/core.js +++ b/src/squint/core.js @@ -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;