[Haskell] LiftA2 over fmap

Edward Kmett ekmett at gmail.com
Tue Jan 30 10:41:32 UTC 2018


> In particular, if fmap
<http://hackage.haskell.org/package/base-4.10.1.0/docs/Data-Functor.html#v:fmap>
 is an expensive operation, it is likely better to use liftA2
<http://hackage.haskell.org/package/base-4.10.1.0/docs/Control-Applicative.html#v:liftA2>
 than to (fmap
<http://hackage.haskell.org/package/base-4.10.1.0/docs/Data-Functor.html#v:fmap>
 over the structure *and then use <*>
<http://hackage.haskell.org/package/base-4.10.1.0/docs/Control-Applicative.html#v:-60--42--62->)*
. (emphasis and parens added).

Consider any data constructor that has a strict spine and look at

f <$> m <*> n

This has to walk `m` twice. Once for the fmap on the first argument, and
once for the <*>, but the liftA2 version can walk `m` once fusing things
together.

-Edward

On Tue, Jan 30, 2018 at 4:42 AM, Damien BIHEL <biheldamien at gmail.com> wrote:

> In the documentation here
> <http://hackage.haskell.org/package/base-4.10.1.0/docs/Control-Applicative.html#v:liftA2>
> it's written that for some functors liftA2 has better performance than
> fmap. Does anyone have any examples ?
>
> Regards,
>
> --
> Damien Bihel
> R&D (Research & Development) Engineer at ELRA (European Language Resources
> Association)
> E-Mail: biheldamien at gmail.com
>
> _______________________________________________
> Haskell mailing list
> Haskell at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell/attachments/20180130/e3b2285d/attachment.html>


More information about the Haskell mailing list