<div dir="ltr">LambdaCase is useful for things like this. </div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Mar 21, 2020 at 10:50 PM Henning Thielemann <<a href="mailto:lemming@henning-thielemann.de">lemming@henning-thielemann.de</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><br>
On Sat, 21 Mar 2020, Michael Hull wrote:<br>
<br>
> Hi,I use the State monad in a lot of places, but when I use it with the lens library, I find myself writing code<br>
> like:<br>
> <br>
> myFunc :: StateT MyState ([ResultType])<br>
> myFunc = do<br>
> st <- get<br>
> case st ^. some_property of<br>
> True -> do<br>
> my_value .= "Hello"<br>
> True -> do<br>
> my_value .= "Goodbye"<br>
> <br>
> I want to do something based on a 'state-variable', but I have to write 'get' -- is there a pattern that allows<br>
> me to avoid the intermediate 'st' variable? <br>
<br>
gets (^. some_property)<br>
<br>
But you still need a local variable for the result of 'gets'.<br>
<br>
<br>
> I want to write something like:<br>
> <br>
> myFunc = do<br>
> case (get ^. some_property) of<br>
> True -> do<br>
> my_value .= "Hello"<br>
> True -> do<br>
> my_value .= "Goodbye"<br>
> <br>
> but that won't compile.<br>
<br>
There cannot be an according accessor, because getting the state needs <br>
access to the state in the monad. One could at least think of using <br>
LambdaCase._______________________________________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
Only members subscribed via the mailman list are allowed to post.</blockquote></div>