Copyright | (c) 2016 Stephen Diehl (c) 2016-2018 Serokell (c) 2018-2019 Kowainik |
---|---|
License | MIT |
Maintainer | Kowainik <xrom.xkov@gmail.com> |
Safe Haskell | None |
Language | Haskell2010 |
Fixes and additions to Foldable
.
Synopsis
- flipfoldl' :: Foldable f => (a -> b -> b) -> b -> f a -> b
- asumMap :: (Foldable f, Alternative m) => (a -> m b) -> f a -> m b
- foldMapA :: forall b m f a. (Semigroup b, Monoid b, Applicative m, Foldable f) => (a -> m b) -> f a -> m b
- foldMapM :: forall b m f a. (Monoid b, Monad m, Foldable f) => (a -> m b) -> f a -> m b
- sum :: forall a f. (Foldable f, Num a) => f a -> a
- product :: forall a f. (Foldable f, Num a) => f a -> a
- elem :: (Foldable f, DisallowElem f, Eq a) => a -> f a -> Bool
- notElem :: (Foldable f, DisallowElem f, Eq a) => a -> f a -> Bool
- allM :: (Foldable f, Monad m) => (a -> m Bool) -> f a -> m Bool
- anyM :: (Foldable f, Monad m) => (a -> m Bool) -> f a -> m Bool
- andM :: (Foldable f, Monad m) => f (m Bool) -> m Bool
- orM :: (Foldable f, Monad m) => f (m Bool) -> m Bool
- type family DisallowElem (f :: Type -> Type) :: Constraint where ...
- type family ElemErrorMessage (t :: k) (msg :: Symbol) :: ErrorMessage where ...
Documentation
flipfoldl' :: Foldable f => (a -> b -> b) -> b -> f a -> b Source #
Similar to foldl'
but takes a function with its arguments flipped.
>>>
flipfoldl' (/) 5 [2,3] :: Rational
15 % 2
This function can be useful for constructing containers from lists.
asumMap :: (Foldable f, Alternative m) => (a -> m b) -> f a -> m b Source #
Alternative version of asum
.
>>>
asumMap (\x -> if x > 2 then Just x else Nothing) [1..4]
Just 3
foldMapA :: forall b m f a. (Semigroup b, Monoid b, Applicative m, Foldable f) => (a -> m b) -> f a -> m b Source #
Polymorphic version of concatMapA
function.
>>>
foldMapA @[Int] (Just . replicate 3) [1..3]
Just [1,1,1,2,2,2,3,3,3]
foldMapM :: forall b m f a. (Monoid b, Monad m, Foldable f) => (a -> m b) -> f a -> m b Source #
Polymorphic version of concatMapM
function.
>>>
foldMapM @[Int] (Just . replicate 3) [1..3]
Just [1,1,1,2,2,2,3,3,3]
sum :: forall a f. (Foldable f, Num a) => f a -> a Source #
Stricter version of sum
.
>>>
sum [1..10]
55
product :: forall a f. (Foldable f, Num a) => f a -> a Source #
Stricter version of product
.
>>>
product [1..10]
3628800
elem :: (Foldable f, DisallowElem f, Eq a) => a -> f a -> Bool Source #
Like elem
but doesn't work on Set
and HashSet
for performance reasons.
>>>
elem 'x' ("abc" :: String)
False>>>
elem False (one True :: Set Bool)
... • Do not use 'elem' and 'notElem' methods from 'Foldable' on Set Suggestions: Instead of elem :: (Foldable t, Eq a) => a -> t a -> Bool use member :: Ord a => a -> Set a -> Bool ... Instead of notElem :: (Foldable t, Eq a) => a -> t a -> Bool use not . member ...
notElem :: (Foldable f, DisallowElem f, Eq a) => a -> f a -> Bool Source #
Like notElem
but doesn't work on Set
and HashSet
for performance reasons.
>>>
notElem 'x' ("abc" :: String)
True>>>
notElem False (one True :: Set Bool)
... • Do not use 'elem' and 'notElem' methods from 'Foldable' on Set Suggestions: Instead of elem :: (Foldable t, Eq a) => a -> t a -> Bool use member :: Ord a => a -> Set a -> Bool ... Instead of notElem :: (Foldable t, Eq a) => a -> t a -> Bool use not . member ...
allM :: (Foldable f, Monad m) => (a -> m Bool) -> f a -> m Bool Source #
Monadic version of all
.
>>>
allM (readMaybe >=> pure . even) ["6", "10"]
Just True>>>
allM (readMaybe >=> pure . even) ["5", "aba"]
Just False>>>
allM (readMaybe >=> pure . even) ["aba", "10"]
Nothing
anyM :: (Foldable f, Monad m) => (a -> m Bool) -> f a -> m Bool Source #
Monadic version of any
.
>>>
anyM (readMaybe >=> pure . even) ["5", "10"]
Just True>>>
anyM (readMaybe >=> pure . even) ["10", "aba"]
Just True>>>
anyM (readMaybe >=> pure . even) ["aba", "10"]
Nothing
andM :: (Foldable f, Monad m) => f (m Bool) -> m Bool Source #
Monadic version of and
.
>>>
andM [Just True, Just False]
Just False>>>
andM [Just True]
Just True>>>
andM [Just True, Just False, Nothing]
Just False>>>
andM [Just True, Nothing]
Nothing>>>
andM [putTextLn "1" >> pure True, putTextLn "2" >> pure False, putTextLn "3" >> pure True]
1 2 False
orM :: (Foldable f, Monad m) => f (m Bool) -> m Bool Source #
Monadic version of or
.
>>>
orM [Just True, Just False]
Just True>>>
orM [Just True, Nothing]
Just True>>>
orM [Nothing, Just True]
Nothing
Internals
type family DisallowElem (f :: Type -> Type) :: Constraint where ... Source #
Type family that produces compile-time errors when elem
and notElem
functions are used with Set
and HashSet
.
DisallowElem Set = TypeError (ElemErrorMessage Set SetMemberType) | |
DisallowElem HashSet = TypeError (ElemErrorMessage HashSet HashSetMemberType) | |
DisallowElem f = () |
type family ElemErrorMessage (t :: k) (msg :: Symbol) :: ErrorMessage where ... Source #
ElemErrorMessage t msg = (((((((((((Text "Do not use 'elem' and 'notElem' methods from 'Foldable' on " :<>: ShowType t) :$$: Text "Suggestions:") :$$: Text " Instead of") :$$: Text " elem :: (Foldable t, Eq a) => a -> t a -> Bool") :$$: Text " use") :$$: (Text " member :: " :<>: Text msg)) :$$: Text "") :$$: Text " Instead of") :$$: Text " notElem :: (Foldable t, Eq a) => a -> t a -> Bool") :$$: Text " use") :$$: Text " not . member") :$$: Text "" |