[Haskell-cafe] Using State monad with lens

Michael Hull mikehulluk at gmail.com
Sat Mar 21 14:42:43 UTC 2020


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 like:

myFunc :: StateT MyState ([ResultType])
myFunc = do
   st <- get
   case st ^. some_property of
      True -> do
            my_value .= "Hello"
      True -> do
            my_value .= "Goodbye"

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?
I want to write something like:

myFunc = do
   case (get ^. some_property) of
      True -> do
            my_value .= "Hello"
      True -> do
            my_value .= "Goodbye"

but that won't compile.

Best wishes,

Mike
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20200321/35e08423/attachment.html>


More information about the Haskell-Cafe mailing list