Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added traverseM #108

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Data/Traversable.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Data.Traversable
( class Traversable, traverse, sequence
, traverseDefault, sequenceDefault
, traverseM
, for
, scanl
, scanr
Expand Down Expand Up @@ -70,6 +71,30 @@ sequenceDefault
-> m (t a)
sequenceDefault = traverse identity

-- | A version of `traverse` where a `join` is applied to the inner result
-- |
-- | For example where `traverse` applied over an array of `Effect`s will produce `Effect (Array (Array _))`
-- | The same array applied to `traverseM` will results in `Effect (Array _)`
-- |
-- | testTraverseM :: Effect Unit
-- | testTraverseM = do
-- | result <- traverseM identity [ arrayZero, arrayOne, arrayTwo, arrayThree ]
-- | assert $ result == [ 1, 2, 2, 3, 3, 3 ]
-- | where
-- | arrayZero = pure [ ]
-- | arrayOne = pure [ 1 ]
-- | arrayTwo = pure [ 2, 2 ]
-- | arrayThree = pure [ 3, 3, 3 ]
traverseM
:: forall a b f t
. Applicative f
=> Traversable t
=> Bind t
=> (a -> f (t b))
-> t a
-> f (t b)
traverseM fn ta = join <$> traverse fn ta

instance traversableArray :: Traversable Array where
traverse = traverseArrayImpl apply map pure
sequence = sequenceDefault
Expand Down
16 changes: 15 additions & 1 deletion test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Data.Int (toNumber, pow)
import Data.Maybe (Maybe(..))
import Data.Monoid.Additive (Additive(..))
import Data.Newtype (unwrap)
import Data.Traversable (class Traversable, sequenceDefault, traverse, sequence, traverseDefault)
import Data.Traversable (class Traversable, sequenceDefault, traverse, traverseM, sequence, traverseDefault)
import Data.TraversableWithIndex (class TraversableWithIndex, traverseWithIndex)
import Effect (Effect, foreachE)
import Effect.Console (log)
Expand Down Expand Up @@ -66,6 +66,9 @@ main = do
log "Test sequenceDefault"
testSequenceDefault 20

log "Test traverseM"
testTraverseM

log "Test foldableWithIndexArray instance"
testFoldableWithIndexArrayWith 20

Expand Down Expand Up @@ -386,6 +389,17 @@ testSequenceDefault :: Int -> Effect Unit
testSequenceDefault = testTraversableFWith (SD <<< arrayFrom1UpTo)


testTraverseM :: Effect Unit
testTraverseM = do
result <- traverseM identity [ arrayZero, arrayOne, arrayTwo, arrayThree ]
assert $ result == [ 1, 2, 2, 3, 3, 3 ]
where
arrayZero = pure [ ]
arrayOne = pure [ 1 ]
arrayTwo = pure [ 2, 2 ]
arrayThree = pure [ 3, 3, 3 ]


-- structure for testing bifoldable, picked `inclusive or` as it has both products and sums

data IOr l r = Both l r | Fst l | Snd r
Expand Down