{-| 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`.
-}

module Control.Foldl.NonEmpty where

import Control.Applicative (liftA2)
import Control.Foldl (Fold(..))
import Control.Foldl.Internal (Either'(..))
import Data.List.NonEmpty (NonEmpty(..))
import Data.Profunctor (Profunctor(..))
import Data.Semigroup.Foldable (Foldable1(..))
import Prelude hiding (head, last, minimum, maximum)

import qualified Control.Foldl as Foldl

{-| A `Fold1` is like a `Fold` except that it consumes at least one input
    element
-}
data Fold1 a b = Fold1 (a -> Fold a b)

instance Functor (Fold1 a) where
    fmap :: (a -> b) -> Fold1 a a -> Fold1 a b
fmap a -> b
f (Fold1 a -> Fold a a
k) = (a -> Fold a b) -> Fold1 a b
forall a b. (a -> Fold a b) -> Fold1 a b
Fold1 ((Fold a a -> Fold a b) -> (a -> Fold a a) -> a -> Fold a b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> b) -> Fold a a -> Fold a b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f) a -> Fold a a
k)
    {-# INLINE fmap #-}

instance Profunctor Fold1 where
    lmap :: (a -> b) -> Fold1 b c -> Fold1 a c
lmap a -> b
f (Fold1 b -> Fold b c
k) = (a -> Fold a c) -> Fold1 a c
forall a b. (a -> Fold a b) -> Fold1 a b
Fold1 a -> Fold a c
k'
      where
        k' :: a -> Fold a c
k' a
a = (a -> b) -> Fold b c -> Fold a c
forall (p :: * -> * -> *) a b c.
Profunctor p =>
(a -> b) -> p b c -> p a c
lmap a -> b
f (b -> Fold b c
k (a -> b
f a
a))
    {-# INLINE lmap #-}

    rmap :: (b -> c) -> Fold1 a b -> Fold1 a c
rmap = (b -> c) -> Fold1 a b -> Fold1 a c
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
    {-# INLINE rmap #-}

instance Applicative (Fold1 a) where
    pure :: a -> Fold1 a a
pure a
b = (a -> Fold a a) -> Fold1 a a
forall a b. (a -> Fold a b) -> Fold1 a b
Fold1 (Fold a a -> a -> Fold a a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> Fold a a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
b))
    {-# INLINE pure #-}

    Fold1 a -> Fold a (a -> b)
l <*> :: Fold1 a (a -> b) -> Fold1 a a -> Fold1 a b
<*> Fold1 a -> Fold a a
r = (a -> Fold a b) -> Fold1 a b
forall a b. (a -> Fold a b) -> Fold1 a b
Fold1 ((Fold a (a -> b) -> Fold a a -> Fold a b)
-> (a -> Fold a (a -> b)) -> (a -> Fold a a) -> a -> Fold a b
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 Fold a (a -> b) -> Fold a a -> Fold a b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
(<*>) a -> Fold a (a -> b)
l a -> Fold a a
r)
    {-# INLINE (<*>) #-}

instance Semigroup b => Semigroup (Fold1 a b) where
    <> :: Fold1 a b -> Fold1 a b -> Fold1 a b
(<>) = (b -> b -> b) -> Fold1 a b -> Fold1 a b -> Fold1 a b
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 b -> b -> b
forall a. Semigroup a => a -> a -> a
(<>)
    {-# INLINE (<>) #-}

instance Monoid b => Monoid (Fold1 a b) where
    mempty :: Fold1 a b
mempty = b -> Fold1 a b
forall (f :: * -> *) a. Applicative f => a -> f a
pure b
forall a. Monoid a => a
mempty
    {-# INLINE mempty #-}

    mappend :: Fold1 a b -> Fold1 a b -> Fold1 a b
mappend = (b -> b -> b) -> Fold1 a b -> Fold1 a b -> Fold1 a b
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 b -> b -> b
forall a. Monoid a => a -> a -> a
mappend
    {-# INLINE mappend #-}

instance Num b => Num (Fold1 a b) where
    fromInteger :: Integer -> Fold1 a b
fromInteger = b -> Fold1 a b
forall (f :: * -> *) a. Applicative f => a -> f a
pure (b -> Fold1 a b) -> (Integer -> b) -> Integer -> Fold1 a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> b
forall a. Num a => Integer -> a
fromInteger
    {-# INLINE fromInteger #-}

    negate :: Fold1 a b -> Fold1 a b
negate = (b -> b) -> Fold1 a b -> Fold1 a b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> b
forall a. Num a => a -> a
negate
    {-# INLINE negate #-}

    abs :: Fold1 a b -> Fold1 a b
abs = (b -> b) -> Fold1 a b -> Fold1 a b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> b
forall a. Num a => a -> a
abs
    {-# INLINE abs #-}

    signum :: Fold1 a b -> Fold1 a b
signum = (b -> b) -> Fold1 a b -> Fold1 a b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> b
forall a. Num a => a -> a
signum
    {-# INLINE signum #-}

    + :: Fold1 a b -> Fold1 a b -> Fold1 a b
(+) = (b -> b -> b) -> Fold1 a b -> Fold1 a b -> Fold1 a b
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 b -> b -> b
forall a. Num a => a -> a -> a
(+)
    {-# INLINE (+) #-}

    * :: Fold1 a b -> Fold1 a b -> Fold1 a b
(*) = (b -> b -> b) -> Fold1 a b -> Fold1 a b -> Fold1 a b
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 b -> b -> b
forall a. Num a => a -> a -> a
(*)
    {-# INLINE (*) #-}

    (-) = (b -> b -> b) -> Fold1 a b -> Fold1 a b -> Fold1 a b
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (-)
    {-# INLINE (-) #-}

instance Fractional b => Fractional (Fold1 a b) where
    fromRational :: Rational -> Fold1 a b
fromRational = b -> Fold1 a b
forall (f :: * -> *) a. Applicative f => a -> f a
pure (b -> Fold1 a b) -> (Rational -> b) -> Rational -> Fold1 a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> b
forall a. Fractional a => Rational -> a
fromRational
    {-# INLINE fromRational #-}

    recip :: Fold1 a b -> Fold1 a b
recip = (b -> b) -> Fold1 a b -> Fold1 a b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> b
forall a. Fractional a => a -> a
recip
    {-# INLINE recip #-}

    / :: Fold1 a b -> Fold1 a b -> Fold1 a b
(/) = (b -> b -> b) -> Fold1 a b -> Fold1 a b -> Fold1 a b
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 b -> b -> b
forall a. Fractional a => a -> a -> a
(/)
    {-# INLINE (/) #-}

instance Floating b => Floating (Fold1 a b) where
    pi :: Fold1 a b
pi = b -> Fold1 a b
forall (f :: * -> *) a. Applicative f => a -> f a
pure b
forall a. Floating a => a
pi
    {-# INLINE pi #-}

    exp :: Fold1 a b -> Fold1 a b
exp = (b -> b) -> Fold1 a b -> Fold1 a b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> b
forall a. Floating a => a -> a
exp
    {-# INLINE exp #-}

    sqrt :: Fold1 a b -> Fold1 a b
sqrt = (b -> b) -> Fold1 a b -> Fold1 a b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> b
forall a. Floating a => a -> a
sqrt
    {-# INLINE sqrt #-}

    log :: Fold1 a b -> Fold1 a b
log = (b -> b) -> Fold1 a b -> Fold1 a b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> b
forall a. Floating a => a -> a
log
    {-# INLINE log #-}

    sin :: Fold1 a b -> Fold1 a b
sin = (b -> b) -> Fold1 a b -> Fold1 a b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> b
forall a. Floating a => a -> a
sin
    {-# INLINE sin #-}

    tan :: Fold1 a b -> Fold1 a b
tan = (b -> b) -> Fold1 a b -> Fold1 a b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> b
forall a. Floating a => a -> a
tan
    {-# INLINE tan #-}

    cos :: Fold1 a b -> Fold1 a b
cos = (b -> b) -> Fold1 a b -> Fold1 a b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> b
forall a. Floating a => a -> a
cos
    {-# INLINE cos #-}

    asin :: Fold1 a b -> Fold1 a b
asin = (b -> b) -> Fold1 a b -> Fold1 a b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> b
forall a. Floating a => a -> a
asin
    {-# INLINE asin #-}

    atan :: Fold1 a b -> Fold1 a b
atan = (b -> b) -> Fold1 a b -> Fold1 a b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> b
forall a. Floating a => a -> a
atan
    {-# INLINE atan #-}

    acos :: Fold1 a b -> Fold1 a b
acos = (b -> b) -> Fold1 a b -> Fold1 a b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> b
forall a. Floating a => a -> a
acos
    {-# INLINE acos #-}

    sinh :: Fold1 a b -> Fold1 a b
sinh = (b -> b) -> Fold1 a b -> Fold1 a b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> b
forall a. Floating a => a -> a
sinh
    {-# INLINE sinh #-}

    tanh :: Fold1 a b -> Fold1 a b
tanh = (b -> b) -> Fold1 a b -> Fold1 a b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> b
forall a. Floating a => a -> a
tanh
    {-# INLINE tanh #-}

    cosh :: Fold1 a b -> Fold1 a b
cosh = (b -> b) -> Fold1 a b -> Fold1 a b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> b
forall a. Floating a => a -> a
cosh
    {-# INLINE cosh #-}

    asinh :: Fold1 a b -> Fold1 a b
asinh = (b -> b) -> Fold1 a b -> Fold1 a b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> b
forall a. Floating a => a -> a
asinh
    {-# INLINE asinh #-}

    atanh :: Fold1 a b -> Fold1 a b
atanh = (b -> b) -> Fold1 a b -> Fold1 a b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> b
forall a. Floating a => a -> a
atanh
    {-# INLINE atanh #-}

    acosh :: Fold1 a b -> Fold1 a b
acosh = (b -> b) -> Fold1 a b -> Fold1 a b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> b
forall a. Floating a => a -> a
acosh
    {-# INLINE acosh #-}

    ** :: Fold1 a b -> Fold1 a b -> Fold1 a b
(**) = (b -> b -> b) -> Fold1 a b -> Fold1 a b -> Fold1 a b
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 b -> b -> b
forall a. Floating a => a -> a -> a
(**)
    {-# INLINE (**) #-}

    logBase :: Fold1 a b -> Fold1 a b -> Fold1 a b
logBase = (b -> b -> b) -> Fold1 a b -> Fold1 a b -> Fold1 a b
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 b -> b -> b
forall a. Floating a => a -> a -> a
logBase
    {-# INLINE logBase #-}

-- | Apply a strict left `Fold1` to a `NonEmpty` list
fold1 :: Foldable1 f => Fold1 a b -> f a -> b
fold1 :: Fold1 a b -> f a -> b
fold1 (Fold1 a -> Fold a b
k) f a
as1 = Fold a b -> [a] -> b
forall (f :: * -> *) a b. Foldable f => Fold a b -> f a -> b
Foldl.fold (a -> Fold a b
k a
a) [a]
as
  where
    a
a :| [a]
as = f a -> NonEmpty a
forall (t :: * -> *) a. Foldable1 t => t a -> NonEmpty a
toNonEmpty f a
as1
{-# INLINABLE fold1 #-}

-- | Promote any `Fold` to an equivalent `Fold1`
fromFold :: Fold a b -> Fold1 a b
fromFold :: Fold a b -> Fold1 a b
fromFold (Fold x -> a -> x
step x
begin x -> b
done) = (a -> Fold a b) -> Fold1 a b
forall a b. (a -> Fold a b) -> Fold1 a b
Fold1 (\a
a -> (x -> a -> x) -> x -> (x -> b) -> Fold a b
forall a b x. (x -> a -> x) -> x -> (x -> b) -> Fold a b
Fold x -> a -> x
step (x -> a -> x
step x
begin a
a) x -> b
done)
{-# INLINABLE fromFold #-}

-- | Promote any `Fold1` to an equivalent `Fold`
toFold :: Fold1 a b -> Fold a (Maybe b)
toFold :: Fold1 a b -> Fold a (Maybe b)
toFold (Fold1 a -> Fold a b
k0) = (Either' (a -> Fold a b) (Fold a b)
 -> a -> Either' (a -> Fold a b) (Fold a b))
-> Either' (a -> Fold a b) (Fold a b)
-> (Either' (a -> Fold a b) (Fold a b) -> Maybe b)
-> Fold a (Maybe b)
forall a b x. (x -> a -> x) -> x -> (x -> b) -> Fold a b
Fold Either' (a -> Fold a b) (Fold a b)
-> a -> Either' (a -> Fold a b) (Fold a b)
forall a b a.
Either' (a -> Fold a b) (Fold a b) -> a -> Either' a (Fold a b)
step Either' (a -> Fold a b) (Fold a b)
forall b. Either' (a -> Fold a b) b
begin Either' (a -> Fold a b) (Fold a b) -> Maybe b
forall a a a. Either' a (Fold a a) -> Maybe a
done
  where
    begin :: Either' (a -> Fold a b) b
begin = (a -> Fold a b) -> Either' (a -> Fold a b) b
forall a b. a -> Either' a b
Left' a -> Fold a b
k0

    step :: Either' (a -> Fold a b) (Fold a b) -> a -> Either' a (Fold a b)
step (Left' a -> Fold a b
k) a
a = Fold a b -> Either' a (Fold a b)
forall a b. b -> Either' a b
Right' (a -> Fold a b
k a
a)
    step (Right' (Fold x -> a -> x
step' x
begin' x -> b
done')) a
a =
        Fold a b -> Either' a (Fold a b)
forall a b. b -> Either' a b
Right' ((x -> a -> x) -> x -> (x -> b) -> Fold a b
forall a b x. (x -> a -> x) -> x -> (x -> b) -> Fold a b
Fold x -> a -> x
step' (x -> a -> x
step' x
begin' a
a) x -> b
done')

    done :: Either' a (Fold a a) -> Maybe a
done (Right' (Fold x -> a -> x
_ x
begin' x -> a
done')) = a -> Maybe a
forall a. a -> Maybe a
Just (x -> a
done' x
begin')
    done (Left' a
_) = Maybe a
forall a. Maybe a
Nothing
{-# INLINABLE toFold #-}

-- | Fold all values within a non-empty container into a `NonEmpty` list
nonEmpty :: Fold1 a (NonEmpty a)
nonEmpty :: Fold1 a (NonEmpty a)
nonEmpty = (a -> Fold a (NonEmpty a)) -> Fold1 a (NonEmpty a)
forall a b. (a -> Fold a b) -> Fold1 a b
Fold1 (\a
a -> ([a] -> NonEmpty a) -> Fold a [a] -> Fold a (NonEmpty a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a
a a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:|) Fold a [a]
forall a. Fold a [a]
Foldl.list)
{-# INLINEABLE nonEmpty #-}

-- | Fold all values within a non-empty container using (`<>`)
sconcat :: Semigroup a => Fold1 a a
sconcat :: Fold1 a a
sconcat = (a -> Fold a a) -> Fold1 a a
forall a b. (a -> Fold a b) -> Fold1 a b
Fold1 (\a
begin -> (a -> a -> a) -> a -> (a -> a) -> Fold a a
forall a b x. (x -> a -> x) -> x -> (x -> b) -> Fold a b
Fold a -> a -> a
forall a. Semigroup a => a -> a -> a
(<>) a
begin a -> a
forall a. a -> a
id)
{-# INLINABLE sconcat #-}

-- | Get the first element of a non-empty container
head :: Fold1 a a
head :: Fold1 a a
head = (a -> Fold a a) -> Fold1 a a
forall a b. (a -> Fold a b) -> Fold1 a b
Fold1 (\a
begin -> (a -> a -> a) -> a -> (a -> a) -> Fold a a
forall a b x. (x -> a -> x) -> x -> (x -> b) -> Fold a b
Fold a -> a -> a
forall p p. p -> p -> p
step a
begin a -> a
forall a. a -> a
id)
  where
    step :: p -> p -> p
step p
a p
_ = p
a
{-# INLINABLE head #-}

-- | Get the last element of a non-empty container
last :: Fold1 a a
last :: Fold1 a a
last = (a -> Fold a a) -> Fold1 a a
forall a b. (a -> Fold a b) -> Fold1 a b
Fold1 (\a
begin -> (a -> a -> a) -> a -> (a -> a) -> Fold a a
forall a b x. (x -> a -> x) -> x -> (x -> b) -> Fold a b
Fold a -> a -> a
forall p p. p -> p -> p
step a
begin a -> a
forall a. a -> a
id)
  where
    step :: p -> p -> p
step p
_ p
a = p
a
{-# INLINABLE last #-}

-- | Computes the maximum element
maximum :: Ord a => Fold1 a a
maximum :: Fold1 a a
maximum = (a -> Fold a a) -> Fold1 a a
forall a b. (a -> Fold a b) -> Fold1 a b
Fold1 (\a
begin -> (a -> a -> a) -> a -> (a -> a) -> Fold a a
forall a b x. (x -> a -> x) -> x -> (x -> b) -> Fold a b
Fold a -> a -> a
forall a. Ord a => a -> a -> a
max a
begin a -> a
forall a. a -> a
id)
{-# INLINABLE maximum #-}

-- | Computes the maximum element with respect to the given comparison function
maximumBy :: (a -> a -> Ordering) -> Fold1 a a
maximumBy :: (a -> a -> Ordering) -> Fold1 a a
maximumBy a -> a -> Ordering
cmp = (a -> Fold a a) -> Fold1 a a
forall a b. (a -> Fold a b) -> Fold1 a b
Fold1 (\a
begin -> (a -> a -> a) -> a -> (a -> a) -> Fold a a
forall a b x. (x -> a -> x) -> x -> (x -> b) -> Fold a b
Fold a -> a -> a
max' a
begin a -> a
forall a. a -> a
id)
  where
    max' :: a -> a -> a
max' a
x a
y = case a -> a -> Ordering
cmp a
x a
y of
        Ordering
GT -> a
x
        Ordering
_  -> a
y
{-# INLINABLE maximumBy #-}

-- | Computes the minimum element
minimum :: Ord a => Fold1 a a
minimum :: Fold1 a a
minimum = (a -> Fold a a) -> Fold1 a a
forall a b. (a -> Fold a b) -> Fold1 a b
Fold1 (\a
begin -> (a -> a -> a) -> a -> (a -> a) -> Fold a a
forall a b x. (x -> a -> x) -> x -> (x -> b) -> Fold a b
Fold a -> a -> a
forall a. Ord a => a -> a -> a
min a
begin a -> a
forall a. a -> a
id)
{-# INLINABLE minimum #-}

-- | Computes the minimum element with respect to the given comparison function
minimumBy :: (a -> a -> Ordering) -> Fold1 a a
minimumBy :: (a -> a -> Ordering) -> Fold1 a a
minimumBy a -> a -> Ordering
cmp = (a -> Fold a a) -> Fold1 a a
forall a b. (a -> Fold a b) -> Fold1 a b
Fold1 (\a
begin -> (a -> a -> a) -> a -> (a -> a) -> Fold a a
forall a b x. (x -> a -> x) -> x -> (x -> b) -> Fold a b
Fold a -> a -> a
min' a
begin a -> a
forall a. a -> a
id)
  where
    min' :: a -> a -> a
min' a
x a
y = case a -> a -> Ordering
cmp a
x a
y of
        Ordering
GT -> a
y
        Ordering
_  -> a
x
{-# INLINABLE minimumBy #-}