Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching for missing State Combinator for Lens

I currently have code that looks like this:

do
  x <- use foo
  foo <~ runFoo x

where foo is a Lens to a Foo field and runFoo :: MonadState m => Foo -> m Foo

I think there should be a way to do this operation in one line, but I cannot find it. I think it ought to have a comment like:

(????) :: MonadState s m => Lens' s a -> (a -> m a) -> m a

Questions:

  • Does such a combinator exist? if so what is it?
  • When I run into another question like this, what is the best way to search for it (i.e. Usually I would just type this into Hoogle but I've not had good luck doing that with the lens library)
  • Is this actually a primitive combinator found in Control.Monad? (I will be slightly embarrassed if this is yet another job for the kleisli arrow)
like image 744
John F. Miller Avatar asked Nov 11 '22 22:11

John F. Miller


1 Answers

lens doesn't offer such a combinator in a ready-made form. If instead of runFoo you had some g :: a -> a function, you would be able to use (%=), which is to over what use is to view, and write simply foo %= g. Your desired functionality, though, involves interleaving the m effects introduced by runFoo in between the getting and setting, which precludes a solution this straightforward.

like image 126
duplode Avatar answered Nov 15 '22 07:11

duplode