/** * @inheritdoc * * fs <*> xs = [f x | f <- fs, x <- xs] */ public function ap(FantasyLand\Apply $applicative) { return $this->reduce(function ($accumulator, $value) use($applicative) { /** @var $applicative self */ return f\concatM($accumulator, $applicative->map($value)); }, self::mempty()); }
public function test_foldr() { $list = Listt::of([1, 2, 3, 4]); $result = f\foldr(function ($accumulator, $value) { return f\concatM($accumulator, Listt::of([$value + 1])); }, Listt::of([]), $list); $this->assertEquals($result, Listt::of([5, 4, 3, 2])); }
/** * Generic test to verify if a type obey the monodic laws. * * @param callable $assertEqual Asserting function (Monoid $m1, Monoid $m2, $message) * @param Monoid $x * @param Monoid $y * @param Monoid $z */ public static function test(callable $assertEqual, Monoid $x, Monoid $y, Monoid $z) { $assertEqual(f\concatM($x, f\emptyM($x)), $x, 'Right identity'); $assertEqual(f\concatM(f\emptyM($x), $x), $x, 'Left identity'); $assertEqual(f\concatM($x, f\concatM($y, $z)), f\concatM(f\concatM($x, $y), $z), 'Associativity'); }