filter

package
v2.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 1, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

This package contains functions intended for use with iter.Filter.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func And

func And[T any](filters ...func(T) bool) func(T) bool

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

func Contains[T string | []byte](t T) func(T) bool

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

func GreaterThan[T cmp.Ordered](threshold T) func(T) bool

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

func LessThan[T cmp.Ordered](threshold T) func(T) bool

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

func Match[T string | []byte](pattern *regexp.Regexp) func(T) bool

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

func Not[T any](fn func(T) bool) func(T) bool

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

func Or[T any](filters ...func(T) bool) func(T) bool

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

func Passthrough[V any](value V) bool

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

func Passthrough2[V, W any](v V, w W) bool

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL