Skip to content

Instantly share code, notes, and snippets.

@iokasimov
Last active January 10, 2018 15:06
Show Gist options
  • Save iokasimov/ed5f16b1c9fa55af34a7aeb941fdc23b to your computer and use it in GitHub Desktop.
Save iokasimov/ed5f16b1c9fa55af34a7aeb941fdc23b to your computer and use it in GitHub Desktop.
Lenses are exactly the Coalgebras for the Store Comonad
import Data.Function
import Control.Comonad.Store
type Lens a b = a -> Store b a
(^.) :: Lens a b -> a -> b
(^.) lens = pos . lens
(.~) :: Lens a b -> b -> a -> a
lens .~ new = peek new . lens
type Name = String
type Address = String
type Job = Maybe String
data Person = Person Name Address Job deriving Show
name :: Lens Person Name
name (Person n a j) = store (\new -> Person new a j) n
me :: Person
me = Person "Murat Kasimov" "Serokell" Nothing
main = do
print "typechecked"
print $ name ^. me
print $ me & name .~ "Clint Eastwood"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment