mapA forA Difference

Hello, good evening. I would like to ask the difference between mapA and forA within Prelude, since they’re most likely the same. Thanks!

1 Like

The only difference is the order of arguments. forA xs fn == mapA fn xs. It’s mostly a convenience to be able to have the shorter expression first:

forA xs (\x -> do
  ...
  )

or

mapA myUpdate (some very long expression for a list)
3 Likes

Oh I see, really helpful insight! Thanks! @bernhard!

1 Like

Because of the order of parameters, forA also lets you put the long argument behind a $ so you can avoid nesting parentheses. I find this quite useful when I have a bunch of stuff going on:

forA xss $ \xs ->
  forA xs $ \x -> do
    ...
3 Likes

A slight variant of @SamirTalwar’s suggestion is to use it infix (any function you are passing two arguments to can be called infix):

xss `forA` \xs ->
   ...

This is what I learned as the use case for forA, and I also used this recently in DAML’s own tests.

Situations like these arise in DAML, where a very, very simple utility function can have surprising benefits for readability. This would hold whether forA was in the standard library or whether you wrote it yourself, so don’t dismiss the potential benefits of a function you might write just because its implementation is trivial. (Conversely, not every trivial function is worth writing, either. It’s a matter of your own judgement.)

3 Likes