public function testFlatMapForRightNothing() { // create a MaybeT that represents an `Either Maybe` $mt = new MaybeT(Either::fromValue(Maybe::nothing())); $newMt = $mt->flatMap('firstLetter'); $this->assertEquals($newMt, $mt); }
public function testEitherToMaybeForLeft() { $left = Either::left("I am Error!"); $conversionVisitor = new EitherToMaybe(); $actualMaybe = $left->accept($conversionVisitor); $expectedMaybe = Maybe::nothing(); $this->assertEquals($expectedMaybe, $actualMaybe); }
public function testMapForNothing() { $maybeInt = Maybe::nothing(); $maybeIntPlusOne = $maybeInt->map(function ($i) { return addOne($i); }); $this->assertInstanceOf('TMciver\\Functional\\Maybe\\Nothing', $maybeIntPlusOne); }
public function testOrElseForNothing() { $maybe = Maybe::nothing(''); $maybeNoException = $maybe->orElse(function () { throw new \Exception('I should be caught!'); }, []); $this->assertInstanceOf(Nothing::class, $maybeNoException); }
public function testMaybeToEitherForNothing() { $nothing = Maybe::nothing("I am Error!"); $conversionVisitor = new MaybeToEither("Hey, I was Nothing but now I'm Left!"); $actualEither = $nothing->accept($conversionVisitor); $expectedEither = Either::left("Hey, I was Nothing but now I'm Left!"); $this->assertEquals($expectedEither, $actualEither); }
public function testApplicativeForNothing() { // The function $maybeFn = Maybe::nothing('There is no function!'); // The args $maybeStr = Maybe::fromValue('Hello world!'); $maybeStart = Maybe::fromValue(6); $maybeLength = Maybe::fromValue(5); // Apply the function applicatively $maybeResult = $maybeFn($maybeStr, $maybeStart, $maybeLength); $expectedResult = $maybeFn; $this->assertEquals($expectedResult, $maybeResult); }
private function head($array) { if (is_array($array)) { if (count($array) > 0) { $vals = array_values($array); $h = Maybe::fromValue($vals[0]); } else { $h = Maybe::nothing(); } } else { $h = Maybe::nothing(); } return $h; }
public function fail($msg) { return new MaybeT($this->monad->pure(Maybe::nothing())); }
/** * Function for doing double dispatch from Just::append. */ protected abstract function appendJust($just); /** * @param $default The default value to return if this Maybe is an instance * of Nothing. * @return The contained value if this Maybe is an instance of Just, or the * default value otherwise. */ public abstract function getOrElse($default); /** * Calls the Callable $f (or not) in a sub-class-specific way. * @param Callable $f the Callable to (optionally) call. This callable * should be a function of a number of arguments equal to the number of * array elements given in $args and should return a sub-class of Maybe. * @param array $args The arguments to pass to the Callable $f. The array * length must be equal to the number of arguments expected by $f. * @return An instance of Maybe. */ abstract function orElse(callable $f, array $args); /** * @return True if this object is an instance of the Nothing class; false * otherwise. */ abstract function isNothing(); abstract function accept($maybeVisitor); } // initialize Maybe::$nothing Maybe::$nothing = new Nothing();
function visitLeft($left) { return Maybe::nothing(); }