[Haskell-cafe] mixing map and mapM ?
wren ng thornton
wren at freegeek.org
Sun May 9 07:18:47 EDT 2010
Pierre-Etienne Meunier wrote:
> This way :
>
> do
> times<-mapM PF.getFileStatus filenames >>= return.(map PF.modificationTime)
>
> Or also :
>
> do
> times<-mapM (PF.getFileStatus >>= (return.(PF.modificationTime))) filenames
> let sorted=...
>
> I do not know exactly how ghc compiles the IO monad, but it seems to me that the latter would allocate a little less.
FWIW, (a >>= (return . f)) == (liftM f a) ~= (fmap f a)
Where available, the fmap version is the most efficient. The liftM
function can be less efficient since it's defined generically (namely
with the bind/return definition above), whereas fmap can take advantage
of knowing the specific monad it's working on. But then, not everyone
defines Functor instances for their monads...
--
Live well,
~wren
More information about the Haskell-Cafe
mailing list