-- | Folds of other types trivially lifted into 'NonemptyFold'
module Fold.Nonempty.Examples.Boring
  (
    {- * Endpoints -} first,
    {- * Monoid -} monoid,
    {- * Length -} length,
    {- * Boolean -} and, or, all, any,
    {- * Numeric -} sum, product, mean, variance, standardDeviation,
    {- * Search -} element, notElement, find, lookup,
    {- * Index -} index, findIndex, elementIndex,
    {- * List -} list, reverseList,
  )
  where

import Data.Bool (Bool)
import Data.Eq (Eq)
import Data.Maybe (Maybe)
import Data.Monoid (Monoid)
import Fold.Nonempty.Type (NonemptyFold)
import Numeric.Natural (Natural)
import Prelude (Floating, Fractional, Num)

import qualified Fold.Nonempty.Conversion as Convert
import qualified Fold.Pure.Examples.Interesting as Pure
import qualified Fold.Shortcut.Examples.Interesting as Shortcut
import qualified Fold.ShortcutNonempty.Examples as ShortcutNonempty

{-| The first input -}
first :: NonemptyFold a a
first :: forall a. NonemptyFold a a
first = forall a b. ShortcutNonemptyFold a b -> NonemptyFold a b
Convert.shortcutNonemptyFold forall a. ShortcutNonemptyFold a a
ShortcutNonempty.first

{-| Start with 'mempty', append each input on the right with ('<>') -}
monoid :: Monoid a => NonemptyFold a a
monoid :: forall a. Monoid a => NonemptyFold a a
monoid = forall a b. Fold a b -> NonemptyFold a b
Convert.fold forall a. Monoid a => Fold a a
Pure.monoid

{-| The number of inputs -}
length :: NonemptyFold a Natural
length :: forall a. NonemptyFold a Natural
length = forall a b. Fold a b -> NonemptyFold a b
Convert.fold forall a. Fold a Natural
Pure.length

{-| 'True' if all inputs are 'True' -}
and :: NonemptyFold Bool Bool
and :: NonemptyFold Bool Bool
and = forall a b. ShortcutFold a b -> NonemptyFold a b
Convert.shortcutFold ShortcutFold Bool Bool
Shortcut.and

{-| 'True' if any input is 'True' -}
or :: NonemptyFold Bool Bool
or :: NonemptyFold Bool Bool
or = forall a b. ShortcutFold a b -> NonemptyFold a b
Convert.shortcutFold ShortcutFold Bool Bool
Shortcut.or

{-| 'True' if all inputs satisfy the predicate -}
all :: (a -> Bool) -> NonemptyFold a Bool
all :: forall a. (a -> Bool) -> NonemptyFold a Bool
all a -> Bool
predicate = forall a b. ShortcutFold a b -> NonemptyFold a b
Convert.shortcutFold (forall a. (a -> Bool) -> ShortcutFold a Bool
Shortcut.all a -> Bool
predicate)

{-| 'True' if any input satisfies the predicate -}
any :: (a -> Bool) -> NonemptyFold a Bool
any :: forall a. (a -> Bool) -> NonemptyFold a Bool
any a -> Bool
predicate = forall a b. ShortcutFold a b -> NonemptyFold a b
Convert.shortcutFold (forall a. (a -> Bool) -> ShortcutFold a Bool
Shortcut.any a -> Bool
predicate)

{-| Adds the inputs -}
sum :: Num a => NonemptyFold a a
sum :: forall a. Num a => NonemptyFold a a
sum = forall a b. Fold a b -> NonemptyFold a b
Convert.fold forall a. Num a => Fold a a
Pure.sum

{-| Multiplies the inputs -}
product :: Num a => NonemptyFold a a
product :: forall a. Num a => NonemptyFold a a
product = forall a b. Fold a b -> NonemptyFold a b
Convert.fold forall a. Num a => Fold a a
Pure.product

{-| Numerically stable arithmetic mean of the inputs -}
mean :: Fractional a => NonemptyFold a a
mean :: forall a. Fractional a => NonemptyFold a a
mean = forall a b. Fold a b -> NonemptyFold a b
Convert.fold forall a. Fractional a => Fold a a
Pure.mean

{-| Numerically stable (population) variance over the inputs -}
variance :: Fractional a => NonemptyFold a a
variance :: forall a. Fractional a => NonemptyFold a a
variance = forall a b. Fold a b -> NonemptyFold a b
Convert.fold forall a. Fractional a => Fold a a
Pure.variance

{-| Numerically stable (population) standard deviation over the inputs -}
standardDeviation :: Floating a => NonemptyFold a a
standardDeviation :: forall a. Floating a => NonemptyFold a a
standardDeviation = forall a b. Fold a b -> NonemptyFold a b
Convert.fold forall a. Floating a => Fold a a
Pure.standardDeviation

{-| 'True' if any input is equal to the given value -}
element :: Eq a => a -> NonemptyFold a Bool
element :: forall a. Eq a => a -> NonemptyFold a Bool
element a
a = forall a b. ShortcutFold a b -> NonemptyFold a b
Convert.shortcutFold (forall a. Eq a => a -> ShortcutFold a Bool
Shortcut.element a
a)

{-| 'False' if any input is equal to the given value -}
notElement :: Eq a => a -> NonemptyFold a Bool
notElement :: forall a. Eq a => a -> NonemptyFold a Bool
notElement a
a = forall a b. ShortcutFold a b -> NonemptyFold a b
Convert.shortcutFold (forall a. Eq a => a -> ShortcutFold a Bool
Shortcut.notElement a
a)

{-| The first input that satisfies the predicate, if any -}
find :: (a -> Bool) -> NonemptyFold a (Maybe a)
find :: forall a. (a -> Bool) -> NonemptyFold a (Maybe a)
find a -> Bool
ok = forall a b. ShortcutFold a b -> NonemptyFold a b
Convert.shortcutFold (forall a. (a -> Bool) -> ShortcutFold a (Maybe a)
Shortcut.find a -> Bool
ok)

{-| The /n/th input, where n=0 is the first input, if the index is in bounds -}
index :: Natural -> NonemptyFold a (Maybe a)
index :: forall a. Natural -> NonemptyFold a (Maybe a)
index Natural
i = forall a b. ShortcutFold a b -> NonemptyFold a b
Convert.shortcutFold (forall a. Natural -> ShortcutFold a (Maybe a)
Shortcut.index Natural
i)

{-| The index of the first input that matches the given value, if any -}
elementIndex :: Eq a => a -> NonemptyFold a (Maybe Natural)
elementIndex :: forall a. Eq a => a -> NonemptyFold a (Maybe Natural)
elementIndex a
a = forall a b. ShortcutFold a b -> NonemptyFold a b
Convert.shortcutFold (forall a. Eq a => a -> ShortcutFold a (Maybe Natural)
Shortcut.elementIndex a
a)

{-| The index of the first input that satisfies the predicate, if any -}
findIndex :: (a -> Bool) -> NonemptyFold a (Maybe Natural)
findIndex :: forall a. (a -> Bool) -> NonemptyFold a (Maybe Natural)
findIndex a -> Bool
ok = forall a b. ShortcutFold a b -> NonemptyFold a b
Convert.shortcutFold (forall a. (a -> Bool) -> ShortcutFold a (Maybe Natural)
Shortcut.findIndex a -> Bool
ok)

{-| The @b@ from the first tuple where @a@ equals the given value, if any -}
lookup :: Eq a => a -> NonemptyFold (a, b) (Maybe b)
lookup :: forall a b. Eq a => a -> NonemptyFold (a, b) (Maybe b)
lookup a
a = forall a b. ShortcutFold a b -> NonemptyFold a b
Convert.shortcutFold (forall a b. Eq a => a -> ShortcutFold (a, b) (Maybe b)
Shortcut.lookup a
a)

{-| All the inputs -}
list :: NonemptyFold a [a]
list :: forall a. NonemptyFold a [a]
list = forall a b. Fold a b -> NonemptyFold a b
Convert.fold forall a. Fold a [a]
Pure.list

{-| All the inputs in reverse order -}
reverseList :: NonemptyFold a [a]
reverseList :: forall a. NonemptyFold a [a]
reverseList = forall a b. Fold a b -> NonemptyFold a b
Convert.fold forall a. Fold a [a]
Pure.reverseList