Home

Awesome

<a href="https://github.com/fantasyland/fantasy-land"><img alt="Fantasy Land" src="https://raw.githubusercontent.com/fantasyland/fantasy-land/master/logo.png" width="75" height="75" align="left"></a>

sanctuary-maybe

The Maybe type represents optional values: a value of type Maybe a is either Nothing (the empty value) or a Just whose value is of type a.

Maybe a satisfies the following Fantasy Land specifications:

> const Useless = require ('sanctuary-useless')

> const isTypeClass = x =>
.   type (x) === 'sanctuary-type-classes/TypeClass@1'

> S.map (k => k + ' '.repeat (16 - k.length) +
.             (Z[k].test (Just (Useless)) ? '\u2705   ' :
.              Z[k].test (Nothing)        ? '\u2705 * ' :
.              /* otherwise */              '\u274C   '))
.       (S.keys (S.unchecked.filter (isTypeClass) (Z)))
[ 'Setoid          ✅ * ',  // if ‘a’ satisfies Setoid
. 'Ord             ✅ * ',  // if ‘a’ satisfies Ord
. 'Semigroupoid    ❌   ',
. 'Category        ❌   ',
. 'Semigroup       ✅ * ',  // if ‘a’ satisfies Semigroup
. 'Monoid          ✅ * ',  // if ‘a’ satisfies Semigroup
. 'Group           ❌   ',
. 'Filterable      ✅   ',
. 'Functor         ✅   ',
. 'Bifunctor       ❌   ',
. 'Profunctor      ❌   ',
. 'Apply           ✅   ',
. 'Applicative     ✅   ',
. 'Chain           ✅   ',
. 'ChainRec        ✅   ',
. 'Monad           ✅   ',
. 'Alt             ✅   ',
. 'Plus            ✅   ',
. 'Alternative     ✅   ',
. 'Foldable        ✅   ',
. 'Traversable     ✅   ',
. 'Extend          ✅   ',
. 'Comonad         ❌   ',
. 'Contravariant   ❌   ' ]

<a name="Maybe" href="https://github.com/sanctuary-js/sanctuary-maybe/blob/v2.1.0/index.js#L147">Maybe :: TypeRep Maybe</a>

Maybe type representative.

<a name="Maybe.Nothing" href="https://github.com/sanctuary-js/sanctuary-maybe/blob/v2.1.0/index.js#L151">Maybe.Nothing :: Maybe a</a>

The empty value of type Maybe a.

> Nothing
Nothing

<a name="Maybe.Just" href="https://github.com/sanctuary-js/sanctuary-maybe/blob/v2.1.0/index.js#L161">Maybe.Just :: a -⁠> Maybe a</a>

Constructs a value of type Maybe a from a value of type a.

> Just (42)
Just (42)

<a name="Maybe.fantasy-land/empty" href="https://github.com/sanctuary-js/sanctuary-maybe/blob/v2.1.0/index.js#L184">Maybe.fantasy-land/empty :: () -⁠> Maybe a</a>

> S.empty (Maybe)
Nothing

<a name="Maybe.fantasy-land/of" href="https://github.com/sanctuary-js/sanctuary-maybe/blob/v2.1.0/index.js#L194">Maybe.fantasy-land/of :: a -⁠> Maybe a</a>

> S.of (Maybe) (42)
Just (42)

<a name="Maybe.fantasy-land/chainRec" href="https://github.com/sanctuary-js/sanctuary-maybe/blob/v2.1.0/index.js#L207">Maybe.fantasy-land/chainRec :: ((a -⁠> c, b -⁠> c, a) -⁠> Maybe c, a) -⁠> Maybe b</a>

> Z.chainRec (
.   Maybe,
.   (next, done, x) =>
.     x <= 1 ? Nothing : Just (x >= 1000 ? done (x) : next (x * x)),
.   1
. )
Nothing

> Z.chainRec (
.   Maybe,
.   (next, done, x) =>
.     x <= 1 ? Nothing : Just (x >= 1000 ? done (x) : next (x * x)),
.   2
. )
Just (65536)

<a name="Maybe.fantasy-land/zero" href="https://github.com/sanctuary-js/sanctuary-maybe/blob/v2.1.0/index.js#L236">Maybe.fantasy-land/zero :: () -⁠> Maybe a</a>

> S.zero (Maybe)
Nothing

<a name="Maybe.prototype.@@show" href="https://github.com/sanctuary-js/sanctuary-maybe/blob/v2.1.0/index.js#L246">Maybe#@@show :: Showable a => Maybe a ~> () -⁠> String</a>

> show (Nothing)
'Nothing'

> show (Just (['foo', 'bar', 'baz']))
'Just (["foo", "bar", "baz"])'

<a name="Maybe.prototype.fantasy-land/equals" href="https://github.com/sanctuary-js/sanctuary-maybe/blob/v2.1.0/index.js#L265">Maybe#fantasy-land/equals :: Setoid a => Maybe a ~> Maybe a -⁠> Boolean</a>

> S.equals (Nothing) (Nothing)
true

> S.equals (Just ([1, 2, 3])) (Just ([1, 2, 3]))
true

> S.equals (Just ([1, 2, 3])) (Just ([3, 2, 1]))
false

> S.equals (Just ([1, 2, 3])) (Nothing)
false

