What am I reinventing with toMaybe?
| import Data.Monoid | |
| import Control.Applicative | |
| import Control.Category(Category) | |
| import qualified Control.Category as C | |
| class Functor f => Maybeable f where | |
| toMaybe :: | |
| f a | |
| -> Maybe a | |
| newtype Id a = | |
| Id a | |
| deriving (Eq, Show) | |
| instance Functor Id where | |
| fmap f (Id a) = | |
| Id (f a) | |
| instance Maybeable Id where | |
| toMaybe (Id a) = | |
| Just a | |
| data Trivial a = | |
| Trivial | |
| instance Functor Trivial where | |
| fmap _ _ = | |
| Trivial | |
| instance Maybeable Trivial where | |
| toMaybe _ = | |
| Nothing | |
| instance Maybeable [] where | |
| toMaybe (h:_) = | |
| Just h | |
| toMaybe [] = | |
| Nothing | |
| instance Maybeable Maybe where | |
| toMaybe = | |
| id | |
| newtype HeadTail a = | |
| HeadTail [a] | |
| deriving (Eq, Show) | |
| instance Functor HeadTail where | |
| fmap f (HeadTail a) = | |
| HeadTail (fmap f a) | |
| instance Maybeable HeadTail where | |
| toMaybe (HeadTail (_:h':_)) = | |
| Just h' | |
| toMaybe _ = | |
| Nothing | |
| newtype HeadTailTail a = | |
| HeadTailTail [a] | |
| deriving (Eq, Show) | |
| instance Functor HeadTailTail where | |
| fmap f (HeadTailTail a) = | |
| HeadTailTail (fmap f a) | |
| instance Maybeable HeadTailTail where | |
| toMaybe (HeadTailTail (_:_:h':_)) = | |
| Just h' | |
| toMaybe _ = | |
| Nothing | |
| maybed :: | |
| Maybeable f => | |
| x | |
| -> (a -> x) | |
| -> f a | |
| -> x | |
| maybed n j f = | |
| case toMaybe f of | |
| Nothing -> n | |
| Just a -> j a | |
| (??.) :: | |
| Maybeable f => | |
| a | |
| -> f a | |
| -> a | |
| a ??. f = | |
| maybed a id f | |
| (??) :: | |
| Maybeable f => | |
| f a | |
| -> a | |
| -> a | |
| (??) = | |
| flip (??.) | |
| (<?>) :: | |
| Maybeable f => | |
| f a | |
| -> f a | |
| -> f a | |
| a <?> b = | |
| maybed b (const a) a | |
| is :: | |
| Maybeable f => | |
| f a | |
| -> Bool | |
| is = | |
| maybed False (const True) | |
| isnot :: | |
| Maybeable f => | |
| f a | |
| -> Bool | |
| isnot = | |
| not . is | |
| forall :: | |
| Maybeable f => | |
| (a -> Bool) | |
| -> f a | |
| -> Bool | |
| forall p = | |
| maybed True p | |
| exists :: | |
| Maybeable f => | |
| (a -> Bool) | |
| -> f a | |
| -> Bool | |
| exists p = | |
| maybed False p | |
| orempty :: | |
| (Monoid a, Maybeable f) => | |
| f a | |
| -> a | |
| orempty a = | |
| a ?? mempty | |
| isempty :: | |
| (Eq a, Monoid a, Maybeable f) => | |
| f a | |
| -> Bool | |
| isempty = | |
| exists (== mempty) | |
| thenid :: | |
| (Category c, Maybeable f) => | |
| c a a | |
| -> f a | |
| -> c a a | |
| thenid q a = | |
| if is a | |
| then | |
| C.id | |
| else | |
| q | |
| elseid :: | |
| (Category c, Maybeable f) => | |
| c a a | |
| -> f a | |
| -> c a a | |
| elseid q a = | |
| if is a | |
| then | |
| q | |
| else | |
| C.id | |
| (.=<<) :: | |
| (Maybeable f, Alternative g) => | |
| (a -> g b) | |
| -> f a | |
| -> g b | |
| (.=<<) = | |
| maybed empty | |
| (.<*>) :: | |
| (Maybeable f, Maybeable g, Alternative h) => | |
| f (a -> b) | |
| -> g a | |
| -> h b | |
| f .<*> a = | |
| (\f' -> (f' $) .<$> a) .=<< f | |
| (.<$>) :: | |
| (Maybeable f, Alternative g) => | |
| (a -> b) | |
| -> f a | |
| -> g b | |
| (.<$>) f = | |
| (.=<<) (pure . f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment