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.Fold

A Fold S A has the ability to extract some number of elements of type A from a container of type S. For example, toListOf can be used to obtain the contained elements as a list. Unlike a Traversal, there is no way to set or update elements.

This can be seen as a generalisation of traverse_, where the type S does not need to be a type constructor with A as the last parameter.

A close relative is the AffineFold, which is a Fold that contains at most one element.

  • 2 types
  • 43 values

Formation

1 declaration

Introduction

1 declaration

Elimination

8 declarations
valuefoldlOf' :: Is k A_Fold => Optic' k is s a -> (r -> a -> r) -> r -> s -> r
#

Fold left-associatively, and strictly.

valuetoListOf :: Is k A_Fold => Optic' k is s a -> s -> [a]
#

Fold to a list.

Example1 expression
toListOf (_1 % folded % _Right) ([Right 'h', Left 5, Right 'i'], "bye")"hi"

Computation

0 declarations
traverseOf_ (foldVL f) ≡ f

Additional introduction forms

4 declarations
valuefolding :: Foldable f => (s -> f a) -> Fold s a
#

Obtain a Fold by lifting an operation that returns a Foldable result.

This can be useful to lift operations from Data.List and elsewhere into a Fold.

Example1 expression
toListOf (folding tail) [1,2,3,4][2,3,4]
valuefoldring
  1. :: forall (f :: Type -> Type). Applicative f => (a -> f u -> f u) -> f v -> s -> f w
  2. -> Fold s a
#

Obtain a Fold by lifting foldr like function.

Example1 expression
toListOf (foldring foldr) [1,2,3,4][1,2,3,4]
valueunfolded :: (s -> Maybe (a, s)) -> Fold s a
#

Build a Fold that unfolds its values from a seed.

Prelude.unfoldr ≡ toListOf . unfolded
Example1 expression
toListOf (unfolded $ \b -> if b == 0 then Nothing else Just (b, b - 1)) 10[10,9,8,7,6,5,4,3,2,1]

Additional elimination forms

26 declarations

See also setOf, which constructs a Set from a Fold.

valuehas :: Is k A_Fold => Optic' k is s a -> s -> Bool
#

Check to see if this optic matches 1 or more entries.

Example1 expression
has _Left (Left 12)True
Example1 expression
has _Right (Left 12)False

This will always return True for a Lens or Getter.

Example1 expression
has _1 ("hello","world")True
valuehasn't :: Is k A_Fold => Optic' k is s a -> s -> Bool
#

Check to see if this Fold or Traversal has no matches.

Example1 expression
hasn't _Left (Right 12)True
Example1 expression
hasn't _Left (Left 12)False
valueheadOf :: Is k A_Fold => Optic' k is s a -> s -> Maybe a
#

Retrieve the first entry of a Fold.

Example1 expression
headOf folded [1..10]Just 1
Example1 expression
headOf each (1,2)Just 1
valuelastOf :: Is k A_Fold => Optic' k is s a -> s -> Maybe a
#

Retrieve the last entry of a Fold.

Example1 expression
lastOf folded [1..10]Just 10
Example1 expression
lastOf each (1,2)Just 2
valueanyOf :: Is k A_Fold => Optic' k is s a -> (a -> Bool) -> s -> Bool
#

Returns True if any target of a Fold satisfies a predicate.

Example1 expression
anyOf each (=='x') ('x','y')True
valuenoneOf :: Is k A_Fold => Optic' k is s a -> (a -> Bool) -> s -> Bool
#

Returns True only if no targets of a Fold satisfy a predicate.

