Created
May 14, 2018 17:26
-
-
Save jship/b94ec5c660efc6d722479c5b8f79ef70 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env stack | |
-- stack --resolver lts-10.8 --install-ghc exec ghci --package lens | |
{-# LANGUAGE TemplateHaskell #-} | |
import Control.Lens (Getter, makeLenses, view) | |
data Foo = Foo | |
{ _fooInt :: Int | |
, _fooString :: String | |
, _fooDouble :: Double | |
} | |
data Bar = Bar | |
{ _barInt :: Int | |
, _barString :: String | |
} deriving (Show) | |
makeLenses ''Foo | |
makeLenses ''Bar | |
-- type Getter s a = forall f. (Contravariant f, Functor f) => (a -> f a) -> s -> f s | |
-- barFromFoo :: forall f. (Contravariant f, Functor f) => (Bar -> f Bar) -> Foo -> f Foo | |
barFromFoo :: Getter Foo Bar | |
barFromFoo f foo = fmap (const foo) (f bar) where | |
bar = Bar intPart stringPart | |
intPart = view fooInt foo | |
stringPart = view fooString foo | |
main :: IO () | |
main = print . view barFromFoo $ foo where | |
foo = Foo 42 "abcd" 33.3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment