title: Does there exist a monad instance for Data.Map / Data.IntMap?
body: <p>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:</p>
  
  <ul>
  <li>Look for value X in the map.</li>
  <li>If it matches a criteria, remove this value from the map.</li>
  <li>Loop until no more values exist in the map.</li>
  </ul>
  
  <p>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 <code>do</code> notation.</p>
  
  <p>Is there a standard "State"-like monad where the state is represented by <code>Data.Map</code> or <code>Data.IntMap</code>, where I can do something like:</p>
  
  <pre><code>do
    insert 5 20
    (ma, mv) &lt;- lookup 4
    case mv of
      Just v -&gt; delete (fromJust ma)
      Nothing -&gt; return ()
  </code></pre>
  
  <p>Honestly I'm not sure how to best express this.. due to <code>lookup</code> it would seem to benefit from some kind of <code>MaybeT IntMap m</code> stack or something.</p>
  
  <p>I did do a bit of work trying to define my own state monad based on <code>Data.IntMap</code>, even got as far as making <code>insert</code> and <code>delete</code> work, but got a little stuck with how to deal with <code>lookup</code>.  Mostly i feel like this is probably something someone has already solved, but I can't find it on Hackage.</p>