Documentation ¶
Overview ¶
Package `filter` contains utilities for filtering slices and sequences using dynamically-constructed rules.
Index ¶
- func Filter[S ~[]E, E any](src S, fn Fn[E]) iter.Seq[E]
- func FilterRef[S ~[]E, E any](src S, fn Fn[*E]) iter.Seq[*E]
- func FilterSeq[E any](src iter.Seq[E], fn Fn[E]) iter.Seq[E]
- func Filtered[S ~[]E, E any](src S, fn Fn[E]) S
- func FilteredRef[S ~[]E, E any](src S, fn Fn[*E]) []*E
- func FilteredSeq[E any](src iter.Seq[E], fn Fn[E]) []E
- type ComparableMethod
- type Fn
- type Method
- type Methods
- type OrderedMethod
- type StringMethod
- type StringsMethod
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FilterRef ¶ added in v0.1.1
Returns an iterator that yields pointers to elements of src for which fn returns true.
Useful when the elements of src are inefficient to copy, or where the intent is to modify the matched elements in place.
Example ¶
package main import ( "fmt" "gitlab.com/cptpackrat/go-seq/filter" ) type Bar struct { Value int Name string Tags []string } func (b *Bar) Tag(tag string) { b.Tags = append(b.Tags, tag) } var bar = []Bar{ {1, "first", []string{}}, {2, "second", []string{}}, {3, "third", []string{}}, {4, "fourth", []string{}}, {5, "fifth", []string{}}, {6, "sixth", []string{}}, {7, "seventh", []string{}}, {8, "eighth", []string{}}, {9, "ninth", []string{}}, } func isOdd(b *Bar) bool { return b.Value%2 == 1 } func isEven(b *Bar) bool { return b.Value%2 == 0 } func isExactlyThree(b *Bar) bool { return b.Value == 3 } func main() { for b := range filter.FilterRef(bar, isOdd) { b.Tag("odd") } for b := range filter.FilterRef(bar, isEven) { b.Tag("even") } for b := range filter.FilterRef(bar, isExactlyThree) { b.Tag("three") } for _, b := range bar { fmt.Println(b) } }
Output: {1 first [odd]} {2 second [even]} {3 third [odd three]} {4 fourth [even]} {5 fifth [odd]} {6 sixth [even]} {7 seventh [odd]} {8 eighth [even]} {9 ninth [odd]}
func FilteredRef ¶ added in v0.1.1
Returns a new slice containing pointers to elements of src for which fn returns true.
Useful when the elements of src are inefficient to copy, or where the intent is to modify the matched elements in place.
Types ¶
type ComparableMethod ¶ added in v0.1.2
type ComparableMethod[T any, V comparable] struct { Method[T] // Parses the source value from the query, or returns an error. Parse func(exp string) (V, error) // Extracts the target value for an instance of T. Value func(rcv T) V // Optional approximate match implementation. Approx func(rcv, exp V) bool // Optional ordered comparison implementation. Compare func(rcv, exp V) int }
Filters based on any comparable value extracted from instances of T.
Does not support approximate matching or ordered comparison by default, but accepts optional custom implementations to enable them.
func (*ComparableMethod[T, V]) ApproxTo ¶ added in v0.1.2
func (m *ComparableMethod[T, V]) ApproxTo(val string, neg bool) (Fn[T], error)
type Method ¶ added in v0.1.1
type Method[T any] interface { // Returns an equality comparator for instances of T against val. EqualTo(val string, neg bool) (Fn[T], error) // Returns an approximate comparator for instances of T against val. ApproxTo(val string, neg bool) (Fn[T], error) // Returns an ordered comparator for instances of T against val. CompareTo(val string, gt, eq bool) (Fn[T], error) }
A method of filtering instances of T.
type Methods ¶ added in v0.1.1
A collection of methods for filtering instances of T.
Example (Basic) ¶
package main import ( "fmt" "strconv" "gitlab.com/cptpackrat/go-seq/filter" ) type Foo struct { X int Y string } var foo = []Foo{ {1, "first"}, {2, "second"}, {3, "third"}, {1, "fourth"}, {2, "fifth"}, {3, "sixth"}, {1, "seventh"}, {2, "eighth"}, {3, "ninth"}, } var ms = filter.Methods[Foo]{ "x": &filter.OrderedMethod[Foo, int]{ Parse: strconv.Atoi, Value: func(rcv Foo) int { return rcv.X }, }, "y": &filter.StringMethod[Foo]{ Value: func(rcv Foo) string { return rcv.Y }, }, } func main() { // select where X < 2 fn := ms.MustParse("x<2") for val := range filter.Filter(foo, fn) { fmt.Println(val) } }
Output: {1 first} {1 fourth} {1 seventh}
Example (Compound) ¶
package main import ( "fmt" "strconv" "gitlab.com/cptpackrat/go-seq/filter" ) type Foo struct { X int Y string } var foo = []Foo{ {1, "first"}, {2, "second"}, {3, "third"}, {1, "fourth"}, {2, "fifth"}, {3, "sixth"}, {1, "seventh"}, {2, "eighth"}, {3, "ninth"}, } var ms = filter.Methods[Foo]{ "x": &filter.OrderedMethod[Foo, int]{ Parse: strconv.Atoi, Value: func(rcv Foo) int { return rcv.X }, }, "y": &filter.StringMethod[Foo]{ Value: func(rcv Foo) string { return rcv.Y }, }, } func main() { // select where X == 2 AND Y contains "th" fn := filter.And( ms.MustParse("x==2"), ms.MustParse("y=~th")) for val := range filter.Filter(foo, fn) { fmt.Println(val) } }
Output: {2 fifth} {2 eighth}
Example (Convoluted) ¶
package main import ( "fmt" "strconv" "gitlab.com/cptpackrat/go-seq/filter" ) type Foo struct { X int Y string } var foo = []Foo{ {1, "first"}, {2, "second"}, {3, "third"}, {1, "fourth"}, {2, "fifth"}, {3, "sixth"}, {1, "seventh"}, {2, "eighth"}, {3, "ninth"}, } var ms = filter.Methods[Foo]{ "x": &filter.OrderedMethod[Foo, int]{ Parse: strconv.Atoi, Value: func(rcv Foo) int { return rcv.X }, }, "y": &filter.StringMethod[Foo]{ Value: func(rcv Foo) string { return rcv.Y }, }, } func main() { // select where Y == "first" OR Y == "ninth" OR (X == 2 AND Y does not contain "th") fn := filter.Or( ms.MustParse("y==first"), ms.MustParse("y==ninth"), filter.And( ms.MustParse("x==2"), ms.MustParse("y!~th"))) for val := range filter.Filter(foo, fn) { fmt.Println(val) } }
Output: {1 first} {2 second} {3 ninth}
func (Methods[T]) MustParseAll ¶ added in v0.1.1
Alternate version of ParseAll that panics on error.
type OrderedMethod ¶ added in v0.1.1
type OrderedMethod[T any, V cmp.Ordered] struct { Method[T] Reverse bool // Reverse reported ordering. // Parses the source value from the query, or returns an error. Parse func(exp string) (V, error) // Extracts the target value for an instance of T. Value func(rcv T) V // Optional approximate match implementation. Approx func(rcv, exp V) bool }
Filters based on any ordered value extracted from instances of T.
Does not support approximate matching by default, but accepts an optional custom implementation to enable it.
func (*OrderedMethod[T, V]) ApproxTo ¶ added in v0.1.1
func (m *OrderedMethod[T, V]) ApproxTo(val string, neg bool) (Fn[T], error)
type StringMethod ¶ added in v0.1.1
type StringMethod[T any] struct { Method[T] EqualFold bool // If true, equality comparison ignores case. ApproxFold bool // If true, approximate match ignores case. ApproxRegexp bool // If true, approximate match uses regular expressions instead of substring match. // Extracts the target value for an instance of T. Value func(rcv T) string // Optional implementation for ordered comparison. Compare func(rcv, exp string) int }
Filters based on a string value extracted from instances of T.
Does not support ordered comparison by default, but accepts an optional custom implementation to enable it.
func (*StringMethod[T]) ApproxTo ¶ added in v0.1.1
func (m *StringMethod[T]) ApproxTo(val string, neg bool) (Fn[T], error)
type StringsMethod ¶ added in v0.1.1
type StringsMethod[T any] struct { Method[T] EqualFold bool // If true, equality comparison ignores case. ApproxFold bool // If true, approximate match ignores case. ApproxRegexp bool // If true, approximate match uses regular expressions instead of substring match. // Extracts the target values for an instance of T. Values func(rcv T) []string }
Filters based on an array of string values extracted from instances of T.
Does not support ordered comparison.
func (*StringsMethod[T]) ApproxTo ¶ added in v0.1.1
func (m *StringsMethod[T]) ApproxTo(val string, neg bool) (Fn[T], error)