mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
Simple refactor of ForEach functions (#323)
This commit is contained in:
@@ -507,19 +507,17 @@ func flattenRecursive(value reflect.Value, result reflect.Value) reflect.Value {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ForEach iterates over elements of slice and invokes function for each element.
|
// ForEach iterates over elements of slice and invokes function for each element.
|
||||||
// Play: https://go.dev/play/p/DrPaa4YsHRF
|
|
||||||
func ForEach[T any](slice []T, iteratee func(index int, item T)) {
|
func ForEach[T any](slice []T, iteratee func(index int, item T)) {
|
||||||
for i := 0; i < len(slice); i++ {
|
for idx, elem := range slice {
|
||||||
iteratee(i, slice[i])
|
iteratee(idx, elem)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ForEachWithBreak iterates over elements of slice and invokes function for each element,
|
// ForEachWithBreak iterates over elements of slice and invokes function for each element,
|
||||||
// when iteratee return false, will break the for each loop.
|
// when function return false, will break the for each loop.
|
||||||
// Play: https://go.dev/play/p/qScs39f3D9W
|
|
||||||
func ForEachWithBreak[T any](slice []T, iteratee func(index int, item T) bool) {
|
func ForEachWithBreak[T any](slice []T, iteratee func(index int, item T) bool) {
|
||||||
for i := 0; i < len(slice); i++ {
|
for idx, elem := range slice {
|
||||||
if !iteratee(i, slice[i]) {
|
if !iteratee(idx, elem) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user