Data.FoldableWithIndex
#FoldableWithIndex
class (Foldable f) <= FoldableWithIndex i f | f -> i where
A Foldable
with an additional index.
A FoldableWithIndex
instance must be compatible with its Foldable
instance
foldr f = foldrWithIndex (const f)
foldl f = foldlWithIndex (const f)
foldMap f = foldMapWithIndex (const f)
Default implementations are provided by the following functions:
foldrWithIndexDefault
foldlWithIndexDefault
foldMapWithIndexDefaultR
foldMapWithIndexDefaultL
Note: some combinations of the default implementations are unsafe to use together - causing a non-terminating mutually recursive cycle. These combinations are documented per function.
Members
foldrWithIndex :: forall a b. (i -> a -> b -> b) -> b -> f a -> b
foldlWithIndex :: forall a b. (i -> b -> a -> b) -> b -> f a -> b
foldMapWithIndex :: forall a m. Monoid m => (i -> a -> m) -> f a -> m
Instances
#foldrWithIndexDefault
foldrWithIndexDefault :: forall i f a b. FoldableWithIndex i f => (i -> a -> b -> b) -> b -> f a -> b
A default implementation of foldrWithIndex
using foldMapWithIndex
.
Note: when defining a FoldableWithIndex
instance, this function is
unsafe to use in combination with foldMapWithIndexDefaultR
.
#foldlWithIndexDefault
foldlWithIndexDefault :: forall i f a b. FoldableWithIndex i f => (i -> b -> a -> b) -> b -> f a -> b
A default implementation of foldlWithIndex
using foldMapWithIndex
.
Note: when defining a FoldableWithIndex
instance, this function is
unsafe to use in combination with foldMapWithIndexDefaultL
.
#foldMapWithIndexDefaultR
foldMapWithIndexDefaultR :: forall i f a m. FoldableWithIndex i f => Monoid m => (i -> a -> m) -> f a -> m
A default implementation of foldMapWithIndex
using foldrWithIndex
.
Note: when defining a FoldableWithIndex
instance, this function is
unsafe to use in combination with foldrWithIndexDefault
.
#foldMapWithIndexDefaultL
foldMapWithIndexDefaultL :: forall i f a m. FoldableWithIndex i f => Monoid m => (i -> a -> m) -> f a -> m
A default implementation of foldMapWithIndex
using foldlWithIndex
.
Note: when defining a FoldableWithIndex
instance, this function is
unsafe to use in combination with foldlWithIndexDefault
.
#foldWithIndexM
foldWithIndexM :: forall i f m a b. FoldableWithIndex i f => Monad m => (i -> a -> b -> m a) -> a -> f b -> m a
Similar to 'foldlWithIndex', but the result is encapsulated in a monad.
Note: this function is not generally stack-safe, e.g., for monads which
build up thunks a la Eff
.
#traverseWithIndex_
traverseWithIndex_ :: forall i a b f m. Applicative m => FoldableWithIndex i f => (i -> a -> m b) -> f a -> m Unit
Traverse a data structure with access to the index, performing some
effects encoded by an Applicative
functor at each value, ignoring the
final result.
For example:
> traverseWithIndex_ (curry logShow) ["a", "b", "c"]
(Tuple 0 "a")
(Tuple 1 "b")
(Tuple 2 "c")
#forWithIndex_
forWithIndex_ :: forall i a b f m. Applicative m => FoldableWithIndex i f => f a -> (i -> a -> m b) -> m Unit
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:
forWithIndex_ ["a", "b", "c"] \i x -> do
logShow i
log x
#surroundMapWithIndex
surroundMapWithIndex :: forall i f a m. FoldableWithIndex i f => Semigroup m => m -> (i -> a -> m) -> f a -> m
foldMapWithIndex
but with each element surrounded by some fixed value.
For example:
> surroundMapWithIndex "*" (\i x -> show i <> x) []
= "*"
> surroundMapWithIndex "*" (\i x -> show i <> x) ["a"]
= "*0a*"
> surroundMapWithIndex "*" (\i x -> show i <> x) ["a", "b"]
= "*0a*1b*"
> surroundMapWithIndex "*" (\i x -> show i <> x) ["a", "b", "c"]
= "*0a*1b*2c*"
#allWithIndex
allWithIndex :: forall i a b f. FoldableWithIndex i f => HeytingAlgebra b => (i -> a -> b) -> f a -> b
allWithIndex f
is the same as and <<< mapWithIndex f
; map a function over the
structure, and then get the conjunction of the results.
#anyWithIndex
anyWithIndex :: forall i a b f. FoldableWithIndex i f => HeytingAlgebra b => (i -> a -> b) -> f a -> b
anyWithIndex f
is the same as or <<< mapWithIndex f
; map a function over the
structure, and then get the disjunction of the results.
#findWithIndex
findWithIndex :: forall i a f. FoldableWithIndex i f => (i -> a -> Boolean) -> f a -> Maybe { index :: i, value :: a }
Try to find an element in a data structure which satisfies a predicate with access to the index.
#foldrDefault
foldrDefault :: forall i f a b. FoldableWithIndex i f => (a -> b -> b) -> b -> f a -> b
A default implementation of foldr
using foldrWithIndex
#foldlDefault
foldlDefault :: forall i f a b. FoldableWithIndex i f => (b -> a -> b) -> b -> f a -> b
A default implementation of foldl
using foldlWithIndex
#foldMapDefault
foldMapDefault :: forall i f a m. FoldableWithIndex i f => Monoid m => (a -> m) -> f a -> m
A default implementation of foldMap
using foldMapWithIndex
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