filter

package
v0.0.0-...-157c9c8 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: GPL-3.0 Imports: 9 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

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 Comparable

type Comparable interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
		~float32 | ~float64 |
		~string
}

Comparable are the types that support ==, != and >.

type Filter

type Filter[T any] func(T) bool

Filter represents logic to classify a type as passing or failing.

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 Comparable](a T) Filter[T]

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

func GT

func GT[T Comparable](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 Comparable](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 Comparable](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 Comparable](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 Comparable](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 Filter is true.

func (Filter[T]) Chan

func (f Filter[T]) Chan(ch <-chan T, buf int) <-chan 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]) 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 Map

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

func NewMap

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

func (Map[K, V]) Filter

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

func (Map[K, V]) Map

func (mf Map[K, V]) Map(m, buf map[K]V) map[K]V

func (Map[K, V]) Purge

func (mf Map[K, V]) Purge(m map[K]V, buf []K) slice.Slice[K]

func (Map[K, V]) Slice

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

type MapKeyFilter

type MapKeyFilter[K comparable, V any] Filter[K]

MapKeyFilter applys a Filter to the keys of a map.

func (MapKeyFilter[K, V]) KeySlice

func (mkf MapKeyFilter[K, V]) KeySlice(m map[K]V) slice.Slice[K]

KeySlice returns all the keys in the map for which the underlying filter is true.

func (MapKeyFilter[K, V]) Map

func (mkf MapKeyFilter[K, V]) Map(m map[K]V) map[K]V

Map creates a new map populated with all the key/value pairs for with the underlying filter is true for the key.

func (MapKeyFilter[K, V]) Purge

func (mkf MapKeyFilter[K, V]) Purge(m map[K]V)

Purge all values from the map where the filter is false for the key.

func (MapKeyFilter[K, V]) ValSlice

func (mkf MapKeyFilter[K, V]) ValSlice(m map[K]V) slice.Slice[V]

ValSlice returns all the values in the map for which the underlying filter is true for the corresponding key.

type MapSliceFlag

type MapSliceFlag byte

type MapValueFilter

type MapValueFilter[K comparable, V any] Filter[V]

MapValueFilter applys a Filter to the values of a map.

func (MapValueFilter[K, V]) KeySlice

func (mvf MapValueFilter[K, V]) KeySlice(m map[K]V) slice.Slice[K]

KeySlice returns all the keys in the map for which the underlying filter is true for the corresponding value.

func (MapValueFilter[K, V]) Map

func (mvf MapValueFilter[K, V]) Map(m map[K]V) map[K]V

Map creates a new map populated with all the key/value pairs for with the underlying filter is true for the value.

func (MapValueFilter[K, V]) Purge

func (mvf MapValueFilter[K, V]) Purge(m map[K]V)

Purge all values from the map where the filter is false for the value.

func (MapValueFilter[K, V]) ValSlice

func (mvf MapValueFilter[K, V]) ValSlice(m map[K]V) slice.Slice[V]

ValSlice returns all the values in the map for which the underlying filter is true.

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

func InType

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

TODO

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 Filter[int]) Type

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

func NumInEq

func NumInEq(n int) Type

TODO

func NumOut

func NumOut(f Filter[int]) Type

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

func NumOutEq

func NumOutEq(n int) Type

TODO

func OutType

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

TODO

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) 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.

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