Documentation ¶
Overview ¶
Package filter contains utility functions for filtering slices through the distributed application of a filter function.
The package is an experiment to see how easy it is to write such things in Go. It is easy, but for loops are just as easy and more efficient.
You should not use this package.
Index ¶
- func Apply(slice, function interface{}) interface{}
- func ApplyInPlace(slice, function interface{})
- func Choose(slice, function interface{}) interface{}
- func ChooseInPlace(pointerToSlice, function interface{})
- func Drop(slice, function interface{}) interface{}
- func DropInPlace(pointerToSlice, function interface{})
- func Reduce(slice, pairFunction, zero interface{}) interface{}
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Apply ¶
func Apply(slice, function interface{}) interface{}
Apply takes a slice of type []T and a function of type func(T) T. (If the input conditions are not satisfied, Apply panics.) It returns a newly allocated slice where each element is the result of calling the function on successive elements of the slice.
func ApplyInPlace ¶
func ApplyInPlace(slice, function interface{})
ApplyInPlace is like Apply, but overwrites the slice rather than returning a newly allocated slice.
func Choose ¶
func Choose(slice, function interface{}) interface{}
Choose takes a slice of type []T and a function of type func(T) bool. (If the input conditions are not satisfied, Choose panics.) It returns a newly allocated slice containing only those elements of the input slice that satisfy the function.
func ChooseInPlace ¶
func ChooseInPlace(pointerToSlice, function interface{})
ChooseInPlace is like Choose, but overwrites the slice rather than returning a newly allocated slice. Since ChooseInPlace must modify the header of the slice to set the new length, it takes as argument a pointer to a slice rather than a slice.
func Drop ¶
func Drop(slice, function interface{}) interface{}
Drop takes a slice of type []T and a function of type func(T) bool. (If the input conditions are not satisfied, Drop panics.) It returns a newly allocated slice containing only those elements of the input slice that do not satisfy the function, that is, it removes elements that satisfy the function.
func DropInPlace ¶
func DropInPlace(pointerToSlice, function interface{})
DropInPlace is like Drop, but overwrites the slice rather than returning a newly allocated slice. Since DropInPlace must modify the header of the slice to set the new length, it takes as argument a pointer to a slice rather than a slice.
func Reduce ¶
func Reduce(slice, pairFunction, zero interface{}) interface{}
Reduce computes the reduction of the pair function across the elements of the slice. (If the types of the slice and function do not correspond, Reduce panics.) For instance, if the slice contains successive integers starting at 1 and the function is multiply, the result will be the factorial function. If the slice is empty, Reduce returns zero; if it has only one element, it returns that element. The return value must be type-asserted by the caller back to the element type of the slice. Example:
func multiply(a, b int) int { return a*b } a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} factorial := Reduce(a, multiply, 1).(int)
Types ¶
This section is empty.