Documentation
¶
Overview ¶
This package contains functions intended for use with iter.Filter.
Index ¶
- func And[T any](filters ...func(T) bool) func(T) bool
- func Contains[T string | []byte](t T) func(T) bool
- func GreaterThan[T cmp.Ordered](threshold T) func(T) bool
- func IsEqual[T comparable](value T) func(T) bool
- func IsEven[...](integer T) bool
- func IsOdd[...](integer T) bool
- func IsZero[T comparable](value T) bool
- func LessThan[T cmp.Ordered](threshold T) func(T) bool
- func Match[T string | []byte](pattern *regexp.Regexp) func(T) bool
- func Not[T any](fn func(T) bool) func(T) bool
- func NotEqual[T comparable](value T) func(T) bool
- func Or[T any](filters ...func(T) bool) func(T) bool
- func Passthrough[V any](value V) bool
- func Passthrough2[V, W any](v V, w W) bool
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func And ¶
And returns a function that returns true when all provided functions return true.
Example ¶
package main import ( "fmt" "slices" "github.com/BooleanCat/go-functional/v2/it" "github.com/BooleanCat/go-functional/v2/it/filter" ) func main() { numbers := slices.Values([]int{1, 2, 3, 4}) for number := range it.Filter(numbers, filter.And(filter.IsOdd, filter.GreaterThan(2))) { fmt.Println(number) } }
Output: 3
func Contains ¶
Contains returns a function that returns true when the provided string or byte slice is found within another string or byte slice. See strings.Contains and bytes.Contains.
Example (Bytes) ¶
package main import ( "fmt" "slices" "github.com/BooleanCat/go-functional/v2/it" "github.com/BooleanCat/go-functional/v2/it/filter" ) func main() { strings := slices.Values([][]byte{[]byte("foobar"), []byte("barfoo")}) for element := range it.Filter(strings, filter.Contains([]byte("rfoo"))) { fmt.Println(string(element)) } }
Output: barfoo
Example (String) ¶
package main import ( "fmt" "slices" "github.com/BooleanCat/go-functional/v2/it" "github.com/BooleanCat/go-functional/v2/it/filter" ) func main() { strings := slices.Values([]string{"foobar", "barfoo"}) for element := range it.Filter(strings, filter.Contains("rfoo")) { fmt.Println(element) } }
Output: barfoo
func GreaterThan ¶
GreaterThan returns a function that returns true when the provided value is greater than a threshold.
Example ¶
package main import ( "fmt" "slices" "github.com/BooleanCat/go-functional/v2/it" "github.com/BooleanCat/go-functional/v2/it/filter" ) func main() { for number := range it.Filter(slices.Values([]int{1, 2, 3, 4}), filter.GreaterThan(2)) { fmt.Println(number) } }
Output: 3 4
func IsEqual ¶
func IsEqual[T comparable](value T) func(T) bool
IsEqual returns a function that returns true when the provided value is equal to some value.
Example ¶
package main import ( "fmt" "slices" "github.com/BooleanCat/go-functional/v2/it" "github.com/BooleanCat/go-functional/v2/it/filter" ) func main() { for number := range it.Filter(slices.Values([]int{1, 2, 3, 4}), filter.IsEqual(2)) { fmt.Println(number) } }
Output: 2
func IsEven ¶
func IsEven[T ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~int | ~int8 | ~int16 | ~int32 | ~int64](integer T) bool
IsEven returns true when the provided integer is even.
Example ¶
package main import ( "fmt" "slices" "github.com/BooleanCat/go-functional/v2/it" "github.com/BooleanCat/go-functional/v2/it/filter" ) func main() { for number := range it.Filter(slices.Values([]int{1, 2, 3, 4}), filter.IsEven) { fmt.Println(number) } }
Output: 2 4
func IsOdd ¶
func IsOdd[T ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~int | ~int8 | ~int16 | ~int32 | ~int64](integer T) bool
IsOdd returns true when the provided integer is odd.
Example ¶
package main import ( "fmt" "slices" "github.com/BooleanCat/go-functional/v2/it" "github.com/BooleanCat/go-functional/v2/it/filter" ) func main() { for number := range it.Filter(slices.Values([]int{1, 2, 3, 4}), filter.IsOdd) { fmt.Println(number) } }
Output: 1 3
func IsZero ¶
func IsZero[T comparable](value T) bool
IsZero returns true when the provided value is the zero value for its type.
Example ¶
package main import ( "fmt" "slices" "github.com/BooleanCat/go-functional/v2/it" "github.com/BooleanCat/go-functional/v2/it/filter" ) func main() { for number := range it.Filter(slices.Values([]int{0, 1, 2, 3}), filter.IsZero) { fmt.Println(number) } }
Output: 0
func LessThan ¶
LessThan returns a function that returns true when the provided value is less than a threshold.
Example ¶
package main import ( "fmt" "slices" "github.com/BooleanCat/go-functional/v2/it" "github.com/BooleanCat/go-functional/v2/it/filter" ) func main() { for number := range it.Filter(slices.Values([]int{1, 2, 3, 4}), filter.LessThan(3)) { fmt.Println(number) } }
Output: 1 2
func Match ¶
Match returns a function that returns true when the provided string or byte slice matches a pattern. See regexp.MatchString.
Example (Bytes) ¶
package main import ( "fmt" "regexp" "slices" "github.com/BooleanCat/go-functional/v2/it" "github.com/BooleanCat/go-functional/v2/it/filter" ) func main() { pattern := regexp.MustCompile(`^foo`) strings := slices.Values([][]byte{[]byte("foobar"), []byte("barfoo")}) for match := range it.Filter(strings, filter.Match[[]byte](pattern)) { fmt.Println(string(match)) } }
Output: foobar
Example (String) ¶
package main import ( "fmt" "regexp" "slices" "github.com/BooleanCat/go-functional/v2/it" "github.com/BooleanCat/go-functional/v2/it/filter" ) func main() { pattern := regexp.MustCompile(`^foo`) strings := slices.Values([]string{"foobar", "barfoo"}) for match := range it.Filter(strings, filter.Match[string](pattern)) { fmt.Println(match) } }
Output: foobar
func Not ¶
Not returns a function that inverts the result of the provided function.
Example ¶
package main import ( "fmt" "slices" "github.com/BooleanCat/go-functional/v2/it" "github.com/BooleanCat/go-functional/v2/it/filter" ) func main() { numbers := slices.Values([]int{1, 2, 3, 4}) for number := range it.Filter(numbers, filter.Not[int](filter.IsEven)) { fmt.Println(number) } }
Output: 1 3
func NotEqual ¶
func NotEqual[T comparable](value T) func(T) bool
NotEqual returns a function that returns true when the provided value is not equal to some value.
Example ¶
package main import ( "fmt" "slices" "github.com/BooleanCat/go-functional/v2/it" "github.com/BooleanCat/go-functional/v2/it/filter" ) func main() { for number := range it.Filter(slices.Values([]int{1, 2, 3, 4}), filter.NotEqual(2)) { fmt.Println(number) } }
Output: 1 3 4
func Or ¶
Or returns a function that returns true when any of the provided functions return true.
Example ¶
package main import ( "fmt" "slices" "github.com/BooleanCat/go-functional/v2/it" "github.com/BooleanCat/go-functional/v2/it/filter" ) func main() { numbers := slices.Values([]int{1, 2, 3, 4}) for number := range it.Filter(numbers, filter.Or(filter.IsEven, filter.LessThan(3))) { fmt.Println(number) } }
Output: 1 2 4
func Passthrough ¶
Passthrough returns a function that returns true for any value.
Example ¶
package main import ( "fmt" "slices" "github.com/BooleanCat/go-functional/v2/it" "github.com/BooleanCat/go-functional/v2/it/filter" ) func main() { for number := range it.Filter(slices.Values([]int{1, 2, 3}), filter.Passthrough) { fmt.Println(number) } }
Output: 1 2 3
func Passthrough2 ¶
Passthrough2 returns a function that returns true for any pair of values.
Example ¶
package main import ( "fmt" "maps" "github.com/BooleanCat/go-functional/v2/it" "github.com/BooleanCat/go-functional/v2/it/filter" ) func main() { for key, value := range it.Filter2(maps.All(map[int]string{1: "two"}), filter.Passthrough2) { fmt.Println(key, value) } }
Output: 1 two
Types ¶
This section is empty.