[Haskell-cafe] usage of traversal
Michael Snoyman
michael at snoyman.com
Tue Sep 23 07:14:23 UTC 2014
Forgot to CC the list:
I'd be -1 on an operator, I think having a named function for this is a
good thing for readability of code.
As far as good style: I personally think it is. In classy-prelude, I
actually export the Foldable-based `mapM` by default, and will regularly
use that (or forM) for this kind of code.
On Tue, Sep 23, 2014 at 8:27 AM, Kazu Yamamoto <kazu at iij.ad.jp> wrote:
> 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
> _______________________________________________
> 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/20140923/0b5633c5/attachment.html>
More information about the Haskell-Cafe
mailing list