Skip to content

Commit

Permalink
Use direct recursion in folding functions
Browse files Browse the repository at this point in the history
  • Loading branch information
s-and-witch committed Oct 14, 2024
1 parent 03e9ab5 commit 11613a5
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/PersistentOrderedMap.mo
Original file line number Diff line number Diff line change
Expand Up @@ -536,11 +536,14 @@ module {
combine : (Key, Value, Accum) -> Accum
) : Accum
{
var acc = base;
for(val in iter(rbMap, #fwd)){
acc := combine(val.0, val.1, acc);
};
acc
switch (rbMap) {
case (#leaf) { base };
case (#node(_, l, (k, v), r)) {
let left = foldLeft(l, base, combine);
let middle = combine(k, v, left);
foldLeft(r, middle, combine)
}
}
};

/// Collapses the elements in `rbMap` into a single value by starting with `base`
Expand Down Expand Up @@ -576,11 +579,14 @@ module {
combine : (Key, Value, Accum) -> Accum
) : Accum
{
var acc = base;
for(val in iter(rbMap, #bwd)){
acc := combine(val.0, val.1, acc);
};
acc
switch (rbMap) {
case (#leaf) { base };
case (#node(_, l, (k, v), r)) {
let right = foldRight(r, base, combine);
let middle = combine(k, v, right);
foldRight(l, middle, combine)
}
}
};


Expand Down

0 comments on commit 11613a5

Please sign in to comment.