Documentation
¶
Overview ¶
Package async provides a golang representation of the async/promise/deferred monad along with useful methods to operate on these monads.
Additional information
http://en.wikipedia.org/wiki/Futures_and_promises http://en.wikipedia.org/wiki/Monad_%28functional_programming%29 http://en.wikipedia.org/wiki/Monad_%28category_theory%29
Installation
go get github.com/obsc/async
The Async struct represents a value or slice of values that become determined when some asynchronous operation complete.
An Async struct can either be "determined" or "undetermined" at any point in time. However, it can only be "determined" once, and will henceforth remain determined forever.
Unless otherwise specified, every library call will return immediately and not block any other operations. Because of this, it is recommended that any operation you write that returns an Async struct, do so immediately, allowing the program to continue to other tasks.
While many of the methods can be written purely in terms of Return and Bind, they are still implemented separately to minimize the overheard due to the creation of more goroutines and more channels.
This package is loosely based off of Jane Street's OCaml library: Async
https://ocaml.janestreet.com/ocaml-core/111.17.00/doc/async/#Std
Index ¶
- func Lift(f interface{}) func(*Async) *Async
- func Lift2(f interface{}) func(*Async) func(*Async) *Async
- type Async
- func (self *Async) And(other *Async) *Async
- func (self *Async) Bind(f interface{}) *Async
- func (self *Async) End(f interface{})
- func (self *Async) Fmap(f interface{}) *Async
- func (self *Async) IsDone() bool
- func (self *Async) Join(f interface{}) *Async
- func (self *Async) Or(other *Async) *Async
- func (self *Async) Wait()
- func (self *Async) WaitToEnd(f interface{})
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Async ¶
type Async struct {
// contains filtered or unexported fields
}
Type: M a
Represents the async monad
func All ¶
Type: [](M a) -> M ([]a)
All returns an Async struct that becomes determined when all of asyncs becomes determined. It contains the return value of all the structs concatenated together.
func Always ¶
func Always() *Async
Type: unit -> M unit
Always represents an Async struct that is determined as soon as it is created.
func Any ¶
Type: [](M a) -> M ([]a)
Any returns an Async struct that becomes determined whenever any of asyncs becomes determined. It contains the return value of whichever one is determined first.
func Deferred ¶
func Deferred(f interface{}) *Async
Type: (unit -> a) -> M a
Deferred executes a function asynchronously and returns an Async struct representing the return values.
func Never ¶
func Never() *Async
Type: unit -> M unit
Never represents an Async struct that is never determined.
func Return ¶
func Return(vs ...interface{}) *Async
Type: a -> M a
Return takes a set of inputs and returns an already determined Async struct wrapping those inputs
func (*Async) And ¶
Type: M a -> M b -> M (a, b)
And returns an Async struct that becomes determined when both self and other become determined. It contains return values of self and other concatenated together.
func (*Async) Bind ¶
Type: M a -> (a -> M b) -> M b
Bind behaves the same way as Fmap except it operates on functions that return Async structs.
func (*Async) End ¶
func (self *Async) End(f interface{})
Type: M a -> (a -> b) -> unit
End is the nonblocking version of WaitToEnd
func (*Async) Fmap ¶
Type: M a -> (a -> b) -> M b
Fmapping a function to self guarantees that function will execute on the return values of self as soon self is determined. It returns an Async struct representing the return values of the called function.
If Fmap was called after self is already deteremined, the function will begin to execute right away.
Note that while this represents a functor, the input types are reversed. This is done to make it a method rather than a function as Go does not allow creation of new operators.
func (*Async) IsDone ¶
Type: M a -> bool
IsDone returns whether or not self has becomed determined yet.
func (*Async) Or ¶
Type: M a -> M a -> M a
Or returns an Async struct that becomes determined whenever the first of either self or other becomes determined. It contains the return value of whichever one is determined first.