filter

package
v0.0.0-...-7e023e1 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2024 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ReturnKeys = 1 << iota
	InverseKeys
	ReturnVals
	InverseVals

	ReturnBoth = ReturnKeys | ReturnVals
)

Variables

This section is empty.

Functions

func TypeErr

func TypeErr(format string) func(t reflect.Type) error

TypeErr formats an error using reflect.Type.

Types

type Checker

type Checker[T any] func(T) error

Checker returns an error based on a single argument.

func (Checker[T]) Panic

func (c Checker[T]) Panic(val T)

Panic runs the Checker and if it returns an error, panics with that error.

type Compare

type Compare[T any] func(a, b T) int

Compare should return 0 if a==0, -1 if a<b and 1 if a>b.

func Comparer

func Comparer[C constraints.Ordered]() Compare[C]

type Filter

type Filter[T any] func(T) bool

Filter represents boolean logic on a Type.

func Contains

func Contains(substr string) Filter[string]

Contains creates a string Filter that returns true when passed a string that contains the given substr.

func EQ

func EQ[T constraints.Ordered](a T) Filter[T]

EQ returns a Filter that will check if a given value is equal to 'a'.

func GT

func GT[T constraints.Ordered](a T) Filter[T]

EQ returns a Filter that will check if a given value is greater than to 'a'.

func GTE

func GTE[T constraints.Ordered](a T) Filter[T]

GTE returns a Filter that will check if a given value is greater than or equal to 'a'.

func LT

func LT[T constraints.Ordered](a T) Filter[T]

LT returns a Filter that will check if a given value is not less than to 'a'.

func LTE

func LTE[T constraints.Ordered](a T) Filter[T]

LTE returns a Filter that will check if a given value is less than or equal to 'a'.

func MethodName

func MethodName(f func(string) bool) Filter[*reflector.Method]

MethodName takes a string filter and applies it to a Method name.

func NEQ

func NEQ[T constraints.Ordered](a T) Filter[T]

NEQ returns a Filter that will check if a given value is not equal to 'a'.

func New

func New[T any](fn func(T) bool) Filter[T]

New is syntactic sugar to make filter declarations a bit cleaner.

func Prefix

func Prefix(prefix string) Filter[string]

Prefix creates a string Filter that returns true when passed a string with the given prefix.

func Regex

func Regex(re string) (Filter[string], error)

Regex returns the MatchString method on regular expressions generated from re.

func Suffix

func Suffix(suffix string) Filter[string]

Suffix creates a string Filter that returns true when passed a string with the given suffix.

func (Filter[T]) And

func (f Filter[T]) And(f2 Filter[T]) Filter[T]

And builds a new Filter that will return true if both underlying Filters are true.

func (Filter[T]) AndN

func (f Filter[T]) AndN(fs ...Filter[T]) Filter[T]

func (Filter[T]) Chan

func (f Filter[T]) Chan(pipe channel.Pipe[T]) channel.Pipe[T]

Chan runs a go routine listening on ch and any int that passes the Int is passed to the channel that is returned.

func (Filter[T]) Check

func (f Filter[T]) Check(errFn func(T) error) Checker[T]

Check converts a filter to a Checker and returns the provided err if the filter fails.

func (Filter[T]) First

func (f Filter[T]) First(vals ...T) (t T, idx int)

First returns the first value that passes the Filter and the index. If none pass, then idx will be -1 and t will be the default value.

func (Filter[T]) FirstIdx

func (f Filter[T]) FirstIdx(vals ...T) int

FirstIdx finds the index of the first value that passes the filter

func (Filter[T]) Iter

func (f Filter[T]) Iter(i liter.Iter[T]) liter.Wrapper[T]

Iter created from the Filter.

func (Filter[T]) Not

func (f Filter[T]) Not() Filter[T]

Not builds a new Filter that will return true if the underlying Filter is false.

func (Filter[T]) Or

func (f Filter[T]) Or(f2 Filter[T]) Filter[T]

Or builds a new Filter that will return true if either underlying Filter is true.

func (Filter[T]) Slice

func (f Filter[T]) Slice(vals []T) slice.Slice[T]

Slice creates a new slice holding all values that return true when passed to Filter.

func (Filter[T]) SliceBuf

func (f Filter[T]) SliceBuf(vals, buf []T) slice.Slice[T]

func (Filter[T]) SliceInPlace

func (f Filter[T]) SliceInPlace(vals []T) (passing, failing slice.Slice[T])

SliceInPlace reorders the slice so all the elements passing the filter are at the start of the slice and all elements failing the filter are at the end. It returns two subslices, the first for passing, the second for failing. No guarentees are made about the order of the subslices.

type Iter

type Iter[T any] struct {
	In liter.Iter[T]
	Filter[T]
	// contains filtered or unexported fields
}

Iter wraps an Iter and applies a Filter to it. Only values that pass the filter are returned.

func (*Iter[T]) Cur

func (i *Iter[T]) Cur() (t T, done bool)

Cur fulfills liter.Iter and returns the current value of the iterator and a bool indicating if iteration is done.

func (*Iter[T]) Done

func (i *Iter[T]) Done() bool

Done returns a bool indicating if iteration is done.

