Skip to content

Commit

Permalink
Merge branch 'main' into refactor-test-use-assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
lijunchen authored Mar 12, 2024
2 parents 38bd494 + 031a0d6 commit 01a8122
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 @@ -176,3 +176,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 01a8122

Please sign in to comment.