<a name="Maybe.prototype.fantasy-land/lte" href="https://github.com/sanctuary-js/sanctuary-maybe/blob/v2.1.0/index.js#L292">Maybe#fantasy-land/lte :: Ord a => Maybe a ~> Maybe a -⁠> Boolean</a>

> S.filter (S.lte (Nothing)) ([Nothing, Just (0), Just (1), Just (2)])
[Nothing]

> S.filter (S.lte (Just (1))) ([Nothing, Just (0), Just (1), Just (2)])
[Nothing, Just (0), Just (1)]

<a name="Maybe.prototype.fantasy-land/concat" href="https://github.com/sanctuary-js/sanctuary-maybe/blob/v2.1.0/index.js#L313">Maybe#fantasy-land/concat :: Semigroup a => Maybe a ~> Maybe a -⁠> Maybe a</a>

> S.concat (Nothing) (Nothing)
Nothing

> S.concat (Just ([1, 2, 3])) (Just ([4, 5, 6]))
Just ([1, 2, 3, 4, 5, 6])

> S.concat (Nothing) (Just ([1, 2, 3]))
Just ([1, 2, 3])

> S.concat (Just ([1, 2, 3])) (Nothing)
Just ([1, 2, 3])

<a name="Maybe.prototype.fantasy-land/filter" href="https://github.com/sanctuary-js/sanctuary-maybe/blob/v2.1.0/index.js#L341">Maybe#fantasy-land/filter :: Maybe a ~> (a -⁠> Boolean) -⁠> Maybe a</a>

> S.filter (isFinite) (Nothing)
Nothing

> S.filter (isFinite) (Just (Infinity))
Nothing

> S.filter (isFinite) (Just (Number.MAX_SAFE_INTEGER))
Just (9007199254740991)

<a name="Maybe.prototype.fantasy-land/map" href="https://github.com/sanctuary-js/sanctuary-maybe/blob/v2.1.0/index.js#L363">Maybe#fantasy-land/map :: Maybe a ~> (a -⁠> b) -⁠> Maybe b</a>

> S.map (Math.sqrt) (Nothing)
Nothing

> S.map (Math.sqrt) (Just (9))
Just (3)

<a name="Maybe.prototype.fantasy-land/ap" href="https://github.com/sanctuary-js/sanctuary-maybe/blob/v2.1.0/index.js#L382">Maybe#fantasy-land/ap :: Maybe a ~> Maybe (a -⁠> b) -⁠> Maybe b</a>

> S.ap (Nothing) (Nothing)
Nothing

> S.ap (Nothing) (Just (9))
Nothing

> S.ap (Just (Math.sqrt)) (Nothing)
Nothing

> S.ap (Just (Math.sqrt)) (Just (9))
Just (3)

<a name="Maybe.prototype.fantasy-land/chain" href="https://github.com/sanctuary-js/sanctuary-maybe/blob/v2.1.0/index.js#L409">Maybe#fantasy-land/chain :: Maybe a ~> (a -⁠> Maybe b) -⁠> Maybe b</a>

> const head = xs => xs.length === 0 ? Nothing : Just (xs[0])

> S.chain (head) (Nothing)
Nothing

> S.chain (head) (Just ([]))
Nothing

> S.chain (head) (Just (['foo', 'bar', 'baz']))
Just ('foo')

<a name="Maybe.prototype.fantasy-land/alt" href="https://github.com/sanctuary-js/sanctuary-maybe/blob/v2.1.0/index.js#L433">Maybe#fantasy-land/alt :: Maybe a ~> Maybe a -⁠> Maybe a</a>

> S.alt (Nothing) (Nothing)
Nothing

> S.alt (Just (1)) (Nothing)
Just (1)

> S.alt (Nothing) (Just (2))
Just (2)

> S.alt (Just (4)) (Just (3))
Just (3)

<a name="Maybe.prototype.fantasy-land/reduce" href="https://github.com/sanctuary-js/sanctuary-maybe/blob/v2.1.0/index.js#L460">Maybe#fantasy-land/reduce :: Maybe a ~> ((b, a) -⁠> b, b) -⁠> b</a>

> S.reduce (S.concat) ('abc') (Nothing)
'abc'

> S.reduce (S.concat) ('abc') (Just ('xyz'))
'abcxyz'

<a name="Maybe.prototype.fantasy-land/traverse" href="https://github.com/sanctuary-js/sanctuary-maybe/blob/v2.1.0/index.js#L479">Maybe#fantasy-land/traverse :: Applicative f => Maybe a ~> (TypeRep f, a -⁠> f b) -⁠> f (Maybe b)</a>

> S.traverse (Array) (S.words) (Nothing)
[Nothing]

> S.traverse (Array) (S.words) (Just ('foo bar baz'))
[Just ('foo'), Just ('bar'), Just ('baz')]

<a name="Maybe.prototype.fantasy-land/extend" href="https://github.com/sanctuary-js/sanctuary-maybe/blob/v2.1.0/index.js#L498">Maybe#fantasy-land/extend :: Maybe a ~> (Maybe a -⁠> b) -⁠> Maybe b</a>

> S.extend (S.reduce (S.add) (1)) (Nothing)
Nothing

> S.extend (S.reduce (S.add) (1)) (Just (99))
Just (100)