Copyright | (c) 2011-2015 Edward Kmett (c) 2018-2023 Kowainik |
---|---|
License | MIT |
Maintainer | Kowainik <xrom.xkov@gmail.com> |
Stability | Stable |
Portability | Portable |
Safe Haskell | Safe |
Language | Haskell2010 |
Synopsis
- class Foldable f => Foldable1 f where
- foldMap1 :: Semigroup m => (a -> m) -> f a -> m
- fold1 :: Semigroup m => f m -> m
- foldr1 :: (a -> b -> b) -> b -> f a -> b
- toNonEmpty :: f a -> NonEmpty a
- head1 :: f a -> a
- last1 :: f a -> a
- maximum1 :: Ord a => f a -> a
- minimum1 :: Ord a => f a -> a
- maximumOn1 :: Ord b => (a -> b) -> f a -> a
- minimumOn1 :: Ord b => (a -> b) -> f a -> a
- foldl1' :: (a -> a -> a) -> NonEmpty a -> a
- average1 :: forall a f. (Foldable1 f, Fractional a) => f a -> a
Documentation
class Foldable f => Foldable1 f where Source #
The class of foldable data structures that cannot be empty.
Since: 0.3.0
foldMap1 :: Semigroup m => (a -> m) -> f a -> m Source #
Map each element of the non-empty structure to a semigroup, and combine the results.
>>>
foldMap1 SG.Sum (1 :| [2, 3, 4])
Sum {getSum = 10}>>>
foldMap1 show (123 :| [456, 789, 0])
"1234567890"
fold1 :: Semigroup m => f m -> m Source #
Combine the elements of a non-empty structure using a semigroup.
>>>
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 Source #
Combines the elements of a non-empty structure using a binary function f
.
>>>
foldr1 (+) 0 (1 :| [2, 3])
6>>>
foldr1 (+) 1 $ Identity 3
4
Since: 1.0.0.0
toNonEmpty :: f a -> NonEmpty a Source #
Convert a non-empty data structure to a NonEmpty list.
>>>
toNonEmpty (Identity 2)
2 :| []
The first element of a non-empty data structure.
>>>
head1 (1 :| [2, 3, 4])
1
The last element of a non-empty data structure.
>>>
last1 (1 :| [2, 3, 4])
4
maximum1 :: Ord a => f a -> a Source #
The largest element of a non-empty data structure.
>>>
maximum1 (32 :| [64, 8, 128, 16])
128
minimum1 :: Ord a => f a -> a Source #
The smallest element of a non-empty data structure.
>>>
minimum1 (32 :| [64, 8, 128, 16])
8
maximumOn1 :: Ord b => (a -> b) -> f a -> a Source #
The largest element of a non-empty data structure with respect to the given comparison function.
>>>
maximumOn1 abs (0 :| [2, 1, -3, -2])
-3
Since: 1.0.0.0
minimumOn1 :: Ord b => (a -> b) -> f a -> a Source #
The smallest element of a non-empty data structure with respect to the given comparison function.
>>>
minimumOn1 abs (0 :| [2, 1, -3, -2])
0
Since: 1.0.0.0
Instances
Foldable1 Identity Source # | Since: 0.3.0 |
Defined in Relude.Extra.Foldable1 foldMap1 :: Semigroup m => (a -> m) -> Identity a -> m Source # fold1 :: Semigroup m => Identity m -> m Source # foldr1 :: (a -> b -> b) -> b -> Identity a -> b Source # toNonEmpty :: Identity a -> NonEmpty a Source # head1 :: Identity a -> a Source # last1 :: Identity a -> a Source # maximum1 :: Ord a => Identity a -> a Source # minimum1 :: Ord a => Identity a -> a Source # maximumOn1 :: Ord b => (a -> b) -> Identity a -> a Source # minimumOn1 :: Ord b => (a -> b) -> Identity a -> a Source # | |
Foldable1 NonEmpty Source # | Since: 0.3.0 |
Defined in Relude.Extra.Foldable1 foldMap1 :: Semigroup m => (a -> m) -> NonEmpty a -> m Source # fold1 :: Semigroup m => NonEmpty m -> m Source # foldr1 :: (a -> b -> b) -> b -> NonEmpty a -> b Source # toNonEmpty :: NonEmpty a -> NonEmpty a Source # head1 :: NonEmpty a -> a Source # last1 :: NonEmpty a -> a Source # maximum1 :: Ord a => NonEmpty a -> a Source # minimum1 :: Ord a => NonEmpty a -> a Source # maximumOn1 :: Ord b => (a -> b) -> NonEmpty a -> a Source # minimumOn1 :: Ord b => (a -> b) -> NonEmpty a -> a Source # | |
IsListError => Foldable1 [] Source # | ⚠️CAUTION⚠️ This instance is for custom error display only.
In case it is used by mistake, the user will see the following:
Since: 0.6.0.0 |
Defined in Relude.Extra.Foldable1 foldMap1 :: Semigroup m => (a -> m) -> [a] -> m Source # fold1 :: Semigroup m => [m] -> m Source # foldr1 :: (a -> b -> b) -> b -> [a] -> b Source # toNonEmpty :: [a] -> NonEmpty a Source # maximum1 :: Ord a => [a] -> a Source # minimum1 :: Ord a => [a] -> a Source # maximumOn1 :: Ord b => (a -> b) -> [a] -> a Source # minimumOn1 :: Ord b => (a -> b) -> [a] -> a Source # | |
Foldable1 ((,) c) Source # | Since: 0.3.0 |
Defined in Relude.Extra.Foldable1 foldMap1 :: Semigroup m => (a -> m) -> (c, a) -> m Source # fold1 :: Semigroup m => (c, m) -> m Source # foldr1 :: (a -> b -> b) -> b -> (c, a) -> b Source # toNonEmpty :: (c, a) -> NonEmpty a Source # maximum1 :: Ord a => (c, a) -> a Source # minimum1 :: Ord a => (c, a) -> a Source # maximumOn1 :: Ord b => (a -> b) -> (c, a) -> a Source # minimumOn1 :: Ord b => (a -> b) -> (c, a) -> a Source # | |
(Foldable1 f, Foldable1 g) => Foldable1 (Product f g) Source # | Since: 0.3.0 |
Defined in Relude.Extra.Foldable1 foldMap1 :: Semigroup m => (a -> m) -> Product f g a -> m Source # fold1 :: Semigroup m => Product f g m -> m Source # foldr1 :: (a -> b -> b) -> b -> Product f g a -> b Source # toNonEmpty :: Product f g a -> NonEmpty a Source # head1 :: Product f g a -> a Source # last1 :: Product f g a -> a Source # maximum1 :: Ord a => Product f g a -> a Source # minimum1 :: Ord a => Product f g a -> a Source # maximumOn1 :: Ord b => (a -> b) -> Product f g a -> a Source # minimumOn1 :: Ord b => (a -> b) -> Product f g a -> a Source # | |
(Foldable1 f, Foldable1 g) => Foldable1 (Sum f g) Source # | Since: 0.3.0 |
Defined in Relude.Extra.Foldable1 foldMap1 :: Semigroup m => (a -> m) -> Sum f g a -> m Source # fold1 :: Semigroup m => Sum f g m -> m Source # foldr1 :: (a -> b -> b) -> b -> Sum f g a -> b Source # toNonEmpty :: Sum f g a -> NonEmpty a Source # head1 :: Sum f g a -> a Source # last1 :: Sum f g a -> a Source # maximum1 :: Ord a => Sum f g a -> a Source # minimum1 :: Ord a => Sum f g a -> a Source # maximumOn1 :: Ord b => (a -> b) -> Sum f g a -> a Source # minimumOn1 :: Ord b => (a -> b) -> Sum f g a -> a Source # | |
(Foldable1 f, Foldable1 g) => Foldable1 (Compose f g) Source # | Since: 0.3.0 |
Defined in Relude.Extra.Foldable1 foldMap1 :: Semigroup m => (a -> m) -> Compose f g a -> m Source # fold1 :: Semigroup m => Compose f g m -> m Source # foldr1 :: (a -> b -> b) -> b -> Compose f g a -> b Source # toNonEmpty :: Compose f g a -> NonEmpty a Source # head1 :: Compose f g a -> a Source # last1 :: Compose f g a -> a Source # maximum1 :: Ord a => Compose f g a -> a Source # minimum1 :: Ord a => Compose f g a -> a Source # maximumOn1 :: Ord b => (a -> b) -> Compose f g a -> a Source # minimumOn1 :: Ord b => (a -> b) -> Compose f g a -> a Source # |
foldl1' :: (a -> a -> a) -> NonEmpty a -> a Source #
Strictly folds non-empty structure with given function f
:
foldl1' f [x0, x1, x2 ...] = f (f x0 x1) x2 ...
>>>
foldl1' (++) ([1,2] :| [[3,4], [5,6]])
[1,2,3,4,5,6]
Since: 0.3.0
average1 :: forall a f. (Foldable1 f, Fractional a) => f a -> a Source #
Given a Foldable1
of Fractional
elements, computes the average if
possible and returns the resulting element.
>>>
average1 (42 :| [])
42.0>>>
average1 (1 :| [2,3,4])
2.5
Since: 1.0.0.0