Skip to content

Commit bff08b5

Browse files
committed
tweak to each to avoid bounds checking
1 parent 6975feb commit bff08b5

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

array/view.mbt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,27 @@ pub fn rev_inplace[T](self : ArrayView[T]) -> Unit {
2222
}
2323

2424
///|
25+
/// Iterates over each element in the array view and applies a function to it.
26+
///
27+
/// Parameters:
28+
///
29+
/// * `self` : The array view to iterate over.
30+
/// * `function` : A function that takes an element of type `T` and returns
31+
/// nothing. This function will be applied to each element in the array view.
32+
///
33+
/// Example:
34+
///
35+
/// ```moonbit
36+
/// test "array_view/each" {
37+
/// let arr = [1, 2, 3][:]
38+
/// let mut sum = 0
39+
/// arr.each(fn(x) { sum = sum + x })
40+
/// inspect!(sum, content="6")
41+
/// }
42+
/// ```
2543
pub fn each[T](self : ArrayView[T], f : (T) -> Unit) -> Unit {
26-
for i = 0; i < self.length(); i = i + 1 {
27-
f(self[i])
44+
for v in self {
45+
f(v)
2846
}
2947
}
3048

0 commit comments

Comments
 (0)