[Haskell-cafe] mixing map and mapM ?

briand at aracnet.com briand at aracnet.com
Thu May 6 01:20:47 EDT 2010


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


More information about the Haskell-Cafe mailing list