HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Moduleoptics-core-0.4.1.1Haskell2010

Optics.IxFold

An IxFold is an indexed version of a Fold. See the "Indexed optics" section of the overview documentation in the Optics module of the main optics package for more details on indexed optics.

  • 2 types
  • 1 class
  • 22 values

Formation

1 declaration

Introduction

1 declaration

Elimination

6 declarations
valueitoListOf
  1. :: (Is k A_Fold, HasSingleIndex is i)
  2. => Optic' k is s a
  3. -> s
  4. -> [(i, a)]
#

Fold with index to a list.

Example1 expression
itoListOf (folded % ifolded) ["abc", "def"][(0,'a'),(1,'b'),(2,'c'),(0,'d'),(1,'e'),(2,'f')]

Note: currently indexed optics can be used as non-indexed.

Example1 expression
toListOf (folded % ifolded) ["abc", "def"]"abcdef"

Additional introduction forms

3 declarations
valueifolding :: FoldableWithIndex i f => (s -> f a) -> IxFold i s a
#

Obtain an IxFold by lifting an operation that returns a FoldableWithIndex result.

This can be useful to lift operations from Data.List and elsewhere into an IxFold.

Example1 expression
itoListOf (ifolding words) "how are you"[(0,"how"),(1,"are"),(2,"you")]
valueifoldring
  1. :: forall (f :: Type -> Type). Applicative f => (i -> a -> f u -> f u) -> f v -> s -> f w
  2. -> IxFold i s a
#

Obtain an IxFold by lifting ifoldr like function.

Example1 expression
itoListOf (ifoldring ifoldr) "hello"[(0,'h'),(1,'e'),(2,'l'),(3,'l'),(4,'o')]

Additional elimination forms

7 declarations

See also toMapOf, which constructs a Data.Map.Map from an IxFold.

valueifindOf
  1. :: (Is k A_Fold, HasSingleIndex is i)
  2. => Optic' k is s a
  3. -> i -> a -> Bool
  4. -> s
  5. -> Maybe (i, a)
#

The ifindOf function takes an IxFold, a predicate that is also supplied the index, a structure and returns the left-most element of the structure along with its index matching the predicate, or Nothing if there is no such element.

When you don't need access to the index then findOf is more flexible in what it accepts.

valueifindMOf
  1. :: (Is k A_Fold, Monad m, HasSingleIndex is i)
  2. => Optic' k is s a
  3. -> i -> a -> m Bool
  4. -> s
  5. -> m (Maybe (i, a))
#

The ifindMOf function takes an IxFold, a monadic predicate that is also supplied the index, a structure and returns in the monad the left-most element of the structure matching the predicate, or Nothing if there is no such element.

When you don't need access to the index then findMOf is more flexible in what it accepts.

Combinators

3 declarations

Monoid structures

2 declarations

IxFold admits (at least) two monoid structures:

  • isumming concatenates results from both folds.

  • ifailing returns results from the second fold only if the first returns no results.

In both cases, the identity element of the monoid is ignored, which returns no results.

There is no Semigroup or Monoid instance for IxFold, because there is not a unique choice of monoid to use, and the (<>) operator could not be used to combine optics of different kinds.

valueifailing
  1. :: (Is k A_Fold, Is l A_Fold, HasSingleIndex is1 i, HasSingleIndex is2 i)
  2. => Optic' k is1 s a
  3. -> Optic' l is2 s a
  4. -> IxFold i s a
#

Try the first IxFold. If it returns no entries, try the second one.

Example2 expressions
itoListOf (_1 % ifolded `ifailing` _2 % ifolded) (["a"], ["b","c"])[(0,"a")]itoListOf (_1 % ifolded `ifailing` _2 % ifolded) ([], ["b","c"])[(0,"b"),(1,"c")]

Subtyping

1 declaration
datadata A_Fold
#

Tag for a fold.

Instances28Is, JoinKinds, IxOptic, ToReadOnly, ReadOnlyOptic, …

Re-exports

1 declaration
classclass Foldable f => FoldableWithIndex i (f :: Type -> Type) | f -> i where
#

A container that supports folding with an additional index.

Methods

  • ifoldMap :: Monoid m => (i -> a -> m) -> f a -> m

    Fold a container by mapping value to an arbitrary Monoid with access to the index i.

    When you don't need access to the index then foldMap is more flexible in what it accepts.

    foldMap ≡ ifoldMap . const
    
  • ifoldMap' :: Monoid m => (i -> a -> m) -> f a -> m

    A variant of ifoldMap that is strict in the accumulator.

    When you don't need access to the index then foldMap' is more flexible in what it accepts.

    foldMap' ≡ ifoldMap' . const
    
  • ifoldr :: (i -> a -> b -> b) -> b -> f a -> b

    Right-associative fold of an indexed container with access to the index i.

    When you don't need access to the index then foldr is more flexible in what it accepts.

    foldr ≡ ifoldr . const
    
  • ifoldl :: (i -> b -> a -> b) -> b -> f a -> b

    Left-associative fold of an indexed container with access to the index i.

    When you don't need access to the index then foldl is more flexible in what it accepts.

    foldl ≡ ifoldl . const
    
  • ifoldr' :: (i -> a -> b -> b) -> b -> f a -> b

    Strictly fold right over the elements of a structure with access to the index i.

    When you don't need access to the index then foldr' is more flexible in what it accepts.

    foldr' ≡ ifoldr' . const
    
  • ifoldl' :: (i -> b -> a -> b) -> b -> f a -> b

    Fold over the elements of a structure with an index, associating to the left, but strictly.

    When you don't need access to the index then foldlOf' is more flexible in what it accepts.

    foldl' l ≡ ifoldl' l . const
    
Instances28FoldableWithIndex, …