Skip to content

Commit 18f6945

Browse files
deansgGoldziher
andauthored
fix: update documentation for new sliceutils.FlatMap function (#44)
* fix: update documentation for new sliceutils.FlatMap function * Update docs/sliceutils/flatMap.md Co-authored-by: Na'aman Hirschfeld <nhirschfeld@gmail.com> --------- Co-authored-by: Na'aman Hirschfeld <nhirschfeld@gmail.com>
1 parent f2d157b commit 18f6945

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

docs/sliceutils/flatMap.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# FlatMap
2+
3+
`func FlatMap[T any, R any](slice []T, mapper func(value T, index int, slice []T) []R) []R`
4+
5+
FlatMap receives a slice of type T, executes the passed in slice-mapper function for each element in the slice, and
6+
returns a flattened slice containing all the elements from all the mapped slices.
7+
The function is passed the current element, the current index and the slice itself as function arguments.
8+
9+
10+
```go
11+
package main
12+
13+
import (
14+
"github.com/Goldziher/go-utils/sliceutils"
15+
)
16+
17+
func main() {
18+
items := []int{1, 2, 3, 4}
19+
20+
flatMapped := sliceutils.FlatMap(items, func(value int, index int, slice []int) []int {
21+
return []int{value, value * 2}
22+
}) // []int{1, 2, 2, 4, 3, 6, 4, 8}
23+
}
24+
```

0 commit comments

Comments
 (0)