HORIZON HASKELLDocslts/ghc-9.14.x6d5fdfd2026-10-12Search packagesCtrl K
Theme: |

GHC 9.14.1 · lts/ghc-9.14.x · 6d5fdfd · 2026-10-12

Modulefoldl-1.4.18Haskell2010

Control.Foldl.NonEmpty

This module provides a Fold1 type that is a "non-empty" analog of the Fold type, meaning that it requires at least one input element in order to produce a result

This module does not provide all of the same utilities as the Control.Foldl module. Instead, this module only provides the utilities which can make use of the non-empty input guarantee (e.g. head). For all other utilities you can convert them from the equivalent Fold using fromFold.

Import this module qualified to avoid clashing with the Prelude:

Example1 expression
import qualified Control.Foldl.NonEmpty as Foldl1

Use fold1 to apply a Fold1 to a non-empty list:

Example1 expression
Foldl1.fold1 Foldl1.last (1 :| [2..10])10
Contents
  1. Fold Types
  2. Folding
  3. Conversion between Fold and Fold1
  4. Folds
  5. Non-empty Container Folds
  6. Utilities
Synopsis
  • Packagefoldl-1.4.18
  • Exports22
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceNonEmpty.hs

Fold Types

datadata Fold1 a b

A Fold1 is like a Fold except that it consumes at least one input element

Constructors

Instances17Arrow, ArrowChoice, Choice, Closed, Strong, Profunctor, …
patternpattern Fold1_ :: (a -> x) -> (x -> a -> x) -> (x -> b) -> Fold1 a b

Fold1_ is an alternative to the Fold1 constructor if you need to explicitly work with an initial, step and extraction function.

Fold1_ is similar to the Fold constructor, which also works with an initial, step and extraction function. However, note that Fold takes the step function as the first argument and the initial accumulator as the second argument, whereas Fold1_ takes them in swapped order:

Fold1_ initial step extract

While Fold resembles foldl, Fold1_ resembles foldlMap1.

Folding

Conversion between Fold and Fold1

Folds

valuehead :: Fold1 a a

Get the first element of a non-empty container

Non-empty Container Folds

Utilities

valuepurely
  1. :: forall x. (a -> x) -> (x -> a -> x) -> (x -> b) -> r
  2. -> Fold1 a b
  3. -> r

Upgrade a fold to accept the Fold1 type

valuepurely_ :: (forall x. (a -> x) -> (x -> a -> x) -> x) -> Fold1 a b -> b

Upgrade a more traditional fold to accept the Fold1 type

valuepremap :: (a -> b) -> Fold1 b r -> Fold1 a r

(premap f folder) returns a new Fold1 where f is applied at each step

Foldl1.fold1 (premap f folder) list = Foldl1.fold1 folder (NonEmpty.map f list)
Example1 expression
Foldl1.fold1 (premap Sum Foldl1.sconcat) (1 :| [2..10])Sum {getSum = 55}
Example1 expression
Foldl1.fold1 Foldl1.sconcat $ NonEmpty.map Sum (1 :| [2..10])Sum {getSum = 55}
premap id = id

premap (f . g) = premap g . premap f
premap k (pure r) = pure r

premap k (f <*> x) = premap k f <*> premap k x
valuehandles :: Handler1 a b -> Fold1 b r -> Fold1 a r

(handles t folder) transforms the input of a Fold1 using a Lens, Traversal1, or Fold1 optic:

handles _1        :: Fold1 a r -> Fold1 (a, b) r
handles traverse1 :: Traversable1 t => Fold1 a r -> Fold1 (t a) r
handles folded1   :: Foldable1    t => Fold1 a r -> Fold1 (t a) r
Example1 expression
Foldl1.fold1 (handles traverse1 Foldl1.nonEmpty) $ (1 :| [2..4]) :| [ 5 :| [6,7], 8 :| [9,10] ]1 :| [2,3,4,5,6,7,8,9,10]
Example1 expression
Foldl1.fold1 (handles _2 Foldl1.sconcat) $ (1,"Hello ") :| [(2,"World"),(3,"!")]"Hello World!"
handles id = id

handles (f . g) = handles f . handles g
handles t (pure r) = pure r

handles t (f <*> x) = handles t f <*> handles t x
valuefoldOver :: Handler1 s a -> Fold1 a b -> s -> b

(foldOver f folder xs) folds all values from a Lens, Traversal1 or Fold1 optic with the given folder

Example1 expression
foldOver (_2 . both1) Foldl1.nonEmpty (1, (2, 3))2 :| [3]
Foldl1.foldOver f folder xs == Foldl1.fold1 folder (xs ^.. f)
Foldl1.foldOver (folded1 . f) folder == Foldl1.fold1 (Foldl1.handles f folder)
Foldl1.foldOver folded1 == Foldl1.fold1