Non-empty data structures that can be folded.
Methods
fold1 :: Semigroup m => t m -> mfoldMap1 :: Semigroup m => (a -> m) -> t a -> mfoldMap1' :: Semigroup m => (a -> m) -> t a -> mtoNonEmpty :: t a -> NonEmpty aNonEmpty list of elements of a structure, from left to right.
Example1 expression toNonEmpty (Identity 2)2 :| []
maximum :: Ord a => t a -> aThe largest element of a non-empty structure.
Example1 expression maximum (32 :| [64, 8, 128, 16])128
minimum :: Ord a => t a -> aThe least element of a non-empty structure.
Example1 expression minimum (32 :| [64, 8, 128, 16])8
head :: t a -> aThe first element of a non-empty structure.
Example1 expression head (1 :| [2, 3, 4])1
last :: t a -> aThe last element of a non-empty structure.
Example1 expression last (1 :| [2, 3, 4])4
foldrMap1 :: (a -> b) -> (a -> b -> b) -> t a -> bRight-associative fold of a structure, lazy in the accumulator.
In case of NonEmpty lists, foldrMap1, when given a function
f, a binary operatorg, and a list, reduces the list usinggfrom right to left applyingfto the rightmost element:foldrMap1 f g (x1 :| [x2, ..., xn1, xn]) == x1 `g` (x2 `g` ... (xn1 `g` (f xn))...)Note that since the head of the resulting expression is produced by an application of
gto the first element of the list, ifgis lazy in its right argument, foldrMap1 can produce a terminating expression from an unbounded list.For a general Foldable1 structure this should be semantically identical to:
foldrMap1 f g = foldrMap1 f g . toNonEmptyfoldlMap1' :: (a -> b) -> (b -> a -> b) -> t a -> bLeft-associative fold of a structure but with strict application of the operator.
This ensures that each step of the fold is forced to Weak Head Normal Form before being applied, avoiding the collection of thunks that would otherwise occur. This is often what you want to strictly reduce a finite structure to a single strict result.
For a general Foldable1 structure this should be semantically identical to:
foldlMap1' f z = foldlMap1' f z . toNonEmptyfoldlMap1 :: (a -> b) -> (b -> a -> b) -> t a -> bLeft-associative fold of a structure, lazy in the accumulator. This is rarely what you want, but can work well for structures with efficient right-to-left sequencing and an operator that is lazy in its left argument.
In case of NonEmpty lists, foldlMap1, when given a function
f, a binary operatorg, and a list, reduces the list usinggfrom left to right applyingfto the leftmost element:foldlMap1 f g (x1 :| [x2, ..., xn]) == (...(((f x1) `g` x2) `g`...) `g` xnNote that to produce the outermost application of the operator the entire input list must be traversed. This means that foldlMap1 will diverge if given an infinite list.
If you want an efficient strict left-fold, you probably want to use foldlMap1' instead of foldlMap1. The reason for this is that the latter does not force the inner results (e.g.
(f x1) `g` x2in the above example) before applying them to the operator (e.g. to(`g` x3)). This results in a thunk chainO(n)elements long, which then must be evaluated from the outside-in.For a general Foldable1 structure this should be semantically identical to:
foldlMap1 f g = foldlMap1 f g . toNonEmptyfoldrMap1' :: (a -> b) -> (a -> b -> b) -> t a -> bfoldrMap1' is a variant of foldrMap1 that performs strict reduction from right to left, i.e. starting with the right-most element. The input structure must be finite, otherwise foldrMap1' runs out of space (diverges).
If you want a strict right fold in constant space, you need a structure that supports faster than
O(n)access to the right-most element.This method does not run in constant space for structures such as NonEmpty lists that don't support efficient right-to-left iteration and so require
O(n)space to perform right-to-left reduction. Use of this method with such a structure is a hint that the chosen structure may be a poor fit for the task at hand. If the order in which the elements are combined is not important, use foldlMap1' instead.
Instances25Foldable1, …
Foldable1 ComplexDefined in base-4.20.2.0 · Data.Foldable1Foldable1 FirstDefined in base-4.20.2.0 · Data.Foldable1Foldable1 LastDefined in base-4.20.2.0 · Data.Foldable1Foldable1 MaxDefined in base-4.20.2.0 · Data.Foldable1Foldable1 MinDefined in base-4.20.2.0 · Data.Foldable1Foldable1 NonEmptyDefined in base-4.20.2.0 · Data.Foldable1Foldable1 IdentityDefined in base-4.20.2.0 · Data.Foldable1Foldable1 DownDefined in base-4.20.2.0 · Data.Foldable1Foldable1 DualDefined in base-4.20.2.0 · Data.Foldable1Foldable1 ProductDefined in base-4.20.2.0 · Data.Foldable1Foldable1 SumDefined in base-4.20.2.0 · Data.Foldable1Foldable1 Par1Defined in base-4.20.2.0 · Data.Foldable1Foldable1 SoloDefined in base-4.20.2.0 · Data.Foldable1Foldable1 V1Defined in base-4.20.2.0 · Data.Foldable1Foldable1 (Tuple2 a)Defined in base-4.20.2.0 · Data.Foldable1Foldable1 f => Foldable1 (Ap f)Defined in base-4.20.2.0 · Data.Foldable1Foldable1 f => Foldable1 (Alt f)Defined in base-4.20.2.0 · Data.Foldable1Foldable1 f => Foldable1 (Rec1 f)Defined in base-4.20.2.0 · Data.Foldable1(Foldable1 f, Foldable1 g) => Foldable1 (Product f g)Defined in base-4.20.2.0 · Data.Foldable1(Foldable1 f, Foldable1 g) => Foldable1 (Sum f g)Defined in base-4.20.2.0 · Data.Foldable1(Foldable1 f, Foldable1 g) => Foldable1 (f :*: g)Defined in base-4.20.2.0 · Data.Foldable1(Foldable1 f, Foldable1 g) => Foldable1 (f :+: g)Defined in base-4.20.2.0 · Data.Foldable1Foldable1 f => Foldable1 (M1 i c f)Defined in base-4.20.2.0 · Data.Foldable1(Foldable1 f, Foldable1 g) => Foldable1 (Compose f g)Defined in base-4.20.2.0 · Data.Foldable1(Foldable1 f, Foldable1 g) => Foldable1 (f :.: g)Defined in base-4.20.2.0 · Data.Foldable1