1
1
mirror of https://github.com/github/semantic.git synced 2024-12-25 16:02:43 +03:00

Add a generalization of memoize to Enums.

This commit is contained in:
Rob Rix 2016-06-06 22:14:19 -04:00
parent 8096dcd588
commit e31b8e3f57

View File

@ -84,6 +84,9 @@ diffAt' diffTerms cost as bs recur (i, j)
memoize :: (Int -> a) -> (Int -> a) memoize :: (Int -> a) -> (Int -> a)
memoize f = (fmap f [0 ..] !!) memoize f = (fmap f [0 ..] !!)
memoizeEnum :: Enum a => (a -> b) -> a -> b
memoizeEnum f = (fmap f [toEnum 0 ..] !!) . fromEnum
memoize2 :: ((Int, Int) -> a) -> ((Int, Int) -> a) memoize2 :: ((Int, Int) -> a) -> ((Int, Int) -> a)
memoize2 f = fromJust . (`lookup` memo) memoize2 f = fromJust . (`lookup` memo)
where memo = zipWith (\ i j -> ((i, j), f (i, j))) [0 ..] [0 ..] where memo = zipWith (\ i j -> ((i, j), f (i, j))) [0 ..] [0 ..]