Skip to content

Instantly share code, notes, and snippets.

@zereraz
Last active August 11, 2018 13:34
Show Gist options
  • Save zereraz/b9a15424006e2aeccf9094b111f1dc5d to your computer and use it in GitHub Desktop.
Save zereraz/b9a15424006e2aeccf9094b111f1dc5d to your computer and use it in GitHub Desktop.
module Main where
import Prelude
import Effect (Effect)
import Effect.Console (log)
data Maybe a = Nothing | Just a
a :: Maybe Int
a = Just 5
e :: Either String Int
e = Right 5
data Tree a = Leaf a | Branch a (Tree a) (Tree a)
data Tree2 = Leaf2 String | Branch2 String Tree2 Tree2
treeMap :: (String -> String) -> Tree2 -> Tree2
treeMap f (Leaf2 s) = Leaf2 (f s)
treeMap f (Branch2 s tree1 tree2) = Branch2 (f s) (treeMap f tree1) (treeMap f tree2)
treeMap' :: forall a b. (a -> b) -> Tree a -> Tree b
treeMap' f (Leaf a) = Leaf (f a)
treeMap' f (Branch a tree1 tree2) = Branch (f a) (map f tree1) (map f tree2)
instance treeFunc :: Functor Tree where
map f (Leaf a) = Leaf (f a)
map f (Branch a tree1 tree2) = Branch (f a) (map f tree1) (map f tree2)
fn1 :: forall a b c . a -> b -> c -> b
fn1 _ b _ = b
fn2 :: forall b. b -> Unit
fn2 b = unit
fn3 :: forall b c. (b -> b) -> b -> b
fn3 f = f <<< f <<< f
c :: forall b. b -> Unit
c = fn2 <<< fn1
class Functor f where
map :: forall a b. (a -> b) -> f a -> f b
data Either a b = Left a | Right b
data Tuple a b = Tuple a b
data Predicate a = Predicate (a -> Boolean)
class Contravariant f where
contramap :: forall a b. (b -> a) -> f a -> f b
instance contraPredicate :: Contravariant (Predicate) where
contramap f (Predicate f') = Predicate (f' <<< f)
odd :: Predicate Int
odd = Predicate (\i -> mod i 2 `(not eq)` 0)
instance functionFunctor :: Functor (Function r) where
map f f' = f <<< f'
instance eitherFunctor :: Functor (Either a) where
map f (Right r) = Right (f r)
map _ (Left l) = Left l
class Bifunctor f where
bimap :: forall a b c d. (a -> b) -> (c -> d) -> f a c -> f b d
instance eitherBimap :: Bifunctor (Either) where
bimap f _ (Left a) = Left $ f a
bimap _ g (Right r) = map g (Right r)
instance tupleBimap :: Bifunctor (Tuple) where
bimap f g (Tuple a b) = Tuple (f a) (g b)
main :: Effect Unit
main = do
let tInt = Branch 5 (Leaf 6) (Leaf 7)
let tStr = map show tInt
pure unit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment