[Haskell-cafe] usage of traversal

Kazu Yamamoto (=?iso-2022-jp?B?GyRCOzNLXE9CSScbKEI=?=) kazu at iij.ad.jp
Tue Sep 23 05:27:34 UTC 2014


Hi,

Recently, I need to write the following code:

    getModTime :: Maybe FilePath -> IO (Maybe UTCTime)
    getModTime mfile = case mfile of
        Nothing -> return Nothing
        Just file -> Just <$> getModificationTime file

I feel that this is redundant. So, I used 'traverse' instead:

    getModTime :: Maybe FilePath -> IO (Maybe UTCTime)
    getModTime mfile = getModificationTime `traverse` mfile

First, I would like to know whether or not this is a good coding style.

Second, if this is acceptable, why don't we define an operator? For
instance,

   (<:>) :: (Traversable t, Applicative f) => (a -> f b) -> t a -> f (t b)
   (<:>) = traverse

    getModTime :: Maybe FilePath -> IO (Maybe UTCTime)
    getModTime mfile = getModificationTime <:> mfile

Is there such an operator already?

Regards,

--Kazu


More information about the Haskell-Cafe mailing list