From 70d7c4b1cdc78fe03f63180c318d2ca15f4f6a26 Mon Sep 17 00:00:00 2001 From: simonmar Date: Mon, 17 Jan 2005 11:08:52 +0000 Subject: [PATCH] [project @ 2005-01-17 11:08:52 by simonmar] Add Data.FunctorM --- Data/FunctorM.hs | 42 ++++++++++++++++++++++++++++++++++++++++++ package.conf.in | 1 + 2 files changed, 43 insertions(+) create mode 100644 Data/FunctorM.hs diff --git a/Data/FunctorM.hs b/Data/FunctorM.hs new file mode 100644 index 0000000..193047d --- /dev/null +++ b/Data/FunctorM.hs @@ -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') diff --git a/package.conf.in b/package.conf.in index 2ddecaf..4f59dc5 100644 --- a/package.conf.in +++ b/package.conf.in @@ -39,6 +39,7 @@ exposed-modules: Data.Dynamic, Data.Either, Data.FiniteMap, + Data.FunctorM, Data.Generics, Data.Generics.Aliases, Data.Generics.Basics, -- 1.7.10.4