filter

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2024 License: MIT Imports: 0 Imported by: 2

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

func Slice

func Slice[T any](entries []T, filter Func[T]) []T

Slice filters the specified slice of entries and returns a new slice that contains only those elements that have passed the filter Func.

Types

type Func

type Func[T any] func(T) bool

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

func And[T any](filters ...Func[T]) Func[T]

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 False

func False[T any]() Func[T]

False creates a filter Func that always returns false, regardless of the input value.

func Not

func Not[T any](delegate Func[T]) Func[T]

Not returns a filter function that returns the opposite of what the specified Func in the arguments would return.

func Or

func Or[T any](filters ...Func[T]) Func[T]

Or returns true if any 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 True

func True[T any]() Func[T]

True creates a filter Func that always returns true, regardless of the input value.

Jump to

Keyboard shortcuts

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