/** * @dataProvider provideData */ public function test_it_should_work_with_maybe($list, $expected) { $addSingleDigit = function ($acc, $i) { return $i > 9 ? m\nothing() : m\just($acc + $i); }; $this->assertEquals($expected, f\foldM($addSingleDigit, 0, $list)->extract()); }
/** * @dataProvider provideData */ public function test_it_should_extract_elements_which_exists_alternative_solution($data) { // $get :: String a -> Maybe [b] -> Maybe b $get = function ($key) { return f\bind(function ($array) use($key) { return isset($array[$key]) ? m\just($array[$key]) : m\nothing(); }); }; $result = Listt::of($data)->map(Maybe\maybeNull)->bind($get('meta'))->bind($get('images'))->bind($get(0)); $this->assertEquals(Listt::of([m\just('//first.jpg'), m\just('//third.jpg'), m\nothing()]), $result); }
public function get($key) { return array_key_exists($key, $this->data) ? Maybe\just($this->data[$key]) : Maybe\nothing(); }
public function test_it_traverse_nothing() { $list = Listt::of([1, 2, 3, 4]); $result = f\traverse('example\\value_is', $list); $this->assertEquals(m\nothing(), $result); }