module Fold.Nonempty.Examples.Interesting
(
magma, semigroup,
last,
maximum, minimum, maximumBy, minimumBy,
list, reverseList,
)
where
import Fold.Nonempty.Type
import Data.Function (id, const, flip, (.))
import Data.List.NonEmpty (NonEmpty ((:|)))
import Data.Ord (Ord, Ordering (GT), max, min)
import Data.Semigroup (Semigroup, (<>))
import qualified Strict
magma :: (a -> a -> a) -> NonemptyFold a a
magma :: forall a. (a -> a -> a) -> NonemptyFold a a
magma a -> a -> a
step = NonemptyFold{ initial :: a -> a
initial = forall a. a -> a
id, a -> a -> a
step :: a -> a -> a
step :: a -> a -> a
step, extract :: a -> a
extract = forall a. a -> a
id }
semigroup :: Semigroup a => NonemptyFold a a
semigroup :: forall a. Semigroup a => NonemptyFold a a
semigroup = forall a. (a -> a -> a) -> NonemptyFold a a
magma forall a. Semigroup a => a -> a -> a
(<>)
last :: NonemptyFold a a
last :: forall a. NonemptyFold a a
last = forall a. (a -> a -> a) -> NonemptyFold a a
magma (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a b. a -> b -> a
const)
maximum :: Ord a => NonemptyFold a a
maximum :: forall a. Ord a => NonemptyFold a a
maximum = forall a. (a -> a -> a) -> NonemptyFold a a
magma forall a. Ord a => a -> a -> a
max
maximumBy :: (a -> a -> Ordering) -> NonemptyFold a a
maximumBy :: forall a. (a -> a -> Ordering) -> NonemptyFold a a
maximumBy a -> a -> Ordering
cmp = forall a. (a -> a -> a) -> NonemptyFold a a
magma (\a
x a
y -> case a -> a -> Ordering
cmp a
x a
y of { Ordering
GT -> a
x; Ordering
_ -> a
y })
minimum :: Ord a => NonemptyFold a a
minimum :: forall a. Ord a => NonemptyFold a a
minimum = forall a. (a -> a -> a) -> NonemptyFold a a
magma forall a. Ord a => a -> a -> a
min
minimumBy :: (a -> a -> Ordering) -> NonemptyFold a a
minimumBy :: forall a. (a -> a -> Ordering) -> NonemptyFold a a
minimumBy a -> a -> Ordering
cmp = forall a. (a -> a -> a) -> NonemptyFold a a
magma (\a
x a
y -> case a -> a -> Ordering
cmp a
x a
y of { Ordering
GT -> a
y; Ordering
_ -> a
x })
list :: NonemptyFold a (NonEmpty a)
list :: forall a. NonemptyFold a (NonEmpty a)
list = NonemptyFold
{ initial :: a -> Tuple2 a ([a] -> [a])
initial = \a
a -> forall a b. a -> b -> Tuple2 a b
Strict.Tuple2 a
a forall a. a -> a
id
, step :: Tuple2 a ([a] -> [a]) -> a -> Tuple2 a ([a] -> [a])
step = \(Strict.Tuple2 a
a0 [a] -> [a]
x) a
a -> forall a b. a -> b -> Tuple2 a b
Strict.Tuple2 a
a0 ([a] -> [a]
x forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a
a :))
, extract :: Tuple2 a ([a] -> [a]) -> NonEmpty a
extract = \(Strict.Tuple2 a
a0 [a] -> [a]
x) -> a
a0 forall a. a -> [a] -> NonEmpty a
:| ([a] -> [a]
x [])
}
reverseList :: NonemptyFold a (NonEmpty a)
reverseList :: forall a. NonemptyFold a (NonEmpty a)
reverseList = NonemptyFold
{ initial :: a -> NonEmpty a
initial = (forall a. a -> [a] -> NonEmpty a
:| [])
, step :: NonEmpty a -> a -> NonEmpty a
step = \(a
b :| [a]
x) a
a -> a
a forall a. a -> [a] -> NonEmpty a
:| a
b forall a. a -> [a] -> [a]
: [a]
x
, extract :: NonEmpty a -> NonEmpty a
extract = forall a. a -> a
id
}