HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulerelude-1.2.0.0Haskell2010

Relude.Extra.Foldable1

SPDX-License-Identifier : MIT Maintainer : Kowainik xrom.xkov@gmail.com Stability : Stable Portability : Portable

Foldable1 is a typeclass like Foldable but for non-empty structures. For example, NonEmpty, Identity.

Foldable1 has all type-safe and total methods like head1, maximum1 in contradiction with Foldable.

  • 1 class
  • 2 values
  • Packagerelude-1.2.0.0
  • Exports3
  • LanguageHaskell2010
  • LicenceMIT
  • SourceFoldable1.hs
classclass Foldable f => Foldable1 (f :: Type -> Type) where
#

The class of foldable data structures that cannot be empty.

Methods

  • foldMap1 :: Semigroup m => (a -> m) -> f a -> m

    Map each element of the non-empty structure to a semigroup, and combine the results.

    Example2 expressions
    foldMap1 SG.Sum (1 :| [2, 3, 4])Sum {getSum = 10}foldMap1 show (123 :| [456, 789, 0])"1234567890"
  • fold1 :: Semigroup m => f m -> m

    Combine the elements of a non-empty structure using a semigroup.

    Example2 expressions
    fold1 (1 :| [2, 3, 4 :: SG.Sum Int])Sum {getSum = 10}fold1 (4 :| [5, 10 :: SG.Product Int])Product {getProduct = 200}
  • foldr1 :: (a -> b -> b) -> b -> f a -> b

    Combines the elements of a non-empty structure using a binary function f.

    Example2 expressions
    foldr1 (+) 0 (1 :| [2, 3])6foldr1 (+) 1 $ Identity 34
  • toNonEmpty :: f a -> NonEmpty a

    Convert a non-empty data structure to a NonEmpty list.

    Example1 expression
    toNonEmpty (Identity 2)2 :| []
  • head1 :: f a -> a

    The first element of a non-empty data structure.

    Example1 expression
    head1 (1 :| [2, 3, 4])1
  • last1 :: f a -> a

    The last element of a non-empty data structure.

    Example1 expression
    last1 (1 :| [2, 3, 4])4
  • maximum1 :: Ord a => f a -> a

    The largest element of a non-empty data structure.

    Example1 expression
    maximum1 (32 :| [64, 8, 128, 16])128
  • minimum1 :: Ord a => f a -> a

    The smallest element of a non-empty data structure.

    Example1 expression
    minimum1 (32 :| [64, 8, 128, 16])8
  • maximumOn1 :: Ord b => (a -> b) -> f a -> a

    The largest element of a non-empty data structure with respect to the given comparison function.

    Example1 expression
    maximumOn1 abs (0 :| [2, 1, -3, -2])-3
  • minimumOn1 :: Ord b => (a -> b) -> f a -> a

    The smallest element of a non-empty data structure with respect to the given comparison function.

    Example1 expression
    minimumOn1 abs (0 :| [2, 1, -3, -2])0
Instances7Foldable1, …
  • Foldable1 NonEmptyDefined in relude-1.2.0.0 · Relude.Extra.Foldable1
  • Foldable1 IdentityDefined in relude-1.2.0.0 · Relude.Extra.Foldable1
  • IsListError => Foldable1 []Defined in relude-1.2.0.0 · Relude.Extra.Foldable1

    ⚠️CAUTION⚠️ This instance is for custom error display only.

    Foldable1 is not supposed to be used with the lists.

    In case it is used by mistake, the user will see the following:

    Example1 expression
    head1 [1, 2, 3]...... The methods of the 'Foldable1' type class work with non-empty containers.      However, one of the 'Foldable1' functions is applied to the List....      Possible fixes:        * Replace []          with one of the: 'NonEmpty', 'Identity', '(c,)', 'Compose f g', 'Product f g', 'Sum f g'        * Or use 'Foldable' class for your own risk....
  • Foldable1 (Tuple2 c)Defined in relude-1.2.0.0 · Relude.Extra.Foldable1
  • (Foldable1 f, Foldable1 g) => Foldable1 (Product f g)Defined in relude-1.2.0.0 · Relude.Extra.Foldable1
  • (Foldable1 f, Foldable1 g) => Foldable1 (Sum f g)Defined in relude-1.2.0.0 · Relude.Extra.Foldable1
  • (Foldable1 f, Foldable1 g) => Foldable1 (Compose f g)Defined in relude-1.2.0.0 · Relude.Extra.Foldable1
valuefoldl1' :: (a -> a -> a) -> NonEmpty a -> a
#

Strictly folds non-empty structure with given function f:

foldl1' f [x0, x1, x2 ...] = f (f x0 x1) x2 ...
Example1 expression
foldl1' (++) ([1,2] :| [[3,4], [5,6]])[1,2,3,4,5,6]
valueaverage1 :: (Foldable1 f, Fractional a) => f a -> a
#

Given a Foldable1 of Fractional elements, computes the average if possible and returns the resulting element.

Example2 expressions
average1 (42 :| [])42.0average1 (1 :| [2,3,4])2.5