Created
December 22, 2015 01:20
-
-
Save nkpart/3bcd2bc689eba663cb0b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | |
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