-- | Boolean functions.
module Music.Theory.Bool where

import Data.List {- base -}

{- | If-then-else as a function.

> ifThenElse True "true" "false" == "true"
-}
ifThenElse :: Bool -> a -> a -> a
ifThenElse :: forall a. Bool -> a -> a -> a
ifThenElse Bool
p a
q a
r = if Bool
p then a
q else a
r

{- | Case analysis as a function.
     Find first key that is True else elseValue.

> caseElse "z" [(True,"x"),(False,"y")] == "x"
> caseElse "z" [(False,"x"),(False,"y")] == "z"
-}
caseElse :: t -> [(Bool, t)] -> t
caseElse :: forall t. t -> [(Bool, t)] -> t
caseElse t
elseValue = forall b a. b -> (a -> b) -> Maybe a -> b
maybe t
elseValue forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find forall a b. (a, b) -> a
fst

{- | Case-of analysis as a function.
     Find first key that compares equal to selectValue else elseValue.

> caseOfElse "z" 'b' [('a',"x"),('b',"y")] == "y"
-}
caseOfElse :: Eq k => v -> k -> [(k, v)] -> v
caseOfElse :: forall k v. Eq k => v -> k -> [(k, v)] -> v
caseOfElse v
elseValue k
selectValue = forall b a. b -> (a -> b) -> Maybe a -> b
maybe v
elseValue forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find ((forall a. Eq a => a -> a -> Bool
== k
selectValue) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst)