Skip to content
Merged
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
20 changes: 20 additions & 0 deletions src/Data/Traversable.purs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@ import Data.Traversable.Accum.Internal (StateL(..), StateR(..), stateL, stateR)
-- | - `sequence` runs the actions _contained_ in a data structure,
-- | and accumulates the results.
-- |
-- | ```purescript
-- | import Data.Traversable
-- | import Data.Maybe
-- | import Data.Int (fromNumber)
-- |
-- | sequence [Just 1, Just 2, Just 3] == Just [1,2,3]
-- | sequence [Nothing, Just 2, Just 3] == Nothing
-- |
-- | traverse fromNumber [1.0, 2.0, 3.0] == Just [1,2,3]
-- | traverse fromNumber [1.5, 2.0, 3.0] == Nothing
-- |
-- | traverse logShow [1,2,3]
-- | -- prints:
-- | 1
-- | 2
-- | 3
-- |
-- | traverse (\x -> [x, 0]) [1,2,3] == [[1,2,3],[1,2,0],[1,0,3],[1,0,0],[0,2,3],[0,2,0],[0,0,3],[0,0,0]]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this example will be particularly illuminating; it doesn't seem very useful at first glance. I think probably most of the time when I'm using traverse, it's with Effect, Aff, or a monad transformer stack with one of these at the bottom, so I think an effectful example would probably be better. How about something like:

traverse logShow [1,2,3]
-- prints:
1
2
3

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the logShow example.

I think it's also nice to show how traverse can be used for array expansion. Reading code that does this often trips me up because I never expect traverse to do something like this. For example in filterA https://github.com/purescript/purescript-arrays/blob/463dcacb99455de5e28ac4a3312bb795f293d2d9/src/Data/Array.purs#L609-L612

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. I guess it’s good to show a few different examples to illustrate that it can do a whole lot of very different things based on the Applicative instance in use.

-- | ```
-- |
-- | The `traverse` and `sequence` functions should be compatible in the
-- | following sense:
-- |
Expand Down