func (*Iter[T]) Idx

func (i *Iter[T]) Idx() int

Idx returns the current index. This index is associated with the filtered iterator, not the underlying iterator.

func (*Iter[T]) Next

func (i *Iter[T]) Next() (t T, done bool)

Next fulfills liter.Iter, moves to the next value in the underlying Iter that passes the Filter or the default value of T if the iterator is done. Returns a bool indicating if iteration is done.

type MapFilter

type MapFilter[K comparable, V any] struct {
	Key Filter[K]
	Val Filter[V]
}

MapFilter provides a filter for Keys and Values. For either, a nil Filter will be ignored.

func NewMap

func NewMap[K comparable, V any](k Filter[K], v Filter[V]) MapFilter[K, V]

NewMap creates a Map filter from the provided filters.

func (MapFilter[K, V]) Filter

func (mf MapFilter[K, V]) Filter(k K, v V) bool

Filter checks the key and value against the map filter.

func (MapFilter[K, V]) Map

func (mf MapFilter[K, V]) Map(m Mapper[K, V], to lmap.Mapper[K, V]) lmap.Wrapper[K, V]

func (MapFilter[K, V]) Purge

func (mf MapFilter[K, V]) Purge(m Mapper[K, V], buf []K) slice.Slice[K]

func (MapFilter[K, V]) Slice

func (mf MapFilter[K, V]) Slice(m Mapper[K, V], keyBuf []K, valBuf []V, flags MapSliceFlag) (keys slice.Slice[K], vals slice.Slice[V])

type MapSliceFlag

type MapSliceFlag byte

type Mapper

type Mapper[K comparable, V any] interface {
	Each(lmap.IterFunc[K, V])
	Delete(K)
}

Mapper is a representation of lmap.Mapper. But the filter package only needs the Each method.

type Type

type Type struct {
	Filter[reflect.Type]
}

Type is a wrapper around Filter[reflect.Type] to provide helper logic for type filtering.

func AnyType

func AnyType() Type

AnyType is a type filter that returns True for any type. This can be useful when creating checks for things like functions.

func ConvertableTo

func ConvertableTo[I any]() Type

func Func

func Func(args, rets []any) Type

Func builds a filter for a function. Both args and rets can be either a filter.Type, filter.Filter[reflect.Type] or reflect.Type.

func Implements

func Implements[I any]() Type

Implements returns true if the provided type implements interface I.

func InType

func InType(n int, t reflect.Type) Type

InType is a helper equivalent to filter.IsType(t).In(n).

func IsKind

func IsKind(kind reflect.Kind) Type

IsKind creates a Type filter that returns true when given a type that matches the specified kind.

func IsType

func IsType(referenceType reflect.Type) Type

IsType creates a filter using referenceType. Returns true if the filterType is the same as referenceType.

func NumIn

func NumIn(f func(int) bool) Type

NumIn checks the filter type's NumIn value against the given filter.

func NumInEq

func NumInEq(n int) Type

NumInEq is a helper equivalent to filter.NumIn(filter.EQ(n)).

func NumOut

func NumOut(f func(int) bool) Type

NumOut checks the filter type's NumOut value against the given filter.

func NumOutEq

func NumOutEq(n int) Type

NumOutEq is a helper equivalent to filter.NumOut(filter.EQ(n)).

func OutType

func OutType(n int, t reflect.Type) Type

OutType is a helper equivalent to filter.IsType(t).Out(n).

func (Type) And

func (t Type) And(t2 Type) Type

And builds a new Type filter that will return true if both underlying Type filters are true.

func (Type) AndN

func (t Type) AndN(ts ...Type) Type

func (Type) Check

func (t Type) Check(errFn func(reflect.Type) error) TypeChecker

Check creates a TypeChecker from a Type filter. It uses reflector.ToType, so that it can accept either a reflect.Type and use it directly or an interface which it will call reflect.ToType on.

func (Type) Elem

func (t Type) Elem() Type

Elem checks the filter type's Elem against the underlying filter.

func (Type) In

func (t Type) In(i int) Type

In checks the filter type's agument number i against the given filter. If i is less than 0, it will be indexed relative to the number of arguments, so -1 will return the last argument.

func (Type) Method

func (t Type) Method() Filter[*reflector.Method]

Method applies the Type filter to the method function.

func (Type) Not

func (t Type) Not() Type

Or builds a new Type filter that will return true if the underlying Type filter is false.

func (Type) OnInterface

func (t Type) OnInterface(i any) bool

OnInterface applies the filter to the TypeOf i.

func (Type) Or

func (t Type) Or(t2 Type) Type

Or builds a new Type filter that will return true if either underlying Type filters is true.

func (Type) Out

func (t Type) Out(i int) Type

Out checks the filter type's agument number i against the given filter. If i is less than 0, it will be indexed relative to the number of returns, so -1 will return the last return.

func (Type) SliceAnyInPlace

func (t Type) SliceAnyInPlace(vals []any) (passing, failing slice.Slice[any])

type TypeChecker

type TypeChecker func(i any) (reflect.Type, error)

TypeChecker checks a value's type against a filter. It returns the underlying type. It returns an error if the type fails the underlying filter.

Jump to

Keyboard shortcuts

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