feat: add ForEachWithBreak support break for each loop

This commit is contained in:
dudaodong committed 2023-03-15 14:38:33 +08:00
1 parent ef1e548dfc
commit 0734f220b3
3 files changed
+47

No files matched your search

+11
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 {