1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-11 08:12:26 +08:00

feat: add ForEachWithBreak support break for each loop

This commit is contained in:
dudaodong
2023-03-15 14:38:33 +08:00
parent ef1e548dfc
commit 0734f220b3
3 changed files with 47 additions and 0 deletions

View File

@@ -421,6 +421,17 @@ func ForEach[T any](slice []T, iteratee func(index int, item T)) {
}
}
// ForEachWithBreak iterates over elements of slice and invokes function for each element,
// when iteratee return true, will break the for each loop.
func ForEachWithBreak[T any](slice []T, iteratee func(index int, item T) bool) {
loop:
for i, v := range slice {
if !iteratee(i, v) {
break loop
}
}
}
// Map creates an slice of values by running each element of slice thru iteratee function.
// Play: https://go.dev/play/p/biaTefqPquw
func Map[T any, U any](slice []T, iteratee func(index int, item T) U) []U {