Skip to content

Instantly share code, notes, and snippets.

@nkpart
Created December 22, 2015 01:20
Show Gist options
  • Save nkpart/3bcd2bc689eba663cb0b to your computer and use it in GitHub Desktop.
Save nkpart/3bcd2bc689eba663cb0b to your computer and use it in GitHub Desktop.
--
import Data.Map.Lazy
import Control.Lens
-- We wanted to traverse a structure, but not recompute the function for duplicate keys. And then we also needed cache.
-- It's probably tricky to unlens this.
traverseMemoOf :: (Ord a, Monad f) => Traversal s t a b -> (a -> f b) -> s -> f (t, ML.Map a b)
traverseMemoOf l f z =
runStateT (traverseOf l g z) ML.empty
where g dt = do r <- gets (ML.lookup dt)
case r of
Nothing -> do r' <- lift $ f dt
modify $ ML.insert dt r'
return $! r'
Just v -> return $! v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment