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

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
  • 3 types
  • 18 values
  • Packagefoldl-1.4.18
  • Exports22
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceNonEmpty.hs

Fold Types

2 declarations
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

1 declaration

Conversion between Fold and Fold1

2 declarations

Folds

7 declarations
valuehead :: Fold1 a a
#

Get the first element of a non-empty container

valuelast :: Fold1 a a
#

Get the last element of a non-empty container

valuemaximumBy :: (a -> a -> Ordering) -> Fold1 a a
#

Computes the maximum element with respect to the given comparison function

valueminimumBy :: (a -> a -> Ordering) -> Fold1 a a
#

Computes the minimum element with respect to the given comparison function

Non-empty Container Folds

Utilities

9 declarations
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
newtypenewtype FromMaybe b
#
instance Monad m => Semigroup (FromMaybe m a) where
    mappend (FromMaybe f) (FromMaybe g) = FromMaybeM (f . Just . g)

Constructors

Instances1Semigroup
typetype Handler1 a b = forall x. (b -> Const (Dual (FromMaybe x)) b) -> a -> Const (Dual (FromMaybe x)) a
#

A handler for the upstream input of a Fold1

This is compatible with van Laarhoven optics as defined in the lens package. Any lens, fold1 or traversal1 will type-check as a Handler1.

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