--- /dev/null
+<?php
+
+namespace example;
+
+use Widmogrod\Primitive\Listt;
+use Widmogrod\Functional as f;
+use Widmogrod\Monad\Maybe as m;
+
+function value_is($x)
+{
+ return $x % 2 == 1 ? m\nothing() : m\just($x);
+}
+
+class ExampleOfTraversableTest extends \PHPUnit_Framework_TestCase
+{
+ public function test_it_traverse_just()
+ {
+ $list = Listt::of([2, 4]);
+ $result = f\traverse('example\value_is', $list);
+
+ $this->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);
+ }
+}
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);
}
/**