Example2 expressions
noneOf each (not . isn't _Nothing) (Just 3, Just 4, Just 5)TruenoneOf (folded % folded) (<10) [[13,99,20],[3,71,42]]False
valuesumOf :: (Is k A_Fold, Num a) => Optic' k is s a -> s -> a
#

Calculate the Sum of every number targeted by a Fold.

Example3 expressions
sumOf each (5,6)11sumOf folded [1,2,3,4]10sumOf (folded % each) [(1,2),(3,4)]10
sum ≡ sumOf folded

This operation may be more strict than you would expect. If you want a lazier version use \o -> getSum . foldMapOf o Sum

valueasumOf :: (Is k A_Fold, Alternative f) => Optic' k is s (f a) -> s -> f a
#

The sum of a collection of actions.

Example1 expression
asumOf each ("hello","world")"helloworld"
Example1 expression
asumOf each (Nothing, Just "hello", Nothing)Just "hello"
asum ≡ asumOf folded
valuemsumOf :: (Is k A_Fold, MonadPlus m) => Optic' k is s (m a) -> s -> m a
#

The sum of a collection of actions.

Example1 expression
msumOf each ("hello","world")"helloworld"
Example1 expression
msumOf each (Nothing, Just "hello", Nothing)Just "hello"
msum ≡ msumOf folded
valuenotElemOf :: (Is k A_Fold, Eq a) => Optic' k is s a -> a -> s -> Bool
#

Does the element not occur anywhere within a given Fold of the structure?

Example1 expression
notElemOf each 'd' ('a','b','c')True
Example1 expression
notElemOf each 'a' ('a','b','c')False
notElem ≡ notElemOf folded
valuelengthOf :: Is k A_Fold => Optic' k is s a -> s -> Int
#

Calculate the number of targets there are for a Fold in a given container.

Note: This can be rather inefficient for large containers and just like length, this will not terminate for infinite folds.

length ≡ lengthOf folded
Example1 expression
lengthOf _1 ("hello",())1
Example1 expression
lengthOf folded [1..10]10
Example1 expression
lengthOf (folded % folded) [[1,2],[3,4],[5,6]]6
valuemaximumOf :: (Is k A_Fold, Ord a) => Optic' k is s a -> s -> Maybe a
#

Obtain the maximum element (if any) targeted by a Fold safely.

Note: maximumOf on a valid Iso, Lens or Getter will always return Just a value.

Example1 expression
maximumOf folded [1..10]Just 10
Example1 expression
maximumOf folded []Nothing
Example1 expression
maximumOf (folded % filtered even) [1,4,3,6,7,9,2]Just 6
maximum ≡ fromMaybe (error "empty") . maximumOf folded

In the interest of efficiency, This operation has semantics more strict than strictly necessary. \o -> getMax . foldMapOf o Max has lazier semantics but could leak memory.

valueminimumOf :: (Is k A_Fold, Ord a) => Optic' k is s a -> s -> Maybe a
#

Obtain the minimum element (if any) targeted by a Fold safely.

Note: minimumOf on a valid Iso, Lens or Getter will always return Just a value.

Example1 expression
minimumOf folded [1..10]Just 1
Example1 expression
minimumOf folded []Nothing
Example1 expression
minimumOf (folded % filtered even) [1,4,3,6,7,9,2]Just 2
minimum ≡ fromMaybe (error "empty") . minimumOf folded

In the interest of efficiency, This operation has semantics more strict than strictly necessary. \o -> getMin . foldMapOf o Min has lazier semantics but could leak memory.

valuefindOf :: Is k A_Fold => Optic' k is s a -> (a -> Bool) -> s -> Maybe a
#

The findOf function takes a Fold, a predicate and a structure and returns the leftmost element of the structure matching the predicate, or Nothing if there is no such element.

Example1 expression
findOf each even (1,3,4,6)Just 4
Example1 expression
findOf folded even [1,3,5,7]Nothing
find ≡ findOf folded
valuefindMOf
  1. :: (Is k A_Fold, Monad m)
  2. => Optic' k is s a
  3. -> a -> m Bool
  4. -> s
  5. -> m (Maybe a)
#

The findMOf function takes a Fold, a monadic predicate and a structure and returns in the monad the leftmost element of the structure matching the predicate, or Nothing if there is no such element.

Example1 expression
findMOf each (\x -> print ("Checking " ++ show x) >> return (even x)) (1,3,4,6)"Checking 1""Checking 3""Checking 4"Just 4
Example1 expression
findMOf each (\x -> print ("Checking " ++ show x) >> return (even x)) (1,3,5,7)"Checking 1""Checking 3""Checking 5""Checking 7"Nothing
findMOf folded :: (Monad m, Foldable f) => (a -> m Bool) -> f a -> m (Maybe a)
valuelookupOf :: (Is k A_Fold, Eq a) => Optic' k is s (a, v) -> a -> s -> Maybe v
#

The lookupOf function takes a Fold, a key, and a structure containing key/value pairs. It returns the first value corresponding to the given key. This function generalizes lookup to work on an arbitrary Fold instead of lists.

Example1 expression
lookupOf folded 4 [(2, 'a'), (4, 'b'), (4, 'c')]Just 'b'
Example1 expression
lookupOf folded 2 [(2, 'a'), (4, 'b'), (4, 'c')]Just 'a'
valueuniverseOf :: Is k A_Fold => Optic' k is a a -> a -> [a]
#

Given a Fold that knows how to locate immediate children, retrieve all of the transitive descendants of a node, including itself.

valuecosmosOf :: Is k A_Fold => Optic' k is a a -> Fold a a
#

Given a Fold that knows how to locate immediate children, fold all of the transitive descendants of a node, including itself.

valueparaOf :: Is k A_Fold => Optic' k is a a -> (a -> [r] -> r) -> a -> r
#

Perform a fold-like computation on each value, technically a paramorphism.

Combinators

2 declarations

Monoid structures

2 declarations

Fold admits (at least) two monoid structures:

  • summing concatenates results from both folds.

  • failing 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 Fold, because there is not a unique choice of monoid to use, and the (<>) operator could not be used to combine optics of different kinds. When porting code from lens that uses <> to combine folds, use summing instead.

valuesumming
  1. :: (Is k A_Fold, Is l A_Fold)
  2. => Optic' k is s a
  3. -> Optic' l js s a
  4. -> Fold s a
#

Return entries of the first Fold, then the second one.

Example1 expression
toListOf (_1 % ix 0 `summing` _2 % ix 1) ([1,2], [4,7,1])[1,7]

For the traversal version see adjoin.

valuefailing
  1. :: (Is k A_Fold, Is l A_Fold)
  2. => Optic' k is s a
  3. -> Optic' l js s a
  4. -> Fold s a
#

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

Example2 expressions
toListOf (ix 1 `failing` ix 0) [4,7][7]toListOf (ix 1 `failing` ix 0) [4][4]

Subtyping

1 declaration
datadata A_Fold
#

Tag for a fold.

Instances28Is, JoinKinds, IxOptic, ToReadOnly, ReadOnlyOptic, …