module Music.Theory.Maybe where
import Data.Maybe
from_just :: String -> Maybe a -> a
from_just err = fromMaybe (error err)
maybe_unzip :: [Maybe (a,b)] -> ([Maybe a],[Maybe b])
maybe_unzip =
let f x = case x of
Nothing -> (Nothing,Nothing)
Just (i,j) -> (Just i,Just j)
in unzip . map f
maybe_latch :: a -> [Maybe a] -> [a]
maybe_latch i x =
case x of
[] -> []
Just e:x' -> e : maybe_latch e x'
Nothing:x' -> i : maybe_latch i x'
maybe_latch1 :: [Maybe a] -> [a]
maybe_latch1 = maybe_latch (error "maybe_latch1")
maybe_map :: (a -> b) -> [Maybe a] -> [Maybe b]
maybe_map = map . fmap
maybe_eq_by :: (t -> u -> Bool) -> Maybe t -> Maybe u -> Bool
maybe_eq_by eq_fn p q =
case (p,q) of
(Just p',Just q') -> eq_fn p' q'
_ -> False
maybe_join' :: (s -> t) -> (s -> s -> t) -> Maybe s -> Maybe s -> Maybe t
maybe_join' f g p q =
case (p,q) of
(Nothing,_) -> fmap f q
(_,Nothing) -> fmap f p
(Just p',Just q') -> Just (p' `g` q')
maybe_join :: (t -> t -> t) -> Maybe t -> Maybe t -> Maybe t
maybe_join = maybe_join' id
maybe_predicate :: (a -> Bool) -> Maybe a -> Maybe a
maybe_predicate f i =
case i of
Nothing -> Nothing
Just j -> if f j then Just j else Nothing
maybe_filter :: (a -> Bool) -> [Maybe a] -> [Maybe a]
maybe_filter = map . maybe_predicate
filter_maybe :: (a -> Bool) -> [a] -> [Maybe a]
filter_maybe f = maybe_filter f . map Just