Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions builtin/fixedarray.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -774,9 +774,8 @@ pub fn[A, B] FixedArray::rev_foldi(
init~ : B,
f : (Int, B, A) -> B raise?,
) -> B raise? {
let len = self.length()
for i in len>..0; acc = init {
continue f(len - i - 1, acc, self.unsafe_get(i))
for i in self.length()>..0; index = 0, acc = init {
continue index + 1, f(index, acc, self.unsafe_get(i))
} nobreak {
acc
}
Expand All @@ -797,6 +796,16 @@ test "rev_foldi" {
inspect(sum, content="25")
}

///|
test "rev_foldi preserves reversed index element pairs" {
let trace = ([10, 20, 30] : FixedArray[_]).rev_foldi(init=0, (
index,
acc,
elem,
) => acc * 1000 + index * 100 + elem)
inspect(trace, content="30120210")
}

///|
/// Reverses the array in place by swapping elements from both ends until
/// reaching the middle.
Expand Down
Loading