8000 Simple refactor of ForEach functions (#323) · duke-git/lancet@cb8d93c · GitHub
[go: up one dir, main page]

Skip to content

Commit cb8d93c

Browse files
authored
Simple refactor of ForEach functions (#323)
1 parent ae1014c commit cb8d93c

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

slice/slice.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -507,19 +507,17 @@ func flattenRecursive(value reflect.Value, result reflect.Value) reflect.Value {
507507
}
508508

509509
// ForEach iterates over elements of slice and invokes function for each element.
510-
// Play: https://go.dev/play/p/DrPaa4YsHRF
511510
func ForEach[T any](slice []T, iteratee func(index int, item T)) {
512-
for i := 0; i < len(slice); i++ {
513-
iteratee(i, slice[i])
511+
for idx, elem := range slice {
512+
iteratee(idx, elem)
514513
}
515514
}
516515

517516
// ForEachWithBreak iterates over elements of slice and invokes function for each element,
518-
// when iteratee return false, will break the for each loop.
519-
// Play: https://go.dev/play/p/qScs39f3D9W
517+
// when function return false, will break the for each loop.
520518
func ForEachWithBreak[T any](slice []T, iteratee func(index int, item T) bool) {
521-
for i := 0; i < len(slice); i++ {
522-
if !iteratee(i, slice[i]) {
519+
for idx, elem := range slice {
520+
if !iteratee(idx, elem) {
523521
break
524522
}
525523
}

0 commit comments

Comments
 (0)
0