filter

package
v2.0.0-alpha.3 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2024 License: MIT Imports: 1 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"

	"github.com/BooleanCat/go-functional/v2/future/slices"
	"github.com/BooleanCat/go-functional/v2/iter"
	"github.com/BooleanCat/go-functional/v2/iter/filter"
)

func main() {
	fmt.Println(iter.Iterator[int](slices.Values([]int{1, 2, 3, 4})).Filter(filter.And(filter.IsOdd, filter.GreaterThan(2))).Collect())
}
Output:

[3]

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"

	"github.com/BooleanCat/go-functional/v2/future/slices"
	"github.com/BooleanCat/go-functional/v2/iter"
	"github.com/BooleanCat/go-functional/v2/iter/filter"
)

func main() {
	fmt.Println(iter.Iterator[int](slices.Values([]int{1, 2, 3, 4})).Filter(filter.GreaterThan(2)).Collect())
}
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"

	"github.com/BooleanCat/go-functional/v2/future/slices"
	"github.com/BooleanCat/go-functional/v2/iter"
	"github.com/BooleanCat/go-functional/v2/iter/filter"
)

func main() {
	fmt.Println(iter.Iterator[int](slices.Values([]int{1, 2, 3, 4})).Filter(filter.IsEqual(2)).Collect())
}
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"

	"github.com/BooleanCat/go-functional/v2/future/slices"
	"github.com/BooleanCat/go-functional/v2/iter"
	"github.com/BooleanCat/go-functional/v2/iter/filter"
)

func main() {
	fmt.Println(iter.Iterator[int](slices.Values([]int{1, 2, 3, 4})).Filter(filter.IsEven).Collect())
}
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"

	"github.com/BooleanCat/go-functional/v2/future/slices"
	"github.com/BooleanCat/go-functional/v2/iter"
	"github.com/BooleanCat/go-functional/v2/iter/filter"
)

func main() {
	fmt.Println(iter.Iterator[int](slices.Values([]int{1, 2, 3, 4})).Filter(filter.IsOdd).Collect())
}
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"

	"github.com/BooleanCat/go-functional/v2/future/slices"
	"github.com/BooleanCat/go-functional/v2/iter"
	"github.com/BooleanCat/go-functional/v2/iter/filter"
)

func main() {
	fmt.Println(iter.Iterator[int](slices.Values([]int{0, 1, 2, 3})).Filter(filter.IsZero).Collect())
}
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"

	"github.com/BooleanCat/go-functional/v2/future/slices"
	"github.com/BooleanCat/go-functional/v2/iter"
	"github.com/BooleanCat/go-functional/v2/iter/filter"
)

func main() {
	fmt.Println(iter.Iterator[int](slices.Values([]int{1, 2, 3, 4})).Filter(filter.LessThan(3)).Collect())
}
Output:

[1 2]

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"

	"github.com/BooleanCat/go-functional/v2/future/slices"
	"github.com/BooleanCat/go-functional/v2/iter"
	"github.com/BooleanCat/go-functional/v2/iter/filter"
)

func main() {
	fmt.Println(iter.Iterator[int](slices.Values([]int{1, 2, 3, 4})).Filter(filter.Not[int](filter.IsEven)).Collect())
}
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"

	"github.com/BooleanCat/go-functional/v2/future/slices"
	"github.com/BooleanCat/go-functional/v2/iter"
	"github.com/BooleanCat/go-functional/v2/iter/filter"
)

func main() {
	fmt.Println(iter.Iterator[int](slices.Values([]int{1, 2, 3, 4})).Filter(filter.NotEqual(2)).Collect())
}
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"

	"github.com/BooleanCat/go-functional/v2/future/slices"
	"github.com/BooleanCat/go-functional/v2/iter"
	"github.com/BooleanCat/go-functional/v2/iter/filter"
)

func main() {
	fmt.Println(iter.Iterator[int](slices.Values([]int{1, 2, 3, 4})).Filter(filter.Or(filter.IsEven, filter.LessThan(3))).Collect())
}
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"

	"github.com/BooleanCat/go-functional/v2/future/slices"
	"github.com/BooleanCat/go-functional/v2/iter"
	"github.com/BooleanCat/go-functional/v2/iter/filter"
)

func main() {
	fmt.Println(iter.Iterator[int](slices.Values([]int{1, 2, 3, 4})).Filter(filter.Passthrough).Collect())
}
Output:

[1 2 3 4]

func Passthrough2

func Passthrough2[V any, W any](value1 V, value2 W) bool

Passthrough2 returns a function that returns true for any pair of values.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/v2/future/maps"
	"github.com/BooleanCat/go-functional/v2/iter"
	"github.com/BooleanCat/go-functional/v2/iter/filter"
)

func main() {
	numbers := maps.Collect(iter.Filter2(maps.All(map[int]string{1: "two"}), filter.Passthrough2))

	fmt.Println(numbers[1])
}
Output:

two

Types

This section is empty.

Jump to

Keyboard shortcuts

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