Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A simple “zoom” which works in MonadState #580

Open
neongreen opened this issue Aug 3, 2015 · 1 comment
Open

A simple “zoom” which works in MonadState #580

neongreen opened this issue Aug 3, 2015 · 1 comment

Comments

@neongreen
Copy link
Contributor

neongreen commented Aug 3, 2015

zoom'
  :: MonadState s m
  => LensLike' (Zoomed (State s) a) s t -> StateT t Identity a -> m a
zoom' l = state . runState . zoom l

This function could be useful when you just want to edit the state quickly and don't need to do anything specific to the monad you're actually working in.

@WorldSEnder
Copy link

I use l %%= runState modification for this, where modification :: State t a. Especially with BlockArguments it's not that unreadable, for example the following works fine for me

localEnv :: Lens' GlobalEnv LocalEnv
nextFreeId :: Lens' LocalEnv Int
bindings :: Lens' LocalEnv (Map Int Foo)
Reserved :: Foo

localEnv %%= runState do
  newId <- nextFreeId <+= 1
  bindings . at newId <?= Reserved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants