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, variantHelpers, similarHelpers, position
| name | slug | sourceRef | category | subCategory | signatures | variantHelpers | similarHelpers | position | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| FilterMap | filtermap | it/seq.go#L86 | it | sequence |
|
|
|
40 |
Maps elements of a sequence to new values and filters out elements where the callback returns false. Only elements where the second return value is true are included in the result.
seq := func(yield func(int) bool) {
yield(1)
yield(2)
yield(3)
yield(4)
}
result := it.FilterMap(seq, func(x int) (string, bool) {
if x%2 == 0 {
return fmt.Sprintf("even-%d", x), true
}
return "", false
})
// iter.Seq[string] yielding "even-2", "even-4"
seq = func(yield func(string) bool) {
yield("a")
yield("")
yield("c")
yield("d")
}
result = it.FilterMap(seq, func(s string) (int, bool) {
if s != "" {
return len(s), true
}
return 0, false
})
// iter.Seq[int] yielding 1, 1, 1 (length of "a", "c", "d")
FilterMapI
Maps elements of a sequence to new values and filters out elements where the callback returns false. The callback receives both the item and its index.
seq := func(yield func(string) bool) {
yield("apple")
yield("banana")
yield("cherry")
}
result := it.FilterMapI(seq, func(s string, index int) (string, bool) {
if index%2 == 0 {
return fmt.Sprintf("%s-%d", s, index), true
}
return "", false
})
// iter.Seq[string] yielding "apple-0", "cherry-2"