1
0
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:
Idichekop
2025-07-30 08:28:27 +02:00
committed by GitHub
parent ae1014c572
commit cb8d93c499

View File

@@ -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
} }
} }