Skip to content

Instantly share code, notes, and snippets.

@tokiwoousaka
Created July 25, 2015 05:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tokiwoousaka/0104d35348992082ff47 to your computer and use it in GitHub Desktop.
Save tokiwoousaka/0104d35348992082ff47 to your computer and use it in GitHub Desktop.
module Lib where
import Control.Lens
data Foo = Foo { a :: Int, b :: String, c :: Char } deriving Show
data Bar = Bar { x :: Int, y :: String } deriving Show
constructBar :: Lens Foo Foo Bar Bar
constructBar = lens getFoo2Bar setFooFromBar
getFoo2Bar :: Foo -> Bar
getFoo2Bar (Foo a b _) = Bar a b
setFooFromBar :: Foo -> Bar -> Foo
setFooFromBar (Foo _ _ c) (Bar a b) = Foo a b c
test :: IO ()
test = do
let foo1 = Foo 100 "Hello" 'a'
let bar1 = Bar 200 "Lens"
print foo1
print bar1
print $ foo1^.constructBar
print $ foo1&constructBar.~bar1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment