Skip to content

Instantly share code, notes, and snippets.

@jacobm
Created March 10, 2016 13:46
Show Gist options
  • Save jacobm/c2807c0a7d71f396b01d to your computer and use it in GitHub Desktop.
Save jacobm/c2807c0a7d71f396b01d to your computer and use it in GitHub Desktop.
typeclasses purescript
module Main where
import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)
data MyMaybe a = None | Some a
instance functorMyMaybe :: Functor MyMaybe where
map _ None = None
map f (Some x) = Some (f x)
--
-- class (Functor f) <= Apply f where
-- apply :: forall a b. f (a -> b) -> f a -> f b
--f (a -> b) -> f a -> f b
-- MyMaybe (a -> b) -> MyMaybe a -> MyMaybe b
instance applyMyMaybe :: Apply MyMaybe where
apply None _ = None
apply (Some f) x = map f x
instance applicativeMyMaybe :: Applicative MyMaybe where
pure x = Some x
instance bindMyMaybe :: Bind MyMaybe where
bind None _ = None
bind (Some x) f = f x
instance monadMyMaybe :: Monad MyMaybe
-- ///////
data Result fail suc = Success suc | Failure fail
instance functorResult :: Functor (Result a) where
map _ (Failure x) = Failure x
map f (Success res) = Success (f res)
instance applyResult :: Apply (Result a) where
apply (Failure x) _ = Failure x
apply (Success f) x = map f x
instance applicativeResult :: Applicative (Result a) where
pure x = Success x
-- bind :: forall a b. m a -> (a -> m b) -> m b
-- Result fail a -> (a -> Result fail b) -> Result fail b
instance bindResult :: Bind (Result a) where
bind (Failure x) _ = Failure x
bind (Success x) f = f x
instance monadResult :: Monad (Result a)
main :: forall e. Eff (console :: CONSOLE | e) Unit
main = do
log "Hello sailor!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment