[Haskell-cafe] mixing map and mapM ?

Bill Atkins watkins at alum.rpi.edu
Thu May 6 06:51:01 EDT 2010


Almost - "liftM modificationTime" has type Status -> IO EpochTime.  Like
other IO functions (getLine, putStrLn), it returns an IO action but accepts
a pure value (the modification time)

Also, I like this style:

import Control.Applicative ((<$>))

blah = do
  times <- mapM (PF.modificationTime <$> PF.getFileStatus) filenames
  ...

The <$> operator evaluates to fmap so it's a cleaner way to apply a pure
function to an IO value.

On Thu, May 6, 2010 at 1:20 AM, <briand at aracnet.com> wrote:

> On Thu, 6 May 2010 15:07:30 +1000
> Ivan Miljenovic <ivan.miljenovic at gmail.com> wrote:
>
> > On 6 May 2010 15:01,  <briand at aracnet.com> wrote:
> > >
> > > I was doing the following:
> > >
> > >
> > > do status <- mapM PF.getFileStatus filenames
> > >   let times = map PF.modificationTime status
> > >   let sorted = sortBy (\(_, t1) (_,t2) -> compare t1 t2) (zip
> > > filenames times)
> >
> > times <- mapM (liftM PF.modificationTime . PF.getFileStatus) filenames
> >
> > However, I'd be tempted to leave it as is (and hope/assume that fusion
> > does its magic).
> >
>
> well now it's obvious :-)  I did have liftM in there, but just couldn't
> quite figure out how to tie things together.
>
> to be completely clear : liftM takes modificationTime from
>
>  Status -> EpochTime
>
> to
>
>  IO Status -> IO EpochTime
>
> so now it can operate on the results of getFileStatus, which
> returns `IO Status`.
>
> mapM gathers the [IO EpochTime] into `IO [EpochTime]` and then <- gives
> [EpochTime].
>
> It's a little more clear in the verbose form, isn't it ?
>
> Thanks !
>
> Brian
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100506/46dd5b51/attachment.html


More information about the Haskell-Cafe mailing list