Documentation ¶
Overview ¶
Package functools is a simple Golang library including some commonly used functional programming tools. There is no generic in golang, so most of the functions will return interface{}, be sure to type assert it to the type you want before you use it.
Index ¶
- func All(function, slice interface{}) (ret bool, err error)
- func Any(function, slice interface{}) (ret bool, err error)
- func Apply(function, slice interface{}) (ret interface{}, err error)
- func Compose(functions ...interface{}) (ret func(...interface{}) interface{}, err error)
- func Filter(function, slice interface{}) (ret interface{}, err error)
- func Partial(function interface{}, params ...interface{}) (ret func(...interface{}) interface{}, err error)
- func Reduce(function, slice, initial interface{}) (ret interface{}, err error)
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶
All applies a function(the first parameter) returning a boolean value to each element of a slice(second parameter), if all elements make that function return true then All will return true, otherwise false. Notice that All returns a boolean value, not an interface{}.
func Any ¶
Any applies a function(the first parameter) returning a boolean value to each element of a slice(second parameter), if there exist at least one element make that function return true then Any will return true, otherwise false. Notice that Any returns a boolean value, not an interface{}.
func Apply ¶
func Apply(function, slice interface{}) (ret interface{}, err error)
Apply applies a function(the first parameter) to each element of a slice(second parameter). It acts just like Map in other languages.
func Compose ¶
func Compose(functions ...interface{}) (ret func(...interface{}) interface{}, err error)
Compose will compose the received functions, there is no limit on the number of functions. Notice that the return value of the composed function is interface{}.
func Filter ¶
func Filter(function, slice interface{}) (ret interface{}, err error)
Filter apples a function (the first parameter) to each element of a slice (the second parameter), and filters parameters for which the function returns true.
func Partial ¶
func Partial(function interface{}, params ...interface{}) (ret func(...interface{}) interface{}, err error)
Partial will apply params (all parameters from 2nd paramter) to the passed function (first parameter). The function returns another function which can be called with the rest of the parameters.
Types ¶
type Option ¶
type Option struct {
// contains filtered or unexported fields
}
Type Option represents an optional value: every Option is either Some and contains a value, or None, and contains a nil. They have a number of uses. For example, if a function call failes, it can return None on error.
func Some ¶
func Some(i interface{}) Option
Some will return an Option containing the given value. Some cannot use to generate a None. If the given value is nil, it will panic.
func (*Option) And ¶
And returns None if this option is None, otherwise returns the option received.
func (*Option) And_then ¶
And_then returns None if this option is None, otherwise calls the received function with the wrapped value and returns the result Option.
func (*Option) Bind ¶
Bind will apply a given function to the wrapped value of this Option and return a new Option containing the return value of the paramter function. If this Option is None, then it will return None.