From 0bfd8954a9e6e4e893fdab89f38f7cb258dbc588 Mon Sep 17 00:00:00 2001 From: widmogrod Date: Mon, 24 Oct 2016 00:45:04 +0200 Subject: [PATCH] Slowly introduce listt instead of native php array --- src/Functional/functions.php | 16 +++++++++------- test/Functional/FoldrTest.php | 23 +++++++++++++++++++++++ test/Functional/ReduceTest.php | 23 +++++++++++++++++++++++ 3 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 test/Functional/FoldrTest.php create mode 100644 test/Functional/ReduceTest.php diff --git a/src/Functional/functions.php b/src/Functional/functions.php index c7571e9..25eea0a 100644 --- a/src/Functional/functions.php +++ b/src/Functional/functions.php @@ -8,8 +8,8 @@ use Widmogrod\FantasyLand\Foldable; use Widmogrod\FantasyLand\Functor; use Widmogrod\FantasyLand\Monad; use Widmogrod\FantasyLand\Traversable; -use Widmogrod\Primitive\Listt; use Widmogrod\Monad\Identity; +use Widmogrod\Primitive\Listt; /** * @var callable @@ -465,7 +465,9 @@ function foldr(callable $callable, $accumulator = null, Foldable $foldable = nul return reduce( $callable, $accumulator, - toFoldable(reduce(flip(append), [], $foldable)) + reduce(function ($accumulator, $value) { + return concatM(Listt::of([$value]), $accumulator); + }, Listt::of([]), $foldable) ); }), func_get_args()); } @@ -821,8 +823,8 @@ const traverse = 'Widmogrod\Functional\traverse'; * * Map each element of a structure to an action, evaluate these actions from left to right, and collect the results * - * @param callable $transformation (a -> f b) - * @param Traversable $t t a + * @param callable $transformation (a -> f b) + * @param Traversable $t t a * * @return Applicative f (t b) */ @@ -856,7 +858,7 @@ function sequence($monads) /** * filterM :: Monad m => (a -> m Bool) -> [a] -> m [a] * - * @param callable $f (a -> m Bool) + * @param callable $f (a -> m Bool) * @param array|Traversable $collection [a] * * @return Monad m [a] @@ -896,8 +898,8 @@ function filterM(callable $f, $collection) /** * foldM :: Monad m => (a -> b -> m a) -> a -> [b] -> m a * - * @param callable $f (a -> b -> m a) - * @param mixed $initial a + * @param callable $f (a -> b -> m a) + * @param mixed $initial a * @param array|\Traversable $collection [b] * * @return mixed m a diff --git a/test/Functional/FoldrTest.php b/test/Functional/FoldrTest.php new file mode 100644 index 0000000..a4cac53 --- /dev/null +++ b/test/Functional/FoldrTest.php @@ -0,0 +1,23 @@ +assertEquals( + $result, + Listt::of([5, 4, 3, 2]) + ); + } +} diff --git a/test/Functional/ReduceTest.php b/test/Functional/ReduceTest.php new file mode 100644 index 0000000..de4aaeb --- /dev/null +++ b/test/Functional/ReduceTest.php @@ -0,0 +1,23 @@ +assertEquals( + $result, + Listt::of([2, 3, 4, 5]) + ); + } +} -- 2.11.0