File tree Expand file tree Collapse file tree 1 file changed +20
-2
lines changed Expand file tree Collapse file tree 1 file changed +20
-2
lines changed Original file line number Diff line number Diff line change @@ -22,9 +22,27 @@ pub fn rev_inplace[T](self : ArrayView[T]) -> Unit {
22
22
}
23
23
24
24
///|
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
+ /// ```
25
43
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 )
28
46
}
29
47
}
30
48
You can’t perform that action at this time.
0 commit comments