Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Filter ¶
Filter returns elements where fn returns true.
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/slices"
)
func main() {
result := slices.Filter([]int{1, 2, 3, 4, 5}, func(n int) bool {
return n%2 == 0
})
fmt.Println(result)
}
Output: [2 4]
func GroupBy ¶
func GroupBy[T any, K comparable](items []T, keyFn func(T) K) map[K][]T
GroupBy groups elements by key.
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/slices"
)
func main() {
type item struct {
name string
category string
}
items := []item{
{"apple", "fruit"},
{"carrot", "vegetable"},
{"banana", "fruit"},
}
groups := slices.GroupBy(items, func(i item) string {
return i.category
})
fmt.Println(len(groups))
fmt.Println(len(groups["fruit"]))
fmt.Println(len(groups["vegetable"]))
}
Output: 2 2 1
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.