Monads in Pony.
use "monad"
use "debug"
actor Main
new create(env: Env) =>
// Identity[T]
let v = Identity[U32](2)
.map[String]({ (n) => "n: " + n.string() })
.flat_map[String]({ (str) => Identity[String](str + "!") })
.identity()
Debug(v) // => "n: 2!"
// Maybe[T]
let vv = Just[String]("Hello")
.flat_map[String]({ (str) => Nothing[String] })
.map[String]({ (str) => str + ", world!" })
.maybe[String]("Nothing!", { (x) => x })
Debug(vv) // => "Nothing!"
// Either[L, R]
let vvv = Right[String, U32](5)
.map[U32]({ (n) => n + 8 })
.flat_map[U32]({ (n) => Left[String, U32](
"Oops! Error. n: " + n.string()) })
.mapL[String]({ (str) => str + "!" })
.either[String]({ (x) => "Left: " + x }, { (y) => "Right: " + y.string() })
Debug(vvv) // => "Left: Oops! Error. n: 13!"
Using pony-stable:
$ stable add github Bannerets/pony-monad
$ stable fetch
Also known as fmap
.
Monad
is a subclass of Functor
.
Also known as bind
, chain
, >>=
.
Also known as foldl
, foldLeft
, reduce
.
Bifunctor
is a subclass of Functor
.
Identity[A](a)
Identity monad.
Implements Monad
, Foldable
.
Unpacks value.
Just[A](a)
Nothing[A]
Implements Monad
, Foldable
.
See also on hackage.
Left[A, B](a)
Right[A, B](b)
Implements Bifunctor
, Monad
, Foldable
.
See also on hackage.
Function list:
fromNullable[T]
- takes(T | None)
and returnsMaybe[T]
try0[R](fn: { (): R ? }): Maybe[R]