relude-0.1.0: Custom prelude from Kowainik

Safe HaskellNone
LanguageHaskell2010

Relude.Foldable.Fold

Description

Fixes and additions to Foldable.

Synopsis

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

foldMapA :: (Monoid b, Applicative m, Foldable f) => (a -> m b) -> f a -> m b Source #

foldMapM :: (Monoid b, Monad m, Foldable f) => (a -> m b) -> f a -> m b Source #

safeHead :: Foldable f => f a -> Maybe a Source #

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 :: ??? -- TODO
...
          Instead of
              notElem :: (Foldable t, Eq a) => a -> t a -> Bool
          use
              notMember :: ??? -- TODO
...

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 :: ??? -- TODO
...
          Instead of
              notElem :: (Foldable t, Eq a) => a -> t a -> Bool
          use
              notMember :: ??? -- TODO
...

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