[project @ 2005-01-17 11:08:52 by simonmar]
authorsimonmar <unknown>
Mon, 17 Jan 2005 11:08:52 +0000 (11:08 +0000)
committersimonmar <unknown>
Mon, 17 Jan 2005 11:08:52 +0000 (11:08 +0000)
Add Data.FunctorM

Data/FunctorM.hs [new file with mode: 0644]
package.conf.in

diff --git a/Data/FunctorM.hs b/Data/FunctorM.hs
new file mode 100644 (file)
index 0000000..193047d
--- /dev/null
@@ -0,0 +1,42 @@
+-----------------------------------------------------------------------------
+-- |
+-- Module      :  Data.FunctorM
+-- Copyright   :  (c) The University of Glasgow 2005
+-- License     :  BSD-style (see the file libraries/base/LICENSE)
+-- 
+-- Maintainer  :  libraries@haskell.org
+-- Stability   :  provisional
+-- Portability :  portable
+--
+-- fmapM generalises fmap, just as mapM generalises map.
+--
+-----------------------------------------------------------------------------
+
+module FunctorM (
+       FunctorM(..)
+  ) where
+
+import Prelude
+import Data.Array
+
+class FunctorM f where
+    fmapM  :: Monad m => (a -> m b) -> f a -> m (f b)
+    fmapM_ :: Monad m => (a -> m b) -> f a -> m ()
+
+    fmapM_ f t = fmapM f t >> return ()
+
+instance FunctorM [] where
+    fmapM  = mapM
+    fmapM_ = mapM_
+
+instance FunctorM Maybe where
+    fmapM _ Nothing = return Nothing
+    fmapM f (Just x) = f x >>= return . Just 
+
+    fmapM_ _ Nothing = return ()
+    fmapM_ f (Just x) = f x >> return ()
+
+instance Ix i => FunctorM (Array i) where
+    fmapM f a = do 
+       a' <- sequence [ f e >>= return . (,) i | (i,e) <- assocs a]
+       return (array (bounds a) a')
index 2ddecaf..4f59dc5 100644 (file)
@@ -39,6 +39,7 @@ exposed-modules:
        Data.Dynamic,
        Data.Either,
        Data.FiniteMap,
+       Data.FunctorM,
        Data.Generics,
        Data.Generics.Aliases,
        Data.Generics.Basics,