I have an algorithm that operates on an IntMap that I feel would best be expressed imperatively. That is, I'd like to say things like:
- Look for value X in the map.
- If it matches a criteria, remove this value from the map.
- Loop until no more values exist in the map.
This would be fairly trivial to express as a two-line recursion, but the actual algorithm is a little more complex, involving several lookups and deletions, so I'd like to be able to express it in do
notation.
Is there a standard "State"-like monad where the state is represented by Data.Map
or Data.IntMap
, where I can do something like:
do
insert 5 20
(ma, mv) <- lookup 4
case mv of
Just v -> delete (fromJust ma)
Nothing -> return ()
Honestly I'm not sure how to best express this.. due to lookup
it would seem to benefit from some kind of MaybeT IntMap m
stack or something.
I did do a bit of work trying to define my own state monad based on Data.IntMap
, even got as far as making insert
and delete
work, but got a little stuck with how to deal with lookup
. Mostly i feel like this is probably something someone has already solved, but I can't find it on Hackage.