<div dir="ltr">Which as others have mentioned, works well with lambda case, here's an example from a game I am writing:<div><br></div><div>```</div><div>runPhase :: (MonadIO m, MonadState GameState m) => Card -> m ()<br>runPhase c@Card {..} = use phase >>= \case<br> HeadlinePhase -> liftIO . putStrLn $ "Drew: " ++ show c<br> EventResolutionPhase -> liftIO . putStrLn $ "Event"<br> ArmyMovementPhase -> traverse_ moveArmy armyMovements<br> ActionPhase -> liftIO . putStrLn $ "Show me your moves!"<br> KaiserschlachtPhase -> pure ()<br> NationalWillPhase -> do<br> nw <- nationalWill<br> if nw <= -5 then gameOver else pure ()<br> GameOver -> liftIO . putStrLn $ "Game over"<br></div><div>```</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Mar 21, 2020 at 11:34 AM Emily Pillmore <<a href="mailto:emilypi@cohomolo.gy">emilypi@cohomolo.gy</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div><div><div style="display:none;border:0px;width:0px;height:0px;overflow: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;"></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" target="_blank">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></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:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="gmail_extra"><div class="gmail_quote" id="gmail-m_2102201945752204159null"><div dir="ltr">Hi,<div>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><br></div><div>myFunc :: StateT MyState ([ResultType])</div><div>myFunc = do</div><div> st <- get</div><div> case st ^. some_property of</div><div> True -> do</div><div> my_value .= "Hello"</div><div> True -> do</div><div> my_value .= "Goodbye"</div><div></div><div> </div><div>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>I want to write something like:</div><div><div><br></div><div>myFunc = do</div><div> case (get ^. some_property) of</div><div> True -> do</div><div> my_value .= "Hello"</div><div> True -> do</div><div> my_value .= "Goodbye"</div><div></div><div></div></div><div><br></div><div>but that won't compile.</div><div><br></div><div>Best wishes,</div><div><br></div><div>Mike</div><div><br></div><div><br></div><div><br></div></div>
<p>_______________________________________________
<br>
Haskell-Cafe mailing list
<br>
To (un)subscribe, modify options or view archives go to:
<a rel="noopener noreferrer" href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a>
Only members subscribed via the mailman list are allowed to post.</p></div></div></blockquote></div></div><br></div></div></div>_______________________________________________<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>