filter

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2024 License: ISC Imports: 6 Imported by: 0

Documentation

Overview

Package `filter` contains utilities for filtering slices and sequences using dynamically-constructed rules.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Filter

func Filter[S ~[]E, E any](src S, fn Fn[E]) iter.Seq[E]

Returns an iterator that yields elements of src for which fn returns true.

func FilterRef added in v0.1.1

func FilterRef[S ~[]E, E any](src S, fn Fn[*E]) iter.Seq[*E]

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 FilterSeq

func FilterSeq[E any](src iter.Seq[E], fn Fn[E]) iter.Seq[E]

Returns an iterator that yields elements of src for which fn returns true.

func Filtered

func Filtered[S ~[]E, E any](src S, fn Fn[E]) S

Returns a new slice containing elements of src for which fn returns true.

func FilteredRef added in v0.1.1

func FilteredRef[S ~[]E, E any](src S, fn Fn[*E]) []*E

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.

func FilteredSeq

func FilteredSeq[E any](src iter.Seq[E], fn Fn[E]) []E

Returns a new slice containing elements of src for which fn returns true.

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)

func (*ComparableMethod[T, V]) CompareTo added in v0.1.2

func (m *ComparableMethod[T, V]) CompareTo(val string, gt, eq bool) (Fn[T], error)

func (*ComparableMethod[T, V]) EqualTo added in v0.1.2

func (m *ComparableMethod[T, V]) EqualTo(val string, neg bool) (Fn[T], error)

type Fn

type Fn[T any] func(v T) bool

Filter function; returns true for elements that are to be kept.

func And

func And[T any](fns ...Fn[T]) Fn[T]

Logical AND applied to a set of filter functions.

func Not

func Not[T any](fn Fn[T]) Fn[T]

Logical NOT applied to a single filter function.

func Or

func Or[T any](fns ...Fn[T]) Fn[T]

Logical OR applied to a set of filter functions.

func Xor

func Xor[T any](fns ...Fn[T]) Fn[T]

Logical XOR applied to a set of filter functions.

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

type Methods[T any] map[string]Method[T]

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]) MustParse added in v0.1.1

func (ms Methods[T]) MustParse(expr string) Fn[T]

Alternate version of Parse that panics on error.

func (Methods[T]) MustParseAll added in v0.1.1

func (ms Methods[T]) MustParseAll(exprs ...string) []Fn[T]

Alternate version of ParseAll that panics on error.

func (Methods[T]) Parse added in v0.1.1

func (ms Methods[T]) Parse(expr string) (Fn[T], error)

Parses a filter expression into an equivalent filter function.

func (Methods[T]) ParseAll added in v0.1.1

func (ms Methods[T]) ParseAll(exprs ...string) ([]Fn[T], error)

Parses multiple filter expressions into equivalent filter functions.

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)

func (*OrderedMethod[T, V]) CompareTo added in v0.1.1

func (m *OrderedMethod[T, V]) CompareTo(val string, gt, eq bool) (Fn[T], error)

func (*OrderedMethod[T, V]) EqualTo added in v0.1.1

func (m *OrderedMethod[T, V]) EqualTo(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)

func (*StringMethod[T]) CompareTo added in v0.1.1

func (m *StringMethod[T]) CompareTo(val string, gt, eq bool) (Fn[T], error)

func (*StringMethod[T]) EqualTo added in v0.1.1

func (m *StringMethod[T]) EqualTo(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)

func (*StringsMethod[T]) CompareTo added in v0.1.1

func (m *StringsMethod[T]) CompareTo(val string, gt, eq bool) (Fn[T], error)

func (*StringsMethod[T]) EqualTo added in v0.1.1

func (m *StringsMethod[T]) EqualTo(val string, neg bool) (Fn[T], error)

Jump to

Keyboard shortcuts

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