Skip to content

Instantly share code, notes, and snippets.

@tim2CF
Created May 4, 2020 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tim2CF/da8b0d4bed8d18d279c661cbf337dc12 to your computer and use it in GitHub Desktop.
Save tim2CF/da8b0d4bed8d18d279c661cbf337dc12 to your computer and use it in GitHub Desktop.
-- pseudo-code, let's consider these types have corresponding lensens
-- from package proto-lens https://github.com/google/proto-lens
data Foo = Foo {foo :: Maybe Bar}
data Bar = Bar {bar :: Maybe Buz}
data Buz
= Buz
{ buz :: Bool,
buf :: Int,
bif :: Integer
}
-- I'm using bind here to handle Maybe
-- And compose Just values into tuple
-- Is there better/shorter way to do it
-- with some lens-specific functions?
-- Something applicative-like style for lens?
accessBuzBufBif :: Foo -> Maybe (Bool, Int, Integer)
accessBuzBufBif x = do
buz' <- x ^? mbar . buz
buf' <- x ^? mbar . buf
bif' <- x ^? mbar . bif
return (buz', buf', bif')
where
mbar = maybe'foo . _Just . maybe'bar . _Just
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment