/** * Generic test to verify if a type obey the functor laws. * * @param callable $assertEqual Asserting function (Functor $f1, Functor $f2, $message) * @param callable $f (a -> b) * @param callable $g (a -> b) * @param Functor $x f a */ public static function test(callable $assertEqual, callable $f, callable $g, Functor $x) { // identity: fmap id == id $assertEqual(f\map(f\identity, $x), $x, 'identity'); // composition: fmap (f . g) == fmap f . fmap g $assertEqual(f\map(f\compose($f, $g), $x), call_user_func(f\compose(f\map($f), f\map($g)), $x), 'composition'); }
public function test_it_should_sum_all_from_one_list_with_single_element() { // sum <$> [1, 2] <*> [4, 5] $sum = f\curryN(2, 'example\\sum'); $a = Listt::of([1, 2]); $b = Listt::of([4, 5]); $result = f\map($sum, $a)->ap($b); $this->assertEquals(Listt::of([5, 6, 6, 7]), $result); }
/** * @dataProvider provideData */ public function test_it_should_extract_elements_which_exists($data) { // $get :: String a -> [b] -> Maybe b $get = f\curryN(2, function ($key, $array) { return isset($array[$key]) ? m\just($array[$key]) : m\nothing(); }); $listOfFirstImages = f\pipeline(Listt::of, f\map(m\maybeNull), f\bind(f\bind($get('meta'))), f\bind(f\bind($get('images'))), f\bind(f\bind($get(0))), f\join); $result = $listOfFirstImages($data); $this->assertEquals(Listt::of([m\just('//first.jpg'), m\just('//third.jpg'), m\nothing()]), $result); }
/** * @inheritdoc */ public function bind(callable $transformation) { // xs >>= f = concat (map f xs) return self::of(f\concat(f\map($transformation, $this))); }
function handleRequest(array $request) { return call_user_func(f\pipeline('validateInput', f\map('canonizeEmail'), f\bind('updateDatabaseStep'), 'sendMessage'), $request); }