Skip to content

Instantly share code, notes, and snippets.

@jship
Created May 14, 2018 17:26
Show Gist options
  • Save jship/b94ec5c660efc6d722479c5b8f79ef70 to your computer and use it in GitHub Desktop.
Save jship/b94ec5c660efc6d722479c5b8f79ef70 to your computer and use it in GitHub Desktop.
#!/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