Skip to content

Instantly share code, notes, and snippets.

@danidiaz
Last active April 15, 2018 13:52
Show Gist options
  • Select an option

  • Save danidiaz/ff76231a4d666415c10e7504784ff128 to your computer and use it in GitHub Desktop.

Select an option

Save danidiaz/ff76231a4d666415c10e7504784ff128 to your computer and use it in GitHub Desktop.
Teasing out fields
{-# LANGUAGE RankNTypes #-}
data Person = Person { _name :: String , _age :: Int } deriving Show
makeLenses 'Person
example :: Person
example = Person "Bob" 55
pry :: Lens' r x -> Iso' r (x -> r,x)
pry l = iso (\r -> (\x -> set l x r, view l r)) (uncurry ($))
consName :: Prism' Person (Char,Person)
consName =
pry name . aside _Cons . iso (\(a,(b,c)) -> (b,(a,c))) (\(b,(a,c)) -> (a,(b,c))) . seconding (Control.Lens.from (pry name))
-- Λ review consName ('z',example)
-- Person {_name = "zBob", _age = 55}
--
-- Λ preview consName example
-- Just ('B',Person {_name = "ob", _age = 55})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment