Skip to content

Commit

Permalink
Update foreachwhile readme.md (#508)
Browse files Browse the repository at this point in the history
* update readme.md for lo.ForEachWhile play and example code

---------

Co-authored-by: Samuel Berthe <[email protected]>
  • Loading branch information
Sianao and samber authored Aug 9, 2024
1 parent d8e5707 commit 1603a84
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 1603a84

Please sign in to comment.