[Haskell-cafe] Using State monad with lens

Juan Casanova juan.casanova at ed.ac.uk
Sat Mar 21 21:11:29 UTC 2020


I'm surprised noone has mentioned 'zoom' yet. It does not make your  
original example work directly, but almost. It may be very useful to  
keep in mind as a way to do whatever you want.

http://hackage.haskell.org/package/lens-4.19.1/docs/Control-Lens-Zoom.html#v:zoom

The idea of zoom is that you use it like so:

zoom my_lens my_monadic_action

where my_monadic_action is a monadic action that works on the  
sub-element (call it 's') of the state, and the entire thing 'zoom  
my_lens my_monadic_action' is then a monadic action that works on the  
whole element (call it 't'). my_lens is a lens that focuses on 's'  
within 't'.

So instead of using the lens all the time in the monadic action,  
you're basically saying: "Put this everywhere that you need to", and  
then you can do things with the sub-element 's'.

Juan.

PS: I hope I did not severely misunderstand what you wanted, but it's  
still a possibility.

-- 
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.




More information about the Haskell-Cafe mailing list