From 819b2220028466fdd281c0d994d8b72569829dfd Mon Sep 17 00:00:00 2001 From: widmogrod Date: Mon, 24 Oct 2016 00:42:08 +0200 Subject: [PATCH] Fix traversable issue #30 --- example/ExampleOfTraversableTest.php | 31 +++++++++++++++++++++++++++++++ src/Primitive/Listt.php | 8 ++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 example/ExampleOfTraversableTest.php diff --git a/example/ExampleOfTraversableTest.php b/example/ExampleOfTraversableTest.php new file mode 100644 index 0000000..d85a2fb --- /dev/null +++ b/example/ExampleOfTraversableTest.php @@ -0,0 +1,31 @@ +assertEquals(m\just([2, 4]), $result); + } + + 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); + } +} diff --git a/src/Primitive/Listt.php b/src/Primitive/Listt.php index 6f12fdf..1e65217 100644 --- a/src/Primitive/Listt.php +++ b/src/Primitive/Listt.php @@ -99,8 +99,12 @@ class Listt implements public function traverse(callable $transformation) { return f\foldr(function ($ys, $x) use ($transformation) { - return call_user_func($transformation, $x)->map(f\append)->ap($ys); - }, self::of([]), $this); + $functor = $transformation($x); + + return $functor + ->map(f\append) + ->ap($ys ? $ys : $functor::of([])); // https://github.com/widmogrod/php-functional/issues/30 + }, false, $this); } /** -- 2.11.0