The Either type represents values with two possibilities: a value of type
Either a b is either a Left whose value is of type a or a Right whose
value is of type b.
Either a b 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 (Right (Useless)) ? '\u2705 ' :
. Z[k].test (Right (['foo'])) ? '\u2705 * ' :
. /* otherwise */ '\u274C '))
. (S.keys (S.unchecked.filter (isTypeClass) (Z)))
[ 'Setoid ✅ * ', // if ‘a’ and ‘b’ satisfy Setoid
. 'Ord ✅ * ', // if ‘a’ and ‘b’ satisfy Ord
. 'Semigroupoid ❌ ',
. 'Category ❌ ',
. 'Semigroup ✅ * ', // if ‘a’ and ‘b’ satisfy Semigroup
. 'Monoid ❌ ',
. 'Group ❌ ',
. 'Filterable ❌ ',
. 'Functor ✅ ',
. 'Bifunctor ✅ ',
. 'Profunctor ❌ ',
. 'Apply ✅ ',
. 'Applicative ✅ ',
. 'Chain ✅ ',
. 'ChainRec ✅ ',
. 'Monad ✅ ',
. 'Alt ✅ ',
. 'Plus ❌ ',
. 'Alternative ❌ ',
. 'Foldable ✅ ',
. 'Traversable ✅ ',
. 'Extend ✅ ',
. 'Comonad ❌ ',
. 'Contravariant ❌ ' ]Either type representative.
Constructs a value of type Either a b from a value of type a.
> Left ('sqrt undefined for -1')
Left ('sqrt undefined for -1')Constructs a value of type Either a b from a value of type b.
> Right (42)
Right (42)of (Either) (x)is equivalent toRight (x)
> S.of (Either) (42)
Right (42)> Z.chainRec (
. Either,
. (next, done, x) =>
. x <= 1 ? Left ('!!') : Right (x >= 1000 ? done (x) : next (x * x)),
. 1
. )
Left ('!!')
> Z.chainRec (
. Either,
. (next, done, x) =>
. x <= 1 ? Left ('!!') : Right (x >= 1000 ? done (x) : next (x * x)),
. 2
. )
Right (65536)show (Left (x))is equivalent to'Left (' + show (x) + ')'show (Right (x))is equivalent to'Right (' + show (x) + ')'
> show (Left ('sqrt undefined for -1'))
'Left ("sqrt undefined for -1")'
> show (Right ([1, 2, 3]))
'Right ([1, 2, 3])'Left (x)is equal toLeft (y)iffxis equal toyaccording toZ.equalsRight (x)is equal toRight (y)iffxis equal toyaccording toZ.equalsLeft (x)is never equal toRight (y)
> S.equals (Left ([1, 2, 3])) (Left ([1, 2, 3]))
true
> S.equals (Right ([1, 2, 3])) (Right ([1, 2, 3]))
true
> S.equals (Left ([1, 2, 3])) (Right ([1, 2, 3]))
falseLeft (x)is less than or equal toLeft (y)iffxis less than or equal toyaccording toZ.lteRight (x)is less than or equal toRight (y)iffxis less than or equal toyaccording toZ.lteLeft (x)is always less thanRight (y)
> S.filter (S.lte (Left (1))) ([Left (0), Left (1), Left (2)])
[Left (0), Left (1)]
> S.filter (S.lte (Right (1))) ([Right (0), Right (1), Right (2)])
[Right (0), Right (1)]
> S.filter (S.lte (Left (1))) ([Right (0), Right (1), Right (2)])
[]
> S.filter (S.lte (Right (1))) ([Left (0), Left (1), Left (2)])
[Left (0), Left (1), Left (2)]concat (Left (x)) (Left (y))is equivalent toLeft (concat (x) (y))concat (Right (x)) (Right (y))is equivalent toRight (concat (x) (y))concat (Left (x)) (Right (y))is equivalent toRight (y)concat (Right (x)) (Left (y))is equivalent toRight (x)
> S.concat (Left ('abc')) (Left ('def'))
Left ('abcdef')
> S.concat (Right ([1, 2, 3])) (Right ([4, 5, 6]))
Right ([1, 2, 3, 4, 5, 6])
> S.concat (Left ('abc')) (Right ([1, 2, 3]))
Right ([1, 2, 3])
> S.concat (Right ([1, 2, 3])) (Left ('abc'))
Right ([1, 2, 3])map (f) (Left (x))is equivalent toLeft (x)map (f) (Right (x))is equivalent toRight (f (x))
> S.map (S.add (1)) (Left ('sqrt undefined for -1'))
Left ('sqrt undefined for -1')
> S.map (S.add (1)) (Right (99))
Right (100)bimap (f) (g) (Left (x))is equivalent toLeft (f (x))bimap (f) (g) (Right (x))is equivalent toRight (g (x))
> S.bimap (S.toUpper) (S.add (1)) (Left ('abc'))
Left ('ABC')
> S.bimap (S.toUpper) (S.add (1)) (Right (99))
Right (100)ap (Left (x)) (Left (y))is equivalent toLeft (x)ap (Left (x)) (Right (y))is equivalent toLeft (x)ap (Right (f)) (Left (x))is equivalent toLeft (x)ap (Right (f)) (Right (x))is equivalent toRight (f (x))
> S.ap (Left ('div undefined for 0')) (Left ('sqrt undefined for -1'))
Left ('div undefined for 0')
> S.ap (Left ('div undefined for 0')) (Right (99))
Left ('div undefined for 0')
> S.ap (Right (S.add (1))) (Left ('sqrt undefined for -1'))
Left ('sqrt undefined for -1')
> S.ap (Right (S.add (1))) (Right (99))
Right (100)chain (f) (Left (x))is equivalent toLeft (x)chain (f) (Right (x))is equivalent tof (x)
> const sqrt = n => n < 0 ? Left ('sqrt undefined for ' + show (n))
. : Right (Math.sqrt (n))
> S.chain (sqrt) (Left ('div undefined for 0'))
Left ('div undefined for 0')
> S.chain (sqrt) (Right (-1))
Left ('sqrt undefined for -1')
> S.chain (sqrt) (Right (25))
Right (5)alt (Left (y)) (Left (x))is equivalent toLeft (y)alt (Right (y)) (Left (x))is equivalent toRight (y)alt (Left (y)) (Right (x))is equivalent toRight (x)alt (Right (y)) (Right (x))is equivalent toRight (x)
> S.alt (Left ('B')) (Left ('A'))
Left ('B')
> S.alt (Right (1)) (Left ('C'))
Right (1)
> S.alt (Left ('D')) (Right (2))
Right (2)
> S.alt (Right (4)) (Right (3))
Right (3)reduce (f) (x) (Left (y))is equivalent toxreduce (f) (x) (Right (y))is equivalent tof (x) (y)
> S.reduce (S.concat) ([1]) (Left ('sqrt undefined for -1'))
[1]
> S.reduce (S.concat) ([1]) (Right ([2]))
[1, 2]Either#fantasy-land/traverse :: Applicative f => Either a b ~> (TypeRep f, b -> f c) -> f (Either a c)
traverse (A) (f) (Left (x))is equivalent toof (A) (Left (x))traverse (A) (f) (Right (x))is equivalent tomap (Right) (f (x))
> S.traverse (Array) (S.words) (Left ('sqrt undefined for -1'))
[Left ('sqrt undefined for -1')]
> S.traverse (Array) (S.words) (Right ('foo bar baz'))
[Right ('foo'), Right ('bar'), Right ('baz')]extend (f) (Left (x))is equivalent toLeft (x)extend (f) (Right (x))is equivalent toRight (f (Right (x)))
> S.extend (S.reduce (S.add) (1)) (Left ('sqrt undefined for -1'))
Left ('sqrt undefined for -1')
> S.extend (S.reduce (S.add) (1)) (Right (99))
Right (100)