Skip to content
Closed
Show file tree
Hide file tree
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
33 changes: 33 additions & 0 deletions array/array.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -568,3 +568,36 @@ test "ends_with" {
@assertion.assert_true(arr.ends_with([3, 4, 5]))?
@assertion.assert_false(arr.ends_with([2, 3, 4, 5]))?
}

/// Calculates the sum of the array.
///
/// Only available if the elements implements the `Default` trait and have the
/// `op_add()` function. The result is simply adding all elements sequentially
/// to the default value of the element type, which means the `sum()` of an
/// empty array is the default value itself.
///
/// # Examples
///
/// ```
/// let arr = [1, 2, 3, 4, 5]
/// print(arr.sum()) // output: 15
///
/// let empty_arr: Array[Int] = []
/// print(empty_arr.sum()) // output: 0
/// ```
pub fn sum[T : @num.Num + Default](self : Array[T]) -> T {
loop 0, T::default() {
i, acc =>
if i < self.length() {
continue i + 1, acc + self[i]
} else {
acc
}
}
}

test "sum" {
@assertion.assert_eq([].sum(), 0)?
@assertion.assert_eq([1024].sum(), 1024)?
@assertion.assert_eq([1, 2, 3, 4, 5].sum(), 15)?
}
2 changes: 2 additions & 0 deletions array/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"import": [
"moonbitlang/core/assertion",
"moonbitlang/core/math",
"moonbitlang/core/num",
"moonbitlang/core/int",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int should be a test_import only.

"moonbitlang/core/coverage"
]
}
26 changes: 26 additions & 0 deletions list/list.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -1116,3 +1116,29 @@ test "intercalate" {
List::[1, 2, 3, 0, 4, 5, 6, 0, 7, 8, 9],
)?
}

/// Calculates the sum of the list.
///
/// Only available if the elements implements the `Default` trait and have the
/// `op_add()` function. The result is simply adding all elements sequentially
/// to the default value of the element type, which means the `sum()` of an
/// empty list is the default value itself.
///
/// # Examples
///
/// ```
/// let ls = List::[1, 2, 3, 4, 5]
/// print(ls.sum()) // output: 15
///
/// let empty_list: List[Int] = Nil
/// print(empty_list.sum()) // output: 0
/// ```
pub fn sum[T : @num.Num + Default](self : List[T]) -> T {
self.fold(T::default(), fn { acc, x => acc + x })
}

test "sum" {
@assertion.assert_eq(List::Nil.sum(), 0)?
@assertion.assert_eq(List::Cons(1024, Nil).sum(), 1024)?
@assertion.assert_eq(List::[1, 2, 3, 4, 5].sum(), 15)?
}
4 changes: 3 additions & 1 deletion list/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"import": [
"moonbitlang/core/assertion",
"moonbitlang/core/array",
"moonbitlang/core/coverage"
"moonbitlang/core/coverage",
"moonbitlang/core/int",
"moonbitlang/core/num"
]
}