Search results
Traversable
represents data structures which can be traversed,
accumulating results and effects in some Applicative
functor.
traverse
runs an action for every element in a data structure, and accumulates the results.sequence
runs the actions contained in a data structure, and accumulates the results.
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]]
The traverse
and sequence
functions should be compatible in the
following sense:
traverse f xs = sequence (f <$> xs)
sequence = traverse identity
Traversable
instances should also be compatible with the corresponding
Foldable
instances, in the following sense:
foldMap f = runConst <<< traverse (Const <<< f)
Default implementations are provided by the following functions:
traverseDefault
sequenceDefault
Traversable1
represents data structures with a minimum of one element that can be traversed,
accumulating results and effects in some Applicative
functor.
traverse1
runs an action for every element in a data structure, and accumulates the results.sequence1
runs the actions contained in a data structure, and accumulates the results.
The traverse1
and sequence1
functions should be compatible in the
following sense:
traverse1 f xs = sequence1 (f <$> xs)
sequence1 = traverse1 identity
Traversable1
instances should also be compatible with the corresponding
Foldable1
instances, in the following sense:
foldMap1 f = runConst <<< traverse1 (Const <<< f)
Default implementations are provided by the following functions:
traverse1Default
sequence1Default
A Traversable
with an additional index.
A TraversableWithIndex
instance must be compatible with its
Traversable
instance
traverse f = traverseWithIndex (const f)
with its FoldableWithIndex
instance
foldMapWithIndex f = unwrap <<< traverseWithIndex (\i -> Const <<< f i)
and with its FunctorWithIndex
instance
mapWithIndex f = unwrap <<< traverseWithIndex (\i -> Identity <<< f i)
A default implementation is provided by traverseWithIndexDefault
.