Skip to content

Instantly share code, notes, and snippets.

@ljsc
Created August 16, 2013 14:25
Show Gist options
  • Save ljsc/6250375 to your computer and use it in GitHub Desktop.
Save ljsc/6250375 to your computer and use it in GitHub Desktop.
Combining contravariant functors?
type Prop = String
type Value = String
newtype ChartConf = Conf { toList :: [(Prop, Value)] } deriving Show
newtype Charter a = Charter { runCharter :: a -> ChartConf }
instance Monoid ChartConf where
mempty = Conf []
c1 `mappend` c2 = Conf $ toList c1 `mappend` toList c2
c1 :: Charter [Int]
c1 = Charter $ \ xs -> Conf [("length", (show . length) xs)]
c2 :: Charter String
c2 = Charter $ \ str -> Conf [("rev", reverse str)]
x :: (Charter a, c -> a) -> (Charter b, c -> b) -> Charter c
(a, f) `x` (b, g) = Charter $ \ x -> runCharter a (f x) `mappend` runCharter b (g x)
c3 = (c1, fst) `x` (c2, snd)
z = runCharter ((c3, fstsnd) `x` (c2, third)) $ payload
fstsnd (x,y,z) = (x,y)
third (x,y,z) = z
payload = ([1..10], "foo", "foobar")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment