1 module CLasH.Utils where
6 import qualified Data.Map as Map
7 import qualified Control.Monad as Monad
8 import qualified Control.Monad.Trans.State as State
14 -- Make a caching version of a stateful computatation.
15 makeCached :: (Monad m, Ord k) =>
16 k -- ^ The key to use for the cache
17 -> Accessor s (Map.Map k v) -- ^ The accessor to get at the cache
18 -> State.StateT s m v -- ^ How to compute the value to cache?
19 -> State.StateT s m v -- ^ The resulting value, from the cache or freshly
21 makeCached key accessor create = do
22 cache <- getA accessor
23 case Map.lookup key cache of
24 -- Found in cache, just return
25 Just value -> return value
26 -- Not found, compute it and put it in the cache
29 modA accessor (Map.insert key value)
32 unzipM :: (Monad m) =>
35 unzipM = Monad.liftM unzip
37 catMaybesM :: (Monad m) =>
40 catMaybesM = Monad.liftM Maybe.catMaybes
42 concatM :: (Monad m) =>
45 concatM = Monad.liftM concat
47 isJustM :: (Monad m) => m (Maybe a) -> m Bool
48 isJustM = Monad.liftM Maybe.isJust
50 andM, orM :: (Monad m) => m [Bool] -> m Bool
51 andM = Monad.liftM and
54 mapAccumLM :: (Monad m) => (acc -> x -> m (acc, y)) -> acc -> [x] -> m (acc, [y])
55 mapAccumLM _ s [] = return (s, [])
56 mapAccumLM f s (x:xs) = do
58 (s'', ys) <- mapAccumLM f s' xs