From 1603a848b06302f93fb5535a490c6b69a32b4ea7 Mon Sep 17 00:00:00 2001 From: Sianao Luo Date: Sat, 10 Aug 2024 07:25:12 +0800 Subject: [PATCH] Update foreachwhile readme.md (#508) * update readme.md for lo.ForEachWhile play and example code --------- Co-authored-by: Samuel Berthe --- README.md | 15 ++++++++------- slice.go | 3 ++- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index c5b44297..3f73cc8e 100644 --- a/README.md +++ b/README.md @@ -434,18 +434,19 @@ Iterates over collection elements and invokes iteratee for each element collecti ```go list := []int64{1, 2, -42, 4} -ForEachWhile(list, func(x int64, _ int) bool { - if x < 0 { - return false - } - fmt.Println(x) - return true +lo.ForEachWhile(list, func(x int64, _ int) bool { + if x < 0 { + return false + } + fmt.Println(x) + return true }) - // 1 // 2 ``` +[[play](https://go.dev/play/p/QnLGt35tnow)] + ### Times Times invokes the iteratee n times, returning an array of the results of each invocation. The iteratee is invoked with index as argument. diff --git a/slice.go b/slice.go index 431aaf04..d2d3fd84 100644 --- a/slice.go +++ b/slice.go @@ -95,7 +95,8 @@ func ForEach[T any](collection []T, iteratee func(item T, index int)) { } // ForEachWhile iterates over elements of collection and invokes iteratee for each element -// collection return value decide to continue or break ,just like do while() +// collection return value decide to continue or break, like do while(). +// Play: https://go.dev/play/p/QnLGt35tnow func ForEachWhile[T any](collection []T, iteratee func(item T, index int) (goon bool)) { for i := range collection { if !iteratee(collection[i], i) {