filters

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2024 License: MIT Imports: 2 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.

When no functions are provided the result is always true.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
	"github.com/BooleanCat/go-functional/iter/filters"
)

func main() {
	numbers := iter.Lift([]int{1, 2, 3, 4, 5, 6, 7}).Filter(filters.And(
		filters.GreaterThan(2),
		filters.LessThan(7),
	))
	fmt.Println(numbers.Collect())
}
Output:

[3 4 5 6]

func GreaterThan

func GreaterThan[T constraints.Ordered](t T) func(T) bool

GreaterThan returns a function that returns true when a value is greater than a threshold.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
	"github.com/BooleanCat/go-functional/iter/filters"
)

func main() {
	numbers := iter.Lift([]int{1, 2, 3, 4, 5, 1}).Filter(filters.GreaterThan(2))
	fmt.Println(numbers.Collect())
}
Output:

[3 4 5]

func GreaterThanEqual added in v0.16.0

func GreaterThanEqual[T constraints.Ordered](t T) func(T) bool

GreaterThanEqual returns a function that returns true when a value is greater than or equal to a threshold.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
	"github.com/BooleanCat/go-functional/iter/filters"
)

func main() {
	numbers := iter.Lift([]int{1, 2, 3, 4, 5}).Filter(filters.GreaterThanEqual(3))
	fmt.Println(numbers.Collect())
}
Output:

[3 4 5]

func IsEven added in v0.14.0

func IsEven[T constraints.Integer](integer T) bool

IsEven returns true when the provided integer is even

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
	"github.com/BooleanCat/go-functional/iter/filters"
)

func main() {
	numbers := iter.Lift([]int{1, 2, 3, 4}).Filter(filters.IsEven[int])
	fmt.Println(numbers.Collect())
}
Output:

[2 4]

func IsOdd added in v0.14.0

func IsOdd[T constraints.Integer](integer T) bool

IsOdd returns true when the provided integer is odd

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
	"github.com/BooleanCat/go-functional/iter/filters"
)

func main() {
	numbers := iter.Lift([]int{1, 2, 3, 4}).Filter(filters.IsOdd[int])
	fmt.Println(numbers.Collect())
}
Output:

[1 3]

func IsZero

func IsZero[T comparable](t T) bool

IsZero returns true when the provided value is equal to its zero value.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
	"github.com/BooleanCat/go-functional/iter/filters"
)

func main() {
	numbers := iter.Exclude[int](iter.Lift([]int{1, 2, 3, 0, 4}), filters.IsZero[int])
	fmt.Println(numbers.Collect())
}
Output:

[1 2 3 4]

func LessThan

func LessThan[T constraints.Ordered](t T) func(T) bool

LessThan returns a function that returns true when a value is less than a threshold.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
	"github.com/BooleanCat/go-functional/iter/filters"
)

func main() {
	numbers := iter.Lift([]int{1, 2, 3, 4, 5, 1}).Filter(filters.LessThan(2))
	fmt.Println(numbers.Collect())
}
Output:

[1 1]

func LessThanEqual added in v0.16.0

func LessThanEqual[T constraints.Ordered](t T) func(T) bool

LessThanEqual returns a function that returns true when a value is less than or equal to a threshold.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
	"github.com/BooleanCat/go-functional/iter/filters"
)

func main() {
	numbers := iter.Lift([]int{1, 2, 3, 4, 5}).Filter(filters.LessThanEqual(3))
	fmt.Println(numbers.Collect())
}
Output:

[1 2 3]

func Or added in v0.12.0

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

Or returns a function that returns true when one of the provided functions returns true.

When no functions are provided the result is always true.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/iter"
	"github.com/BooleanCat/go-functional/iter/filters"
)

func main() {
	numbers := iter.Lift([]int{1, 2, 3, 4, 5, 6, 7}).Filter(filters.Or(
		filters.LessThan(3),
		filters.GreaterThan(6),
	))

	fmt.Println(numbers.Collect())
}
Output:

[1 2 7]

Types

This section is empty.

Jump to

Keyboard shortcuts

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