Documentation ¶
Overview ¶
Predicates is a package that provides a set of predicate functions that can be used in conjunction of the itertools package to filter and transform iterables.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" "github.com/theMagicalKarp/iter/pkg/predicates" ) func main() { items := itertools.Filter(iter.New(1, 2, 3, 4, 5), predicates.Even[int]) itertools.Print(items) }
Output: 2, 4
Index ¶
- func Even[T constraints.Integer](t T) bool
- func False[T any](_ T) bool
- func Has[T any, V comparable](this V) func(tuple.Tuple[T, V]) bool
- func HasError[T any](this error) func(tuple.Tuple[T, error]) bool
- func IsDir(path string) bool
- func IsError(this error) func(error) bool
- func IsFile(path string) bool
- func Negative[T constraints.Signed](t T) bool
- func Odd[T constraints.Integer](t T) bool
- func PathExists(path string) bool
- func Positive[T constraints.Signed](t T) bool
- func True[T any](_ T) bool
- type Predicate
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Even ¶
func Even[T constraints.Integer](t T) bool
Even checks if the given integer is even. It returns true if the integer is even, and false otherwise.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" "github.com/theMagicalKarp/iter/pkg/predicates" ) func main() { items := itertools.Filter(iter.New(1, 2, 3, 4, 5), predicates.Even[int]) itertools.Print(items) }
Output: 2, 4
func False ¶
False returns false for any input value.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" "github.com/theMagicalKarp/iter/pkg/predicates" ) func main() { items := itertools.Filter(iter.New(1, 2, 3, 4, 5), predicates.False[int]) itertools.Print(items) }
Output:
func Has ¶
func Has[T any, V comparable](this V) func(tuple.Tuple[T, V]) bool
Has returns a predicate function that checks if the second element of a tuple is equal to the given value. The function takes a value of type V and returns a function that takes a tuple of type Tuple[T, V] and returns a boolean. The function compares the second element of the tuple with the given value and returns true if they are equal, false otherwise.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/containers/tuple" "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" "github.com/theMagicalKarp/iter/pkg/predicates" ) func main() { in := iter.New( tuple.New(1, "a"), tuple.New(2, "b"), tuple.New(3, "c"), tuple.New(4, "d"), tuple.New(5, "e"), ) items := itertools.Filter(in, predicates.Has[int, string]("c")) itertools.Print(items) }
Output: {3 c}
func HasError ¶
HasError returns a predicate function that checks if the given error matches the error in a tuple. It takes an error as input and returns a function that takes a tuple of type `containers.Tuple[T, error]` as input and returns a boolean value. The returned function checks if the error in the tuple matches the input error using the `errors.Is` function.
Example ¶
package main import ( "errors" "github.com/theMagicalKarp/iter/pkg/containers/tuple" "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" "github.com/theMagicalKarp/iter/pkg/predicates" ) func main() { err1 := errors.New("error 1") err2 := errors.New("error 2") err3 := errors.New("error 3") in := iter.New( tuple.New(1, err1), tuple.New(2, err2), tuple.New(3, err3), ) items := itertools.Filter(in, predicates.HasError[int](err1)) values := itertools.Map(items, func(t tuple.Tuple[int, error]) int { return t.First() }) itertools.Print(values) }
Output: 1
func IsDir ¶
IsDir checks if the given path is a directory. It returns true if the path is a directory, false otherwise.
func IsError ¶
IsError returns a function that checks if the given error matches a specific error. It uses the errors.Is function to perform the comparison.
Example ¶
package main import ( "errors" "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" "github.com/theMagicalKarp/iter/pkg/predicates" ) func main() { err1 := errors.New("error 1") err2 := errors.New("error 2") err3 := errors.New("error 3") items := itertools.Filter(iter.New(err1, err2, err3), predicates.IsError(err1)) itertools.Print(items) }
Output: error 1
func IsFile ¶
IsFile checks if the given path is a file. It returns true if the path is a file, false otherwise.
func Negative ¶
func Negative[T constraints.Signed](t T) bool
Negative returns true if the given value is negative, otherwise false.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" "github.com/theMagicalKarp/iter/pkg/predicates" ) func main() { items := itertools.Filter(iter.New(1, -1, 0, -4, 5), predicates.Negative[int]) itertools.Print(items) }
Output: -1, -4
func Odd ¶
func Odd[T constraints.Integer](t T) bool
Odd returns true if the given integer is odd, false otherwise.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" "github.com/theMagicalKarp/iter/pkg/predicates" ) func main() { items := itertools.Filter(iter.New(1, 2, 3, 4, 5), predicates.Odd[int]) itertools.Print(items) }
Output: 1, 3, 5
func PathExists ¶
PathExists checks if the given path exists. It returns true if the path exists, and false otherwise.
func Positive ¶
func Positive[T constraints.Signed](t T) bool
Positive checks if the given value is positive. It returns true if the value is greater than zero, otherwise false.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" "github.com/theMagicalKarp/iter/pkg/predicates" ) func main() { items := itertools.Filter(iter.New(1, -1, 0, -4, 5), predicates.Positive[int]) itertools.Print(items) }
Output: 1, 5
func True ¶
True returns true for any input value.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" "github.com/theMagicalKarp/iter/pkg/predicates" ) func main() { items := itertools.Filter(iter.New(1, 2, 3, 4, 5), predicates.True[int]) itertools.Print(items) }
Output: 1, 2, 3, 4, 5
Types ¶
type Predicate ¶
Predicate is a function type that represents a predicate. It takes a value of type T and returns a boolean value indicating whether the value satisfies the predicate.
func Contains ¶
Contains returns a Predicate function that checks if a given string contains a specified substring.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" "github.com/theMagicalKarp/iter/pkg/predicates" ) func main() { items := iter.New("hello", "world", "foo", "bar") items = itertools.Filter(items, predicates.Contains("o")) itertools.Print(items) }
Output: hello, world, foo
func Divisible ¶
func Divisible[T constraints.Integer](divisor T) Predicate[T]
Divisible returns a predicate function that checks if a given number is divisible by the specified divisor. The divisor must be an integer type.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" "github.com/theMagicalKarp/iter/pkg/predicates" ) func main() { items := itertools.Filter(iter.New(1, 2, 3, 4, 5), predicates.Divisible(2)) itertools.Print(items) }
Output: 2, 4
func HasPrefix ¶
HasPrefix returns a predicate function that checks if a string has the specified prefix. It takes a prefix string as input and returns a Predicate[string] function. The returned function checks if the input string starts with the specified prefix and returns true if it does, false otherwise.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" "github.com/theMagicalKarp/iter/pkg/predicates" ) func main() { items := iter.New("hello", "world", "foo", "bar", "foot") items = itertools.Filter(items, predicates.HasPrefix("foo")) itertools.Print(items) }
Output: foo, foot
func HasSuffix ¶
HasSuffix returns a predicate function that checks if a string has the specified suffix. It takes a suffix string as input and returns a Predicate[string] function. The returned function takes a string as input and returns true if the string has the specified suffix, false otherwise.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" "github.com/theMagicalKarp/iter/pkg/predicates" ) func main() { items := iter.New("helloo", "world", "foo", "bar", "foot") items = itertools.Filter(items, predicates.HasSuffix("oo")) itertools.Print(items) }
Output: helloo, foo
func Is ¶
func Is[T comparable](this T) Predicate[T]
Is returns a Predicate function that checks if the given value is equal to the original value. The type of the value must be comparable.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" "github.com/theMagicalKarp/iter/pkg/predicates" ) func main() { items := itertools.Filter(iter.New(1, 2, 3, 4, 5), predicates.Is(3)) itertools.Print(items) }
Output: 3
func Not ¶
Not returns a new predicate that negates the result of the given predicate function. It takes a predicate function `f` as input and returns a new predicate function. The returned predicate function returns the logical negation of the result of `f`.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" "github.com/theMagicalKarp/iter/pkg/predicates" ) func main() { items := itertools.Filter(iter.New(1, 2, 3, 4, 5), predicates.Not(predicates.Even[int])) itertools.Print(items) }
Output: 1, 3, 5