functools

package
v0.0.0-...-c5e9260 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2017 License: BSD-2-Clause Imports: 4 Imported by: 3

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All(function, slice interface{}) (ret bool, err error)

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

func Any(function, slice interface{}) (ret bool, err error)

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.

func Reduce

func Reduce(function, slice, initial interface{}) (ret interface{}, err error)

Reduce applies a function (the first parameter) of two arguments cumulatively to each element of a slice (second parameter), and the initial value is the third parameter.

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.

var None Option = Option{nil}

None is a special Option containing nil.

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

func (this *Option) And(other Option) Option

And returns None if this option is None, otherwise returns the option received.

func (*Option) And_then

func (this *Option) And_then(function interface{}) Option

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

func (this *Option) Bind(function interface{}) Option

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.

func (*Option) Is_none

func (this *Option) Is_none() bool

Is_none determines whether this Option is None.

func (*Option) Is_some

func (this *Option) Is_some() bool

Is_some determines whether this Option is Some. (Contains a value)

func (*Option) Unwrap

func (this *Option) Unwrap() interface{}

Unwrap returns the wrapped value of an Option. If the Option is None, it will panic.

Jump to

Keyboard shortcuts

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