Type synonym for a fold.
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
- Packageoptics-core-0.4.1.1
- Exports45
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourceFold.hs
Formation
1 declarationIntroduction
1 declarationObtain a Fold by lifting traverse_ like function.
foldVL . traverseOf_ ≡ id
traverseOf_ . foldVL ≡ id
Elimination
8 declarationsCombine the results of a fold using a monoid.
Fold via embedding into a monoid.
Fold right-associatively.
Fold left-associatively, and strictly.
Fold to a list.
toListOf (_1 % folded % _Right) ([Right 'h', Left 5, Right 'i'], "bye")"hi"
Evaluate each action in a structure observed by a Fold from left to right, ignoring the results.
sequenceA_ ≡ sequenceOf_ folded
sequenceOf_ each (putStrLn "hello",putStrLn "world")helloworld
Traverse over all of the targets of a Fold, computing an Applicative-based answer, but unlike traverseOf do not construct a new structure. traverseOf_ generalizes traverse_ to work over any Fold.
traverseOf_ each putStrLn ("hello","world")helloworld
traverse_ ≡ traverseOf_ folded
A version of traverseOf_ with the arguments flipped.
Computation
0 declarationstraverseOf_ (foldVL f) ≡ f
Additional introduction forms
4 declarationsFold via the Foldable class.
Additional elimination forms
26 declarationsRetrieve the first entry of a Fold.
headOf folded [1..10]Just 1
headOf each (1,2)Just 1
Retrieve the last entry of a Fold.
lastOf folded [1..10]Just 10
lastOf each (1,2)Just 2
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
lengthOf _1 ("hello",())1
lengthOf folded [1..10]10
lengthOf (folded % folded) [[1,2],[3,4],[5,6]]6
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.
maximumOf folded [1..10]Just 10
maximumOf folded []Nothing
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.
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.
minimumOf folded [1..10]Just 1
minimumOf folded []Nothing
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.
Obtain the maximum element (if any) targeted by a Fold according to a user supplied Ordering.
maximumByOf folded (compare `on` length) ["mustard","relish","ham"]Just "mustard"
In the interest of efficiency, This operation has semantics more strict than strictly necessary.
maximumBy cmp ≡ fromMaybe (error "empty") . maximumByOf folded cmp
Obtain the minimum element (if any) targeted by a Fold according to a user supplied Ordering.
In the interest of efficiency, This operation has semantics more strict than strictly necessary.
minimumByOf folded (compare `on` length) ["mustard","relish","ham"]Just "ham"
minimumBy cmp ≡ fromMaybe (error "empty") . minimumByOf folded cmp
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.
findOf each even (1,3,4,6)Just 4
findOf folded even [1,3,5,7]Nothing
find ≡ findOf folded
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.
findMOf each (\x -> print ("Checking " ++ show x) >> return (even x)) (1,3,4,6)"Checking 1""Checking 3""Checking 4"Just 4
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)
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.
lookupOf folded 4 [(2, 'a'), (4, 'b'), (4, 'c')]Just 'b'
lookupOf folded 2 [(2, 'a'), (4, 'b'), (4, 'c')]Just 'a'
Given a Fold that knows how to locate immediate children, retrieve all of the transitive descendants of a node, including itself.
Given a Fold that knows how to locate immediate children, fold all of the transitive descendants of a node, including itself.
Perform a fold-like computation on each value, technically a paramorphism.
Combinators
2 declarationsConvert a fold to an AffineFold that visits the first element of the original fold.
For the traversal version see singular.
This allows you to traverse the elements of a Fold in the opposite order.
Monoid structures
2 declarationsFold 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.
Try the first Fold. If it returns no entries, try the second one.
toListOf (ix 1 `failing` ix 0) [4,7][7]toListOf (ix 1 `failing` ix 0) [4][4]
Subtyping
1 declarationTag for a fold.
Instances28Is, JoinKinds, IxOptic, ToReadOnly, ReadOnlyOptic, …
Is A_Getter A_FoldDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.SubtypingIs A_Lens A_FoldDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.SubtypingIs A_Prism A_FoldDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.SubtypingIs A_ReversedPrism A_FoldDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.SubtypingIs A_Traversal A_FoldDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.SubtypingIs An_AffineFold A_FoldDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.SubtypingIs An_AffineTraversal A_FoldDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.SubtypingIs An_Iso A_FoldDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds A_Fold A_Fold kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds A_Fold A_Getter kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds A_Fold A_Lens kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds A_Fold A_Prism kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds A_Fold A_ReversedPrism kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds A_Fold A_Traversal kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds A_Fold An_AffineFold kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds A_Fold An_AffineTraversal kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds A_Fold An_Iso kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds A_Getter A_Fold kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds A_Lens A_Fold kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds A_Prism A_Fold kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds A_ReversedPrism A_Fold kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds A_Traversal A_Fold kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds An_AffineFold A_Fold kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds An_AffineTraversal A_Fold kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds An_Iso A_Fold kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtyping(s ~ t, a ~ b) => IxOptic A_Fold s t a bDefined in optics-core-0.4.1.1 · Optics.Indexed.Core(s ~ t, a ~ b) => ToReadOnly A_Fold s t a bDefined in optics-core-0.4.1.1 · Optics.ReadOnlytype ReadOnlyOptic A_Fold = A_FoldDefined in optics-core-0.4.1.1 · Optics.ReadOnly