[Haskell-cafe] Using State monad with lens

Michael Hull mikehulluk at gmail.com
Thu Apr 2 08:01:28 UTC 2020


Thankyou everybody - this is really helpful, especially the \case technique.
@Juan I am not sure my Haskell is strong enough to use zoom yet, but I will
look into it!
Thanks again.

Mike


On Sat, 21 Mar 2020 at 21:12, Juan Casanova <juan.casanova at ed.ac.uk> wrote:

> 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.
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20200402/68dfd441/attachment.html>


More information about the Haskell-Cafe mailing list