<html><head></head><body><div><div><div style="display: none; border: 0px; width: 0px; height: 0px; overflow: hidden; visibility: hidden;"><img src="https://r.superhuman.com/ue3YvBas2Z_MF8vJP5uuRMSJXAZfe95jDfEU0HWfJadUyaRkyg8_cpaDRR-i6PbqEjMHP6zIib9MNSq5fQigUR-2Hwb6FzC2eso2VihNO4BYy8KBL1oPiKVGv6tZ9RfAovYX4AbhdWptpoywVkPEqcnzGTv4-4BYEGv407OyShKe9oyzfckUGXW-gps.gif" alt=" " width="1" height="0" style="display: none; border: 0px; width: 0px; height: 0px; overflow: hidden; visibility: hidden;"/><!--                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                --></div><div><div><div><div>Normally, you'd write `gets` to access entries in your state environment according to some function (e.g. record accessors), but since `some_property` is itself (presumably) a `Lens` or `Getter`, you should be able to use the lensy-State accessor `use`(<a href="https://github.com/ekmett/lens/blob/e06c171840bbe86c789b8dff8c8211b88ac9211e/src/Control/Lens/Getter.hs#L347">https://github.com/ekmett/lens/blob/e06c171840bbe86c789b8dff8c8211b88ac9211e/src/Control/Lens/Getter.hs#L347</a>). Then, your script becomes: <br/></div><div><br/></div><div><div>```<br/></div><div><span style="text-decoration-color:initial;text-decoration-style:initial;">myFunc :: StateT MyState [ResultType]<br/></span>myFunc = do</div></div><div>  p ← use some_property <br/></div><div>  case p of <br/></div><div><div>    True → do my_value .= "Hello"<br/></div><div>    False → do my_value .= "Goodbye"<br/></div><div>```<br/></div><div><br/></div><div>`use` works in the same sense that `view` does for `MonadReader r m ⇒ m`. It even has an analog to `preview` called `preuse`, which you can use for `Prism`s in your state environment.  <br/></div></div><div><br/></div><div>Cheers, hope that helps,<br/></div><div>Emily<br/></div></div><div><br/></div></div><br/><div class="gmail_signature"></div></div><br/><div><div class="gmail_quote">On Sat, Mar 21, 2020 at 10:42 AM, Michael Hull <span dir="ltr"><<a href="mailto:mikehulluk@gmail.com" target="_blank">mikehulluk@gmail.com</a>></span> wrote:<br/><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="gmail_extra"><div class="gmail_quote sh-color-black sh-color" style="null" id="null"><div dir="ltr" class="sh-color-black sh-color">Hi,<div class="sh-color-black sh-color">I use the State monad in a lot of places, but when I use it with the lens library, I find myself writing code like:</div><div class="sh-color-black sh-color"><br/></div><div class="sh-color-black sh-color">myFunc :: StateT MyState ([ResultType])</div><div class="sh-color-black sh-color">myFunc = do</div><div class="sh-color-black sh-color">   st <- get</div><div class="sh-color-black sh-color">   case st ^. some_property of</div><div class="sh-color-black sh-color">      True -> do</div><div class="sh-color-black sh-color">            my_value .= "Hello"</div><div class="sh-color-black sh-color">      True -> do</div><div class="sh-color-black sh-color">            my_value .= "Goodbye"</div><div class="sh-color-black sh-color"></div><div class="sh-color-black sh-color">         </div><div class="sh-color-black sh-color">I want to do something based on a 'state-variable', but I have to write 'get' -- is there a pattern that allows me to avoid the intermediate 'st' variable? </div><div class="sh-color-black sh-color">I want to write something like:</div><div class="sh-color-black sh-color"><div class="sh-color-black sh-color"><br/></div><div class="sh-color-black sh-color">myFunc = do</div><div class="sh-color-black sh-color">   case (get ^. some_property) of</div><div class="sh-color-black sh-color">      True -> do</div><div class="sh-color-black sh-color">            my_value .= "Hello"</div><div class="sh-color-black sh-color">      True -> do</div><div class="sh-color-black sh-color">            my_value .= "Goodbye"</div><div class="sh-color-black sh-color"></div><div class="sh-color-black sh-color"></div></div><div class="sh-color-black sh-color"><br/></div><div class="sh-color-black sh-color">but that won't compile.</div><div class="sh-color-black sh-color"><br/></div><div class="sh-color-black sh-color">Best wishes,</div><div class="sh-color-black sh-color"><br/></div><div class="sh-color-black sh-color">Mike</div><div class="sh-color-black sh-color"><br/></div><div class="sh-color-black sh-color"><br/></div><div class="sh-color-black sh-color"><br/></div></div>

<p class="sh-color-black sh-color">_______________________________________________
<br/>
Haskell-Cafe mailing list
<br/>
To (un)subscribe, modify options or view archives go to:
<a target="_blank" rel="noopener noreferrer" href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe">http:/<wbr/>/<wbr/>mail.<wbr/>haskell.<wbr/>org/<wbr/>cgi-bin/<wbr/>mailman/<wbr/>listinfo/<wbr/>haskell-cafe</a>
Only members subscribed via the mailman list are allowed to post.</p></div></div></blockquote></div></div><br/></div></div></body></html>