module Fold.ShortcutNonempty.Run where

import Fold.ShortcutNonempty.Type

import Data.List.NonEmpty (NonEmpty ((:|)))

{-| Fold a nonempty listlike container to a single summary result,
    forcing only enough input to satisfy the short-cutting fold -}
run :: ShortcutNonemptyFold a b -> NonEmpty a -> b
run :: forall a b. ShortcutNonemptyFold a b -> NonEmpty a -> b
run ShortcutNonemptyFold{ a -> Vitality x y
initial :: ()
initial :: a -> Vitality x y
initial, y -> a -> Vitality x y
step :: ()
step :: y -> a -> Vitality x y
step, x -> b
extractDead :: ()
extractDead :: x -> b
extractDead, y -> b
extractLive :: ()
extractLive :: y -> b
extractLive } =
    \(a
z :| [a]
as) -> Vitality x y -> [a] -> b
go (a -> Vitality x y
initial a
z) [a]
as
  where
    go :: Vitality x y -> [a] -> b
go (Alive Will
Tenacious y
x)  (a
a : [a]
as)  =  Vitality x y -> [a] -> b
go (y -> a -> Vitality x y
step y
x a
a) [a]
as
    go (Alive Will
Tenacious y
x)  []        =  y -> b
extractLive y
x
    go (Alive Will
Ambivalent y
x) [a]
_         =  y -> b
extractLive y
x
    go (Dead x
x)             [a]
_         =  x -> b
extractDead x
x