Data.TraversableWithIndex
#TraversableWithIndex
class (FunctorWithIndex i t, FoldableWithIndex i t, Traversable t) <= TraversableWithIndex i t | t -> i where
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
.
Members
traverseWithIndex :: forall a b m. Applicative m => (i -> a -> m b) -> t a -> m (t b)
Instances
#traverseWithIndexDefault
traverseWithIndexDefault :: forall i t a b m. TraversableWithIndex i t => Applicative m => (i -> a -> m b) -> t a -> m (t b)
A default implementation of traverseWithIndex
using sequence
and mapWithIndex
.
#forWithIndex
forWithIndex :: forall i a b m t. Applicative m => TraversableWithIndex i t => t a -> (i -> a -> m b) -> m (t b)
A version of traverseWithIndex
with its arguments flipped.
This can be useful when running an action written using do notation for every element in a data structure:
For example:
for [1, 2, 3] \i x -> do
logShow i
pure (x * x)
#scanlWithIndex
scanlWithIndex :: forall i a b f. TraversableWithIndex i f => (i -> b -> a -> b) -> b -> f a -> f b
Fold a data structure from the left with access to the indices, keeping
all intermediate results instead of only the final result. Note that the
initial value does not appear in the result (unlike Haskell's
Prelude.scanl
).
scanlWithIndex (\i y x -> i + y + x) 0 [1, 2, 3] = [1, 4, 9]
#mapAccumLWithIndex
mapAccumLWithIndex :: forall i a b s f. TraversableWithIndex i f => (i -> s -> a -> Accum s b) -> s -> f a -> Accum s (f b)
Fold a data structure from the left with access to the indices, keeping all intermediate results instead of only the final result.
Unlike scanlWithIndex
, mapAccumLWithIndex
allows the type of accumulator to differ
from the element type of the final data structure.
#scanrWithIndex
scanrWithIndex :: forall i a b f. TraversableWithIndex i f => (i -> a -> b -> b) -> b -> f a -> f b
Fold a data structure from the right with access to the indices, keeping
all intermediate results instead of only the final result. Note that the
initial value does not appear in the result (unlike Haskell's Prelude.scanr
).
scanrWithIndex (\i x y -> i + x + y) 0 [1, 2, 3] = [9, 8, 5]
#mapAccumRWithIndex
mapAccumRWithIndex :: forall i a b s f. TraversableWithIndex i f => (i -> s -> a -> Accum s b) -> s -> f a -> Accum s (f b)
Fold a data structure from the right with access to the indices, keeping all intermediate results instead of only the final result.
Unlike scanrWithIndex
, imapAccumRWithIndex
allows the type of accumulator to differ
from the element type of the final data structure.
#traverseDefault
traverseDefault :: forall i t a b m. TraversableWithIndex i t => Applicative m => (a -> m b) -> t a -> m (t b)
A default implementation of traverse
in terms of traverseWithIndex
Re-exports from Data.Traversable.Accum
Modules
- Control.Alt
- Control.Alternative
- Control.Applicative
- Control.Apply
- Control.Biapplicative
- Control.Biapply
- Control.Bind
- Control.Category
- Control.Comonad
- Control.Extend
- Control.Lazy
- Control.Monad
- Control.Monad.Gen
- Control.Monad.Gen.Class
- Control.Monad.Gen.Common
- Control.Monad.Rec.Class
- Control.Monad.ST
- Control.Monad.ST.Class
- Control.Monad.ST.Global
- Control.Monad.ST.Internal
- Control.Monad.ST.Ref
- Control.MonadPlus
- Control.MonadZero
- Control.Plus
- Control.Semigroupoid
- Data.Array
- Data.Array.NonEmpty
- Data.Array.NonEmpty.Internal
- Data.Array.Partial
- Data.Array.ST
- Data.Array.ST.Iterator
- Data.Array.ST.Partial
- Data.Bifoldable
- Data.Bifunctor
- Data.Bifunctor.Clown
- Data.Bifunctor.Flip
- Data.Bifunctor.Join
- Data.Bifunctor.Joker
- Data.Bifunctor.Product
- Data.Bifunctor.Wrap
- Data.Bitraversable
- Data.Boolean
- Data.BooleanAlgebra
- Data.Bounded
- Data.Char
- Data.Char.Gen
- Data.Char.Utils
- Data.CommutativeRing
- Data.Distributive
- Data.DivisionRing
- Data.Either
- Data.Either.Inject
- Data.Either.Nested
- Data.Enum
- Data.Enum.Gen
- Data.Eq
- Data.EuclideanRing
- Data.Field
- Data.Foldable
- Data.FoldableWithIndex
- Data.Function
- Data.Function.Uncurried
- Data.Functor
- Data.Functor.Invariant
- Data.FunctorWithIndex
- Data.HeytingAlgebra
- Data.Identity
- Data.Int
- Data.Int.Bits
- Data.Maybe
- Data.Maybe.First
- Data.Maybe.Last
- Data.Monoid
- Data.Monoid.Additive
- Data.Monoid.Alternate
- Data.Monoid.Conj
- Data.Monoid.Disj
- Data.Monoid.Dual
- Data.Monoid.Endo
- Data.Monoid.Multiplicative
- Data.NaturalTransformation
- Data.Newtype
- Data.NonEmpty
- Data.Ord
- Data.Ord.Down
- Data.Ord.Max
- Data.Ord.Min
- Data.Ord.Unsafe
- Data.Ordering
- Data.Ring
- Data.Semigroup
- Data.Semigroup.First
- Data.Semigroup.Foldable
- Data.Semigroup.Last
- Data.Semigroup.Traversable
- Data.Semiring
- Data.Show
- Data.String
- Data.String.CaseInsensitive
- Data.String.CodePoints
- Data.String.CodeUnits
- Data.String.Common
- Data.String.Gen
- Data.String.NonEmpty
- Data.String.NonEmpty.CaseInsensitive
- Data.String.NonEmpty.CodePoints
- Data.String.NonEmpty.CodeUnits
- Data.String.NonEmpty.Internal
- Data.String.Pattern
- Data.String.Regex
- Data.String.Regex.Flags
- Data.String.Regex.Unsafe
- Data.String.Unsafe
- Data.String.Utils
- Data.Symbol
- Data.Traversable
- Data.Traversable.Accum
- Data.Traversable.Accum.Internal
- Data.TraversableWithIndex
- Data.Tuple
- Data.Tuple.Nested
- Data.Unfoldable
- Data.Unfoldable1
- Data.Unit
- Data.Void
- Effect
- Effect.Class
- Effect.Class.Console
- Effect.Console
- Effect.Ref
- Effect.Uncurried
- Effect.Unsafe
- Global
- Global.Unsafe
- MCPrelude
- Main
- Math
- PSCI.Support
- Partial
- Partial.Unsafe
- Prelude
- Prim
- Prim.Boolean
- Prim.Ordering
- Prim.Row
- Prim.RowList
- Prim.Symbol
- Prim.TypeError
- Record.Unsafe
- Type.Data.Row
- Type.Data.RowList
- Type.Equality
- Unsafe.Coerce