mirror of
https://github.com/samber/lo.git
synced 2026-04-22 23:47:11 +08:00
1.8 KiB
1.8 KiB
name, slug, sourceRef, category, subCategory, signatures, playUrl, variantHelpers, similarHelpers, position
| name | slug | sourceRef | category | subCategory | signatures | playUrl | variantHelpers | similarHelpers | position | ||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| FlatMap | flatmap | it/seq.go#L109 | it | sequence |
|
https://go.dev/play/p/1YsRLPl-wx |
|
|
120 |
Transforms and flattens a sequence to another type. Each element is transformed into a sequence, then all sequences are concatenated.
Examples:
seq := func(yield func([]int) bool) {
_ = yield([]int{1, 2})
_ = yield([]int{3, 4})
_ = yield([]int{5})
}
flattened := it.FlatMap(seq, func(arr []int) iter.Seq[int] {
return func(yield func(int) bool) {
for _, v := range arr {
if !yield(v * 2) {
return
}
}
}
})
var result []int
for v := range flattened {
result = append(result, v)
}
// result contains 2, 4, 6, 8, 10
FlatMapI
Transforms and flattens a sequence to another type. Each element is transformed into a sequence with access to the element's index, then all sequences are concatenated.
seq := func(yield func(string) bool) {
_ = yield("a")
_ = yield("b")
_ = yield("c")
}
flattened := it.FlatMapI(seq, func(s string, index int) iter.Seq[string] {
return func(yield func(string) bool) {
for i := 0; i <= index; i++ {
if !yield(fmt.Sprintf("%s-%d", s, i)) {
return
}
}
}
})
var result []string
for v := range flattened {
result = append(result, v)
}
// result contains "a-0", "b-0", "b-1", "c-0", "c-1", "c-2"