Documentation
¶
Overview ¶
Package slice provides functional abstractions over go slices.
Index ¶
- func All[T any](in []T, matchFn MatchFn[T]) bool
- func Any[T any](in []T, matchFn MatchFn[T]) bool
- func FoldL[T any, A any](in []T, acc A, fn AccFn[T, A]) A
- func FoldR[T any, A any](in []T, acc A, fn AccFn[T, A]) A
- func Frequencies[T comparable](in []T) map[T]int
- func FrequenciesBy[T any, R comparable](in []T, keyFn KeyFn[T, R]) map[R]int
- func GroupBy[T any, R comparable](in []T, keyFn KeyFn[T, R]) map[R][]T
- func Map[T any, R any](in []T, fn MapFn[T, R]) []R
- func Take[T any](in []T, amount int) []T
- func TakeEvery[T any](in []T, n uint) []T
- func TakeWhile[T any](in []T, fn MatchFn[T]) []T
- type AccFn
- type KeyFn
- type MapFn
- type MatchFn
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶
All returns true if all elements in the input slice are true according to the provided matchFn.
Example ¶
package main
import (
"fmt"
"github.com/mikehelmick/go-functional/slice"
)
func main() {
// Function to check if an element is even
isEven := func(x int) bool {
return x%2 == 0
}
allEven := []int{2, 4, 6, 8, 10}
fmt.Printf("All(%v) == %v\n", allEven, slice.All(allEven, isEven))
withOds := []int{2, 4, 5, 6}
fmt.Printf("All(%v) == %v\n", withOds, slice.All(withOds, isEven))
}
Output: All([2 4 6 8 10]) == true All([2 4 5 6]) == false
func Any ¶
Any returns true if any of the elements in the input slice return true when the provided matchFn is called.
Example ¶
package main
import (
"fmt"
"github.com/mikehelmick/go-functional/slice"
)
func main() {
// Function to check if an element is seven
isSeven := func(x int) bool {
return x == 7
}
noSevens := []int{5, 6, 8}
fmt.Printf("All(%v) == %v\n", noSevens, slice.Any(noSevens, isSeven))
withSevens := []int{5, 6, 7, 8}
fmt.Printf("All(%v) == %v\n", withSevens, slice.Any(withSevens, isSeven))
}
Output: All([5 6 8]) == false All([5 6 7 8]) == true
func FoldL ¶
FoldL folds (reduces) the input slice from the left. Requires an initial accumulator and an accumulator function.
Example ¶
package main
import (
"fmt"
"github.com/mikehelmick/go-functional/slice"
)
func main() {
sumFn := func(x int, acc int) int {
return acc + x
}
in := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
sum := slice.FoldL(in, 0, sumFn)
fmt.Printf("sum(%v) == %v\n", in, sum)
}
Output: sum([1 2 3 4 5 6 7 8 9 10]) == 55
func FoldR ¶
FoldR folds (reduces) the input slice from the right. Requires an initial accumulator and an accumulator function.
Example ¶
package main
import (
"fmt"
"github.com/mikehelmick/go-functional/slice"
)
func main() {
subFn := func(x int, acc int) int {
return acc - x
}
in := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
sum := slice.FoldR(in, 20, subFn)
fmt.Printf("foldr_sub(%v) == %v\n", in, sum)
}
Output: foldr_sub([1 2 3 4 5 6 7 8 9 10]) == -35
func Frequencies ¶
func Frequencies[T comparable](in []T) map[T]int
Frequencies returns a map with a count of the number of time each element occurs in the input slice.
func FrequenciesBy ¶
func FrequenciesBy[T any, R comparable](in []T, keyFn KeyFn[T, R]) map[R]int
FrequenciesBy returns a map with a count of the number of times each key occurred in the input sequence. The KeyFn provided can manipulate the input elements in any way.
func GroupBy ¶
func GroupBy[T any, R comparable](in []T, keyFn KeyFn[T, R]) map[R][]T
GroupBy groups the input slice according to the key function.
func Map ¶
Map takes in a slice of type T, and a map function that can turn elements of type T into elements of type R. A new slice of type R is returned, withe the map function applied to each element in the input.
Example ¶
package main
import (
"fmt"
"github.com/mikehelmick/go-functional/slice"
)
type Person struct {
Name string
Age int
}
func (p Person) String() string {
return fmt.Sprintf("%s: %v", p.Name, p.Age)
}
func main() {
redactAge := func(p Person) Person {
return Person{
Name: p.Name,
}
}
people := []Person{
{Name: "Jim", Age: 28},
{Name: "Dwight", Age: 32},
}
redacted := slice.Map(people, redactAge)
fmt.Printf("%+v\n", people)
fmt.Printf("%+v\n", redacted)
}
Output: [Jim: 28 Dwight: 32] [Jim: 0 Dwight: 0]
func Take ¶
Take returns an amount of elements from the beginning of a slice. If the amount provides is negative, it returns that amount of elements from the end of the slice. If the amount is zero, an empty slice is returned.
func TakeEvery ¶
TakeEvery returns a new slice with every nth element from the original slice. The first element is always included unless n is 0.
Example ¶
package main
import (
"fmt"
"github.com/mikehelmick/go-functional/slice"
)
func main() {
in := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
takeTwo := slice.TakeEvery(in, 2)
fmt.Printf("Take 2: %+v\n", takeTwo)
takeZero := slice.TakeEvery(in, 0)
fmt.Printf("Take 0: %+v\n", takeZero)
takeAll := slice.TakeEvery(in, 27)
fmt.Printf("Take all: %+v\n", takeAll)
}
Output: Take 2: [1 3 5 7 9] Take 0: [] Take all: [1]
func TakeWhile ¶
TakeWhile returns elements from the beginning of the input slice as long as the provided MatchFn returns true.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/mikehelmick/go-functional/slice"
)
func main() {
in := []string{"dip", "drive", "dodge", "swerve"}
dPrefix := slice.TakeWhile(in, func(s string) bool {
return strings.HasPrefix(s, "d")
})
fmt.Printf("dPrefix: %+v\n", dPrefix)
diPrefix := slice.TakeWhile(in, func(s string) bool {
return strings.HasPrefix(s, "di")
})
fmt.Printf("diPrefix: %+v\n", diPrefix)
sPrefix := slice.TakeWhile(in, func(s string) bool {
return strings.HasPrefix(s, "s")
})
fmt.Printf("sPrefix: %+v\n", sPrefix)
all := slice.TakeWhile(in, func(s string) bool { return true })
fmt.Printf("all: %+v\n", all)
}
Output: dPrefix: [dip drive dodge] diPrefix: [dip] sPrefix: [] all: [dip drive dodge swerve]
Types ¶
type KeyFn ¶
type KeyFn[T any, R comparable] func(T) R
KeyFn determines how the input value should be used when determining frequencies.