filter

package
v2.0.0-beta.3 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 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"
	"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 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 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 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"
	"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