[project @ 2005-10-21 10:26:57 by ross]
[packages/old-time.git] / Data / FunctorM.hs
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module : Data.FunctorM
4 -- Copyright : (c) The University of Glasgow 2005
5 -- License : BSD-style (see the file libraries/base/LICENSE)
6 --
7 -- Maintainer : libraries@haskell.org
8 -- Stability : provisional
9 -- Portability : portable
10 --
11 -- fmapM generalises fmap, just as mapM generalises map.
12 --
13 -----------------------------------------------------------------------------
14
15 module Data.FunctorM (
16 FunctorM(..)
17 ) where
18
19 import Prelude
20 import Data.Array
21
22 class FunctorM f where
23 fmapM :: Monad m => (a -> m b) -> f a -> m (f b)
24 fmapM_ :: Monad m => (a -> m b) -> f a -> m ()
25
26 fmapM_ f t = fmapM f t >> return ()
27
28 instance FunctorM [] where
29 fmapM = mapM
30 fmapM_ = mapM_
31
32 instance FunctorM Maybe where
33 fmapM _ Nothing = return Nothing
34 fmapM f (Just x) = f x >>= return . Just
35
36 fmapM_ _ Nothing = return ()
37 fmapM_ f (Just x) = f x >> return ()
38
39 instance Ix i => FunctorM (Array i) where
40 fmapM f a = do
41 a' <- sequence [ f e >>= return . (,) i | (i,e) <- assocs a]
42 return (array (bounds a) a')