monad: Provide monadi primitives

Description Value Usage Details Examples

Description

These functions implement basic monads as seen in Haskell.

Value

Usually a monad

Usage

Maybe(a) Just(a) Nothing()

unit(x) unit(x)

Bind operator m m

m m

Regular composition f(g(x)) f

Monad composition g(f(x)) f

Monad composition f(g(x)) f

Details

Monads are types that carry context. Monad operators enable chaining monadic operations together.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
require(lambda.r)
safelog(x) %::% numeric : Maybe
safelog(x) %when% { x <= 0 } %as% Nothing()
safelog(x) %:=% Just(log(x))

safesqrt(x) %::% numeric : Maybe
safesqrt(x) %when% { x <= 0 } %as% Nothing()
safesqrt(x) %:=% Just(sqrt(x))

safelogsqrt <- safelog %<=<% safesqrt
safelogsqrt(-2)
safelogsqrt(2)

zatonovo/lambda.tools documentation built on May 4, 2019, 9:11 p.m.