Skip to content

Commit

Permalink
Merge branch 'main' into feature/result
Browse files Browse the repository at this point in the history
  • Loading branch information
CAIMEOX authored Mar 12, 2024
2 parents c22c3c7 + 031a0d6 commit 2996e92
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions array/array.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,20 @@ test "from_array" {
let array = Array::[1, 2, 3, 4, 5]
@assertion.assert_eq(array, [1, 2, 3, 4, 5])?
}

pub fn fold[T, U](self : Array[T], f : (T, U) -> U, init : U) -> U {
loop 0, init {
i, acc =>
if i < self.length() {
continue i + 1,
f(self[i], acc)
} else {
acc
}
}
}

test "fold" {
let sum = [1, 2, 3, 4, 5].fold(fn(elem, sum : Int) { sum + elem }, 0)
@assertion.assert_eq(sum, 15)?
}

0 comments on commit 2996e92

Please sign in to comment.