async

package module
v0.0.0-...-a6e2df6 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2014 License: MIT Imports: 1 Imported by: 0

README

async

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Lift

func Lift(f interface{}) func(*Async) *Async

Type: (a -> b) -> (M a -> M b)

Lift is another representation of Fmap with the types reversed.

Lift can take any unary function and return a function that operates on Async structs.

func Lift2

func Lift2(f interface{}) func(*Async) func(*Async) *Async

Type: ((a, b) -> c) -> M a -> M b -> M c

Lift2 behaves the same as lift except with binary functions.

The parameters to the function are curried as well.

Types

type Async

type Async struct {
	// contains filtered or unexported fields
}

Type: M a

Represents the async monad

func All

func All(asyncs ...*Async) *Async

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

func Any(asyncs ...*Async) *Async

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

func (self *Async) And(other *Async) *Async

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

func (self *Async) Bind(f interface{}) *Async

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

func (self *Async) Fmap(f interface{}) *Async

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

func (self *Async) IsDone() bool

Type: M a -> bool

IsDone returns whether or not self has becomed determined yet.

func (*Async) Join

func (self *Async) Join(f interface{}) *Async

Type: M (M a) -> M a

Join is used to unify multiple layers of Async structs.

func (*Async) Or

func (self *Async) Or(other *Async) *Async

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.

func (*Async) Wait

func (self *Async) Wait()

Type: M a -> unit

Wait is a blocking function that waits until self has become determined.

func (*Async) WaitToEnd

func (self *Async) WaitToEnd(f interface{})

Type: M a -> (a -> b) -> unit

WaitToEnd is a blocking function that executes a function on the return of self. This ends the chain of Asyncs so there is no overheard of a channel being instantiated.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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