Skip to main content

Either

Familiar to those with some Functional Programming knowledge is the Either union composed of two sub-types - Left and Right. The transformable value is that of the Right type and the error value is the constituent of the Left.

either#

either(left, right, either)

either :: (a -> c) -> (b -> c) -> Either a b -> c

Performs case analysis on the Either monad: applies the right function to a Right instance of the either value and a left function to a Left instance of the same value.

Since:

  • bingo-functional - v1.11.0

Arguments:

  • left (function) - function to apply to an instance of Left
  • right (function) - function to apply to an instance of Right
  • either (object) - instance of Either monad
use Chemem\Bingo\Functional\Functors\Monads\Either;
$val = 10;
$result = Either\either(  f\identity,  fn (int $x): int => $x ** 2,  Either::right($val)->filter(fn (int $x): bool => $x % 2 == 0, $val));

fromRight & fromLeft#

fromRight|fromLeft(default, either)

fromRight :: b -> Either a b -> b | fromLeft :: a -> Either a b -> a

Returns the contents of a Right|Left value if present and an arbitrary default value otherwise.

Since:

  • bingo-functional - v1.11.0

Argument(s):

  • default (mixed) - default value
  • either (object) - instance of Either monad
use Chemem\Bingo\Functional\Functors\Monads\Either;
$left   = Either::left(2);$right  = Either::right(5);
echo Either\fromLeft(4, $right);
echo Either\fromRight(25, $left);

lefts & rights#

rights|lefts(eithers)

rights :: [Either a b] -> b | lefts :: [Either a b] -> a

Returns the Left|Right values in a list of Either type objects.

Since:

  • bingo-functional - v1.11.0

Argument(s):

  • eithers (array) - list of Either type objects
use Chemem\Bingo\Functional\Functors\Monads\Either;
$lefts = Either\lefts([  Either::left('foo'),  Either::left('bar'),  Either::right('baz')]);
$rights = Either\rights([  Either::right('foo'),  Either::left('bar'),  Either::right('baz')]);

isRight & isLeft#

isRight(either) | isLeft(either)

isRight :: Either a b -> Bool | isLeft :: Either a b -> Bool

Since:

  • bingo-functional - v1.11.0

Arguments:

  • either (object) - instance of Either monad

The isLeft and isRight functions ascertain if an either object one of either a Left type or Right type.

use Chemem\Bingo\Functional\Functors\Monads\Either;
$left   = Either::left(2);$right  = Either::right(5);
echo Either\isLeft(4, $right);
echo Either\isRight(25, $left);

partitionEithers#

partitionEithers(eithers)

partitionEithers :: [Either a b] -> [[a], [b]]

As is the case with Haskell, the partitionEithers() method transforms an array of Either type items into a multidimensional array of left and right indexed sub-arrays.

Since:

  • bingo-functional - v1.11.0

Arguments:

  • eithers (array) - list of Either type objects
use Chemem\Bingo\Functional\Functors\Monads\Either;
$eithers = Either\partitionEithers([  Either::right(12),  Either::right(10),  Either::left(INF),  Either::left('Invalid Integer')]);