module Fold.Shortcut.Examples.Interesting
(
null,
and, or, all, any,
element, notElement, find, lookup,
index, findIndex, elementIndex,
)
where
import Fold.Shortcut.Type
import Control.Applicative (liftA2)
import Data.Bool (Bool (False, True))
import Data.Eq (Eq, (/=), (==))
import Data.Functor (($>), (<&>))
import Data.Maybe (Maybe (Just, Nothing))
import Numeric.Natural (Natural)
import Prelude ((-))
import Fold.Shortcut.Conversion (fold)
import Fold.Shortcut.Utilities (demotivate)
import qualified Fold.Pure.Examples.Interesting as Fold
null :: ShortcutFold a Bool
null :: forall a. ShortcutFold a Bool
null = ShortcutFold
{ initial :: Vitality () ()
initial = forall a b. Will -> b -> Vitality a b
Alive Will
Tenacious ()
, step :: () -> a -> Vitality () ()
step = \() a
_ -> forall a b. a -> Vitality a b
Dead ()
, extractLive :: () -> Bool
extractLive = \() -> Bool
True
, extractDead :: () -> Bool
extractDead = \() -> Bool
False
}
and :: ShortcutFold Bool Bool
and :: ShortcutFold Bool Bool
and = ShortcutFold
{ initial :: Vitality () ()
initial = forall a b. Will -> b -> Vitality a b
Alive Will
Tenacious ()
, step :: () -> Bool -> Vitality () ()
step = \()
_ Bool
a -> if Bool
a then forall a b. Will -> b -> Vitality a b
Alive Will
Tenacious () else forall a b. a -> Vitality a b
Dead ()
, extractLive :: () -> Bool
extractLive = \() -> Bool
True
, extractDead :: () -> Bool
extractDead = \() -> Bool
False
}
or :: ShortcutFold Bool Bool
or :: ShortcutFold Bool Bool
or = ShortcutFold
{ initial :: Vitality () ()
initial = forall a b. Will -> b -> Vitality a b
Alive Will
Tenacious ()
, step :: () -> Bool -> Vitality () ()
step = \() Bool
a -> if Bool
a then forall a b. a -> Vitality a b
Dead () else forall a b. Will -> b -> Vitality a b
Alive Will
Tenacious ()
, extractLive :: () -> Bool
extractLive = \() -> Bool
False
, extractDead :: () -> Bool
extractDead = \() -> Bool
True
}
all :: (a -> Bool) -> ShortcutFold a Bool
all :: forall a. (a -> Bool) -> ShortcutFold a Bool
all a -> Bool
predicate = ShortcutFold
{ initial :: Vitality () ()
initial = forall a b. Will -> b -> Vitality a b
Alive Will
Tenacious ()
, step :: () -> a -> Vitality () ()
step = \() a
a -> if a -> Bool
predicate a
a then forall a b. Will -> b -> Vitality a b
Alive Will
Tenacious () else forall a b. a -> Vitality a b
Dead ()
, extractLive :: () -> Bool
extractLive = \() -> Bool
True
, extractDead :: () -> Bool
extractDead = \() -> Bool
False
}
any :: (a -> Bool) -> ShortcutFold a Bool
any :: forall a. (a -> Bool) -> ShortcutFold a Bool
any a -> Bool
predicate = ShortcutFold
{ initial :: Vitality () ()
initial = forall a b. Will -> b -> Vitality a b
Alive Will
Tenacious ()
, step :: () -> a -> Vitality () ()
step = \()
_ a
a -> if a -> Bool
predicate a
a then forall a b. a -> Vitality a b
Dead () else forall a b. Will -> b -> Vitality a b
Alive Will
Tenacious ()
, extractLive :: () -> Bool
extractLive = \() -> Bool
False
, extractDead :: () -> Bool
extractDead = \() -> Bool
True
}
element :: Eq a => a -> ShortcutFold a Bool
element :: forall a. Eq a => a -> ShortcutFold a Bool
element a
a = forall a. (a -> Bool) -> ShortcutFold a Bool
any (a
a ==)
notElement :: Eq a => a -> ShortcutFold a Bool
notElement :: forall a. Eq a => a -> ShortcutFold a Bool
notElement a
a = forall a. (a -> Bool) -> ShortcutFold a Bool
all (a
a /=)
find :: (a -> Bool) -> ShortcutFold a (Maybe a)
find :: forall a. (a -> Bool) -> ShortcutFold a (Maybe a)
find a -> Bool
ok = ShortcutFold
{ initial :: Vitality a ()
initial = forall a b. Will -> b -> Vitality a b
Alive Will
Tenacious ()
, step :: () -> a -> Vitality a ()
step = \() a
a -> if a -> Bool
ok a
a then forall a b. a -> Vitality a b
Dead a
a else forall a b. Will -> b -> Vitality a b
Alive Will
Tenacious ()
, extractDead :: a -> Maybe a
extractDead = forall a. a -> Maybe a
Just
, extractLive :: () -> Maybe a
extractLive = \() -> forall a. Maybe a
Nothing
}
index :: Natural -> ShortcutFold a (Maybe a)
index :: forall a. Natural -> ShortcutFold a (Maybe a)
index Natural
i = ShortcutFold
{ initial :: Vitality a Natural
initial = forall a b. Will -> b -> Vitality a b
Alive Will
Tenacious Natural
i
, step :: Natural -> a -> Vitality a Natural
step = \Natural
i' a
a -> if Natural
i' forall a. Eq a => a -> a -> Bool
== Natural
0 then forall a b. a -> Vitality a b
Dead a
a else forall a b. Will -> b -> Vitality a b
Alive Will
Tenacious (Natural
i' forall a. Num a => a -> a -> a
- Natural
1)
, extractDead :: a -> Maybe a
extractDead = forall a. a -> Maybe a
Just
, extractLive :: Natural -> Maybe a
extractLive = \Natural
_ -> forall a. Maybe a
Nothing
}
elementIndex :: Eq a => a -> ShortcutFold a (Maybe Natural)
elementIndex :: forall a. Eq a => a -> ShortcutFold a (Maybe Natural)
elementIndex a
a = forall a. (a -> Bool) -> ShortcutFold a (Maybe Natural)
findIndex (a
a ==)
findIndex :: (a -> Bool) -> ShortcutFold a (Maybe Natural)
findIndex :: forall a. (a -> Bool) -> ShortcutFold a (Maybe Natural)
findIndex a -> Bool
ok = forall a b. ShortcutFold a b -> ShortcutFold a b
demotivate
(
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (,) (forall a b. Fold a b -> ShortcutFold a b
fold forall a. Fold a Natural
Fold.length) (forall a. (a -> Bool) -> ShortcutFold a (Maybe a)
find a -> Bool
ok)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \(Natural
n, Maybe a
found) -> Maybe a
found forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> (Natural
n forall a. Num a => a -> a -> a
- Natural
1)
)
lookup :: Eq a => a -> ShortcutFold (a, b) (Maybe b)
lookup :: forall a b. Eq a => a -> ShortcutFold (a, b) (Maybe b)
lookup a
a0 = ShortcutFold
{ initial :: Vitality b ()
initial = forall a b. Will -> b -> Vitality a b
Alive Will
Tenacious ()
, step :: () -> (a, b) -> Vitality b ()
step = \() (a
a, b
b) -> if a
a forall a. Eq a => a -> a -> Bool
== a
a0 then forall a b. a -> Vitality a b
Dead b
b else forall a b. Will -> b -> Vitality a b
Alive Will
Tenacious ()
, extractLive :: () -> Maybe b
extractLive = \() -> forall a. Maybe a
Nothing
, extractDead :: b -> Maybe b
extractDead = forall a. a -> Maybe a
Just
}