public function testFlatMapForNothing() { $maybeInt = Maybe::nothing(); $maybeIntPlusOne = $maybeInt->flatMap(function ($i) { return maybeAddOne($i); }); $this->assertInstanceOf('TMciver\\Functional\\Maybe\\Nothing', $maybeIntPlusOne); }
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 testMapNullHandling() { $maybeInt = Maybe::fromValue(1); $maybeIntPlusOne = $maybeInt->map(function ($i) { return returnNull($i); }); $this->assertInstanceOf('TMciver\\Functional\\Maybe\\Nothing', $maybeIntPlusOne); }
public function testStaticMethodCall() { $maybe = Maybe::fromValue("Hello world!"); $f = ['\\TMciver\\Functional\\Test\\StaticClass', 'toUpperCase']; $maybeUpperCase = $maybe->map($f); $expexted = Maybe::fromValue("HELLO WORLD!"); $this->assertEquals($expexted, $maybeUpperCase); }
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 testPure() { // create a MaybeT that represents an `Either Maybe` $mt = new MaybeT(Either::left("I am Error.")); // create a pure value $newMt = $mt->pure("Hello!"); $expectedMt = new MaybeT(Either::fromValue(Maybe::fromValue("Hello!"))); $this->assertEquals($newMt, $expectedMt); }
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 testAssociativity() { // we'll combine four values in two different ways and make sure the // results are the same. $just1 = Maybe::fromValue(1); $just2 = Maybe::fromValue(2); $just3 = Maybe::fromValue(3); $just4 = Maybe::fromValue(4); // First, combine the first two, then the last two and finally combine // the two results. $firstTwo = $just1->append($just2); $secondTwo = $just3->append($just4); $result1 = $firstTwo->append($secondTwo); // Next, combine the first two, then combine that result with the third // and then combine that result with the fourth. $firstThree = $firstTwo->append($just3); $result2 = $firstThree->append($just4); $this->assertEquals($result1, $result2); }
public function pure($val) { return new MaybeT($this->monad->pure(Maybe::fromValue($val))); }
/** * 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 visitRight($right) { return Maybe::fromValue($right->get()); }
protected function appendJust($just) { // this is where the real work of appending two Just's is done. // Since we can't know if the value contained in a Maybe is itself a // monoid, we're just going to put the values in an array. But there are // four cases that we have to account for to create the proper result // array so that associativity is maintained. $leftVal = $just->val; $rightVal = $this->val; if (!is_array($leftVal) && !is_array($rightVal)) { $resultArray = [$leftVal, $rightVal]; } else { if (is_array($leftVal) && !is_array($rightVal)) { $leftVal[] = $rightVal; $resultArray = $leftVal; } else { if (!is_array($leftVal) && is_array($rightVal)) { array_unshift($rightVal, $leftVal); $resultArray = $rightVal; } else { // both values are arrays $resultArray = array_merge($leftVal, $rightVal); } } } return Maybe::fromValue($resultArray); }