Documentation
¶
Overview ¶
Package filter adds helper functions for performing filtering over a data set.
There are times when one needs to filter a slice of values but the exact filtering logic cannot be hardcoded in advance and is conditional on some external flags or parameters. In such cases, it is useful to be able to dynamically construct a filtering expression that can pick values from some data set.
This can be achieved through the usage of the Slice function and the set of predefined filtering expressions like Or, And, Not and more, where custom ones can be plugged in as well.
Example ¶
package main import ( "fmt" "strings" "github.com/mokiat/gog/filter" ) func main() { items := []string{ "hello universe", "big world", "hello strange world", "will be dropped", "oh well", "let me through", } hasHelloPrefix := func(candidate string) bool { return strings.HasPrefix(candidate, "hello") } hasWorldSuffix := func(candidate string) bool { return strings.HasSuffix(candidate, "world") } filteredItems := filter.Slice(items, filter.Or( filter.And( hasHelloPrefix, hasWorldSuffix, ), filter.Equal("let me through"), ), ) fmt.Printf("%#v\n", filteredItems) }
Output: []string{"hello strange world", "let me through"}
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Func ¶
Func represents a filtering function. The actual semantics of the Func depend on the user but in general if true is returned then the value that is being checked is accepted.
func And ¶
And returns true if all of the specified Func filters in the arguments return true. If no filters are specified as arguments, then the returned Func always returns true.
func Equal ¶
func Equal[T comparable](expected T) Func[T]
Equal returns a filter that returns true only if the input value matches the specified value.
func Not ¶
Not returns a filter function that returns the opposite of what the specified Func in the arguments would return.