Documentation ¶
Index ¶
- func All[S ~[]E, E any](s S, fn func(E) bool) bool
- func Any[S ~[]E, E any](s S, fn func(E) bool) bool
- func Count[S ~[]E, E any](s S, fn func(E) bool) int
- func Diff[S ~[]E, E comparable](a S, b S) S
- func Every[S ~[]E, E any](s S, fn func(E) bool) bool
- func Find[S ~[]E, E any](s S, offset int, f func(E) bool) (E, int)
- func GroupBy[S ~[]E, E any, T comparable](s S, fn func(E) T) map[T]S
- func Intersect[S ~[]E, E comparable](a S, b S) S
- func Map[S ~[]E, E any, T any](s S, fn func(E) T) []T
- func None[S ~[]E, E any](s S, f func(E) bool) bool
- func Overlap[S ~[]E, E comparable](a S, b S) bool
- func OverlapFunc[S ~[]E, E any](a S, b S, fn func(E, E) bool) bool
- func Reduce[S ~[]E, E any](s S, start E, f func(acc E, curr E) E) E
- func Reject[S ~[]E, E any](s S, f func(E) bool) S
- func Remove[S ~[]E, E comparable](s S, e ...E) S
- func Select[S ~[]E, E any](s S, f func(E) bool) S
- func Some[S ~[]E, E any](s S, fn func(E) bool) bool
- func SplitBy[S ~[]E, E any](s S, f func(E) bool) (S, S)
- func Union[S ~[]E, E comparable](s ...S) S
- func Unique[S ~[]E, E comparable](s S) S
- func UniqueFunc[S ~[]E, E any](s S, cmp func(a, b E) int) S
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶
All checks if every entry in the slice match the predicate.
Example ¶
package main import ( "fmt" "github.com/christopher-kleine/slt" "github.com/christopher-kleine/slt/numbers" ) func main() { input := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} fmt.Println(slt.All(input, numbers.Even)) fmt.Println(slt.All(input, numbers.Below(10, true))) }
Output: false true
func Any ¶
Any checks if there is at least one entry that match the predicate.
Example ¶
package main import ( "fmt" "github.com/christopher-kleine/slt" "github.com/christopher-kleine/slt/numbers" ) func main() { input := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} fmt.Println(slt.Any(input, numbers.Even)) fmt.Println(slt.All(input, numbers.Above(10, true))) }
Output: true false
func Count ¶
Count counts the entries that match the predicate
Example ¶
package main import ( "fmt" "github.com/christopher-kleine/slt" "github.com/christopher-kleine/slt/numbers" ) func main() { input := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} fmt.Println(slt.Count(input, numbers.Even)) fmt.Println(slt.Count(input, numbers.Above(8, true))) }
Output: 5 2
func Diff ¶
func Diff[S ~[]E, E comparable](a S, b S) S
Diff returns the elements that are only in one of the slices.
Example ¶
package main import ( "fmt" "github.com/christopher-kleine/slt" ) func main() { sliceA := []string{"A", "B", "C", "D"} sliceB := []string{"A", "G", "B", "F"} diff := slt.Diff(sliceA, sliceB) fmt.Println(diff) }
Output: [C D G F]
func Find ¶
Find returns the first element that match the predicate. It also returns the index. In case no element matches, an empty value and -1 are returned.
If the offset is less than zero, it defaults to zero.
Example ¶
package main import ( "fmt" "strings" "github.com/christopher-kleine/slt" ) func main() { input := []string{"Anna", "Berta", "chris"} isLowerCase := func(s string) bool { return strings.ToLower(s) == s } lowerCaseEntry, index := slt.Find(input, 0, isLowerCase) if index > -1 { fmt.Println("Found the lower case word entry:", lowerCaseEntry) } else { fmt.Println("No word is purely lowercase") } input = []string{"Anna", "Berta", "Chris"} lowerCaseEntry, index = slt.Find(input, 0, isLowerCase) if index > -1 { fmt.Println("Found the lower case word entry:", lowerCaseEntry) } else { fmt.Println("No word is purely lowercase") } }
Output: Found the lower case word entry: chris No word is purely lowercase
func GroupBy ¶
func GroupBy[S ~[]E, E any, T comparable](s S, fn func(E) T) map[T]S
GroupBy uses the callback to group the provided slice into a map of slices.
Example ¶
package main import ( "fmt" "github.com/christopher-kleine/slt" ) func main() { input := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} toWord := func(v int) string { if v%2 == 0 { return "Even" } else { return "Odd" } } fmt.Println(slt.GroupBy(input, toWord)) }
Output: map[Even:[0 2 4 6 8] Odd:[1 3 5 7 9]]
func Intersect ¶
func Intersect[S ~[]E, E comparable](a S, b S) S
Intersect returns the elements that are in both slices.
Example ¶
package main import ( "fmt" "github.com/christopher-kleine/slt" ) func main() { sliceA := []string{"A", "B", "C", "D"} sliceB := []string{"A", "G", "B", "F"} diff := slt.Intersect(sliceA, sliceB) fmt.Println(diff) }
Output: [A B]
func Map ¶
Map applies a transform function on all entries in a slice and returns the new slice.
Example ¶
package main import ( "fmt" "github.com/christopher-kleine/slt" ) func main() { input := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} toWord := func(v int) string { if v%2 == 0 { return "Even" } else { return "Odd" } } fmt.Println(slt.Map(input, toWord)) }
Output: [Even Odd Even Odd Even Odd Even Odd Even Odd]
func None ¶
None checks if no entry that match the predicate. It's the opposite of Any/Some.
Example ¶
package main import ( "fmt" "github.com/christopher-kleine/slt" "github.com/christopher-kleine/slt/numbers" ) func main() { input := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} fmt.Println(slt.None(input, numbers.Even)) fmt.Println(slt.None(input, numbers.Below(10, true))) }
Output: false false
func Overlap ¶
func Overlap[S ~[]E, E comparable](a S, b S) bool
Overlap checks if any value in slice A is also found in slice B.
Example ¶
package main import ( "fmt" "github.com/christopher-kleine/slt" ) func main() { ListA := []int{1, 2, 3} ListB := []int{1, 5, 7} ListC := []int{5, 7, 9} fmt.Println(slt.Overlap(ListA, ListB)) fmt.Println(slt.Overlap(ListA, ListC)) }
Output: true false
func OverlapFunc ¶
OverlapFunc checks if any value in slice A is also found in slice B.
func Reduce ¶
func Reduce[S ~[]E, E any](s S, start E, f func(acc E, curr E) E) E
Reduce reduces a collection using the function fn
Example ¶
package main import ( "fmt" "github.com/christopher-kleine/slt" ) func main() { numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} add := func(acc int, curr int) int { return acc + curr } fmt.Println("Sum of all numbers between 1 and 10 is:", slt.Reduce(numbers, 0, add)) }
Output: Sum of all numbers between 1 and 10 is: 55
func Reject ¶
Reject returns a new slice, removing all elements using the predicate.
Example ¶
package main import ( "fmt" "github.com/christopher-kleine/slt" "github.com/christopher-kleine/slt/numbers" ) func main() { input := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} fmt.Println("Even Numbers:", slt.Reject(input, numbers.Even)) }
Output: Even Numbers: [1 3 5 7 9]
func Remove ¶
func Remove[S ~[]E, E comparable](s S, e ...E) S
Remove values from a slice.
Example ¶
package main import ( "fmt" "github.com/christopher-kleine/slt" ) func main() { input := []string{"H", "E", "L", "L", "O", "W", "O", "R", "L", "D"} removed := slt.Remove(input, "A", "E", "I", "O", "U") fmt.Println(removed) }
Output: [H L L W R L D]
func Select ¶
Select filters the given slice by using the predicate.
Example ¶
package main import ( "fmt" "github.com/christopher-kleine/slt" "github.com/christopher-kleine/slt/numbers" ) func main() { input := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} fmt.Println("Even Numbers:", slt.Select(input, numbers.Even)) }
Output: Even Numbers: [0 2 4 6 8]
func SplitBy ¶
SplitBy splits the slice into 2 parts and returns them.
The first slice is the result of every entry that match the predicate.
The second slice is the result of every entry that failed the predicate.
Example ¶
package main import ( "fmt" "github.com/christopher-kleine/slt" "github.com/christopher-kleine/slt/numbers" ) func main() { input := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} even, odd := slt.SplitBy(input, numbers.Even) fmt.Println(even) fmt.Println(odd) }
Output: [0 2 4 6 8] [1 3 5 7 9]
func Union ¶
func Union[S ~[]E, E comparable](s ...S) S
Union takes a number of slices and combines them into one. The difference between Union and the built-in function append lies in the removal of duplicates.
Example ¶
package main import ( "fmt" "github.com/christopher-kleine/slt" ) func main() { a := []string{"A", "B", "C", "D", "H"} b := []string{"A", "C", "E", "F", "G"} c := []string{"F", "G", "I", "J", "K"} d := slt.Union(a, b, c) fmt.Println(d) }
Output: [A B C D H E F G I J K]
func Unique ¶
func Unique[S ~[]E, E comparable](s S) S
Unique makes sure every entry only appears once. It won't change the order of entries.
Example ¶
package main import ( "fmt" "github.com/christopher-kleine/slt" ) func main() { input := []int{0, 1, 2, 3, 4, 5, 6, 5, 1} fmt.Println(slt.Unique(input)) }
Output: [0 1 2 3 4 5 6]
func UniqueFunc ¶
UniqueFunc makes sure every entry only appears once. Unlike Unique this changes the order of entries.
Types ¶
This section is empty.