# Why is there no \`for\` in daml?

**URL:** <https://forum.canton.network/t/why-is-there-no-for-in-daml/1268>\
**Category:** App Development\
**Created:** [September 22, 2020, 11:48am UTC](https://forum.canton.network/t/why-is-there-no-for-in-daml/1268 "2020-09-22T11:48:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![meet](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/meet/32/198_2.png) [@meet](https://forum.canton.network/u/meet)\
**Post date:** [September 22, 2020, 11:48am UTC](https://forum.canton.network/t/why-is-there-no-for-in-daml/1268/1 "2020-09-22T11:48:50Z")

</div>

DAML seems to have a `forA` corresponding to a `mapA`. Just for the sake of completeness, why is there no `for` corresponding to a `map`?

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [September 22, 2020, 12:31pm UTC](https://forum.canton.network/t/why-is-there-no-for-in-daml/1268/2 "2020-09-22T12:31:46Z")

</div>

This is primarily due to being based on Haskell. Haskell has a [`for`](https://hackage.haskell.org/package/base-4.14.0.0/docs/Data-Traversable.html#v:for) function but it is actually equivalent to `forA` in DAML. So to avoid any potential confusion, we did not add a `for` to DAML. That said, I don’t think it would be that unreasonable to add it.

---

<div class="post-metadata">

**Author:** ![Andrae](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/andrae/32/395_2.png) [@Andrae](https://forum.canton.network/u/Andrae)\
**Post date:** [September 23, 2020, 3:19am UTC](https://forum.canton.network/t/why-is-there-no-for-in-daml/1268/3 "2020-09-23T03:19:54Z")

</div>

Be aware that `map` is specialised to list, and as such we generally prefer to use `fmap` which is the same function generalised over all `Functor`’s from which `map` is derived. This contrasts with `mapA` which is already generalised over `Applicative` and `Traversable`\*.

Moreover when we use `fmap` (over `[]`, `Optional`, `NonEmpty`, `Update`, or some other `Functor`), we generally use the operator version `<$>`. So instead of writing `fmap someFunction someList` we write `somefunction <$> someFunctor`. This is generally easier to read, especially as operator precedence often allows us to eliminate multiple parentheses by using an operator like this\*\*.

There are occasions where the code is easier to read if we swap the function and the list arguments — fortunately, while there is no `for` function in the standard library, there is a `for` operator `<&>`. So you can always write `someFunctor <&> someFunction`.

In short, you will almost always find your code easier to read and write if you use `<$>` in preference to `fmap` _and_ occasionally `<&>` where you want the arguments flipped.

- In fact in much the same way the existence of a lawful `fmap` _is_ what defines a `Functor`, the existence of a lawful `mapA` _is_ what defines a `Traversable`.

\*\* The two common exceptions to this are:

1. Where you want to partially apply `fmap` to define a function. ie.

> ```haskell
> f : Update Text -> Update Int
> f = fmap length
> 
> ```

1. Where you need to fmap over nested functors, ie. applying `f : (a -> b)` to `arg : [Optional a]` to get `[Optional b]`. So we do find a few places in our code where we write `fmap f <$> arg`.

A quick grep indicates 1805 uses of `<$>`, 241 uses of `<&>`, 267 uses of `fmap` (the vast majority of which fall into case 1 above).

PS. As an aside, as you move from introductory to intermediate DAML, it is highly recommended that you take the time to explore and understand the core typeclasses `Functor`, `Applicative`, `Action`, and `Traversable`, their functions, operators, and hierarchy, as advanced DAML techniques often assume a familiarity with these concepts, and they do allow you to simplify otherwise quite complicated DAML code.
