# Map vs. mapA

**URL:** <https://forum.canton.network/t/map-vs-mapa/2024>\
**Category:** App Development\
**Created:** [February 2, 2021, 10:17am UTC](https://forum.canton.network/t/map-vs-mapa/2024 "2021-02-02T10:17:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mlyczak](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/mlyczak/32/1260_2.png) [@mlyczak](https://forum.canton.network/u/mlyczak)\
**Post date:** [February 2, 2021, 10:17am UTC](https://forum.canton.network/t/map-vs-mapa/2024/1 "2021-02-02T10:17:37Z")

</div>

Hi, This is a beginner’s question.

Could you please give examples for when one should use ‘map’ and when ‘mapA’ ?

Thank you!

---

<div class="post-metadata">

**Author:** ![bernhard](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/bernhard/32/356_2.png) [@bernhard](https://forum.canton.network/u/bernhard)\
**Post date:** [February 2, 2021, 12:18pm UTC](https://forum.canton.network/t/map-vs-mapa/2024/2 "2021-02-02T12:18:37Z")

</div>

Have a look at the [docs section on looping](https://docs.daml.com/daml/intro/9_Functional101.html#looping) and in particular [the subsection on loops in with actions](https://docs.daml.com/daml/intro/9_Functional101.html#folds-and-maps-in-action-contexts).

In short: the function that you are mapping over a list has to be “pure” in the case of `map` meaning it can’t have side-effects like reading or writing the ledger. The `A` in `mapA` means the function can perform actions - ie can have side-effects like reading from the ledger.

So if I had a list of integers and wanted to add one to each, that’s pure:

```haskell
let numbersPlusOne = map (+ 1) [1,2,3]

```

If I had a list of contract IDs `cids` and wanted to fetch their contract arguments, that’s requires performing an action - reading from the ledger:

```haskell
contract_args <- mapA fetch cids

```

---

<div class="post-metadata">

**Author:** ![mlyczak](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/mlyczak/32/1260_2.png) [@mlyczak](https://forum.canton.network/u/mlyczak)\
**Post date:** [February 2, 2021, 3:34pm UTC](https://forum.canton.network/t/map-vs-mapa/2024/3 "2021-02-02T15:34:17Z")

</div>

@bernhard Thank you. This was very clear and helpful.

---

<div class="post-metadata">

**Author:** ![SamirTalwar](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/samirtalwar/32/374_2.png) [@SamirTalwar](https://forum.canton.network/u/SamirTalwar)\
**Post date:** [February 3, 2021, 8:34am UTC](https://forum.canton.network/t/map-vs-mapa/2024/4 "2021-02-03T08:34:40Z")

</div>

Just for your reference: `mapA` isn’t special. You can implement it in terms of `map` and [`Prelude.sequence`](https://docs.daml.com/daml/stdlib/Prelude.html#function-da-internal-prelude-sequence-49837):

```haskell
mapA : Applicative m => (a -> m b) -> [a] -> m [b]
mapA f = sequence . map f
-- or:
mapA f xs = sequence (map f xs)

```

`sequence` will take a list of some values in an `Applicative` context `m`, and turn them into a list in that context. As `Action` is a subclass of `Applicative`, this works for actions too.

`sequence` can be implemented something like this (untested code):

```haskell
sequence : Applicative m => [m a] -> m [a]
sequence [] = pure []
sequence (x : xs) = (::) <$> x <*> sequence xs

```

This uses the `<$>` and `<*>` operators to run operations inside the `Applicative` context. These operators are defined differently for different contexts; [you can read the definitions for lists](https://github.com/digital-asset/daml/blob/5b3663a500e6840109e0e3e21cd6dea3c546573d/compiler/damlc/daml-stdlib-src/DA/Internal/Prelude.daml#L123) if you like.

The Prelude actually [implements it differently](https://github.com/digital-asset/daml/blob/5b3663a500e6840109e0e3e21cd6dea3c546573d/compiler/damlc/daml-stdlib-src/DA/Internal/Prelude.daml#L381), but it’s equivalent, and it’s all Daml code that you could have written yourself if you wanted. 😃
