From 0ec132e92f5f47602db9728d685a211fc1ec452d Mon Sep 17 00:00:00 2001 From: Matthew Leon Date: Wed, 7 Jun 2017 14:19:31 +0100 Subject: [PATCH] traverseE --- .bowerrc | 5 +++++ bower.json | 6 +++++- src/Control/Monad/Eff.js | 16 ++++++++++++++++ src/Control/Monad/Eff.purs | 17 ++++++++++++++++- test/Main.purs | 11 +++++++++++ 5 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 .bowerrc create mode 100644 test/Main.purs diff --git a/.bowerrc b/.bowerrc new file mode 100644 index 0000000..6a3b1d5 --- /dev/null +++ b/.bowerrc @@ -0,0 +1,5 @@ +{ + "ignoredDependencies": [ + "purescript-eff" + ] +} diff --git a/bower.json b/bower.json index b92f6fd..00d5a6a 100644 --- a/bower.json +++ b/bower.json @@ -17,6 +17,10 @@ "package.json" ], "dependencies": { - "purescript-prelude": "^3.0.0" + "purescript-prelude": "^3.0.0", + "purescript-foldable-traversable": "^3.3.0" + }, + "devDependencies": { + "purescript-console": "^3.0.0" } } diff --git a/src/Control/Monad/Eff.js b/src/Control/Monad/Eff.js index 851d4ca..b397ee1 100644 --- a/src/Control/Monad/Eff.js +++ b/src/Control/Monad/Eff.js @@ -57,3 +57,19 @@ exports.foreachE = function (as) { }; }; }; + +exports.traverseEImpl = function (foldl) { + return function (xs) { + return function (f) { + var call = function() { + return function(x) { + f(x)(); + }; + }; + + return function () { + foldl(call)()(xs); + }; + }; + }; +}; diff --git a/src/Control/Monad/Eff.purs b/src/Control/Monad/Eff.purs index 1149c3a..e487603 100644 --- a/src/Control/Monad/Eff.purs +++ b/src/Control/Monad/Eff.purs @@ -3,7 +3,7 @@ module Control.Monad.Eff , Eff , Pure , runPure - , untilE, whileE, forE, foreachE + , untilE, whileE, forE, foreachE, traverseE ) where import Control.Applicative (class Applicative, liftA1) @@ -12,6 +12,7 @@ import Control.Bind (class Bind) import Control.Monad (class Monad, ap) import Data.Functor (class Functor) +import Data.Foldable (class Foldable, foldl) import Data.Unit (Unit) -- | The kind of all effect types. @@ -87,3 +88,17 @@ foreign import forE :: forall e. Int -> Int -> (Int -> Eff e Unit) -> Eff e Unit -- | `foreach xs f` runs the computation returned by the function `f` for each -- | of the inputs `xs`. foreign import foreachE :: forall e a. Array a -> (a -> Eff e Unit) -> Eff e Unit + +-- | Loop over a Foldable collection of values. +-- | +-- | `traverseE xs f` runs the computation returned by the function `f` for each +-- | of the inputs `xs`. +traverseE :: forall e a f. Foldable f => f a -> (a -> Eff e Unit) -> Eff e Unit +traverseE = traverseEImpl foldl + +foreign import traverseEImpl + :: forall e a f + . (forall b. (b -> a -> b) -> b -> f a -> b) + -> f a + -> (a -> Eff e Unit) + -> Eff e Unit diff --git a/test/Main.purs b/test/Main.purs new file mode 100644 index 0000000..9de9246 --- /dev/null +++ b/test/Main.purs @@ -0,0 +1,11 @@ +module Test.Main where + +import Prelude +import Control.Monad.Eff (Eff, foreachE, traverseE) +import Control.Monad.Eff.Console (CONSOLE, log) + +main :: forall eff. Eff (console :: CONSOLE | eff) Unit +main = do + let foobar = ["foo", "bar", "bam"] + foreachE foobar log + traverseE foobar log