# How to achive foldl operation for Map data

**URL:** <https://forum.canton.network/t/how-to-achive-foldl-operation-for-map-data/1557>\
**Category:** App Development\
**Created:** [October 29, 2020, 6:34pm UTC](https://forum.canton.network/t/how-to-achive-foldl-operation-for-map-data/1557 "2020-10-29T18:34:16Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sneha](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/sneha/32/543_2.png) [@Sneha](https://forum.canton.network/u/Sneha)\
**Post date:** [October 29, 2020, 6:34pm UTC](https://forum.canton.network/t/how-to-achive-foldl-operation-for-map-data/1557/1 "2020-10-29T18:34:16Z")

</div>

Could someone guide me to the solution of below case.

Example: There is a mapList on the template which of type Map Text [List]

Now i want to append data to the list after satisying some rules, for this i need a way to concatinate original map i.e., mapList with new data everytime

and the return value should be mapList with new List of data appended

```haskell

Map on template --> mapList : Map Text [VersionList]
VersionList is a Record --> VersionList = [VersionList with value = 1000]

forA VersionList(\version -> do
        let value = version.value
        if (value == "1000" ) then do
            let concateValues = VersionList ++ [2000] 
             mapList = M.insert( "amount" concatValue)
            return ()                                                                                                    
       else do
            return () 

        ) 

```

Output : After for loop expecting mapList to be as (amount, [1000,2000])

---

<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:** [October 29, 2020, 8:00pm UTC](https://forum.canton.network/t/how-to-achive-foldl-operation-for-map-data/1557/2 "2020-10-29T20:00:36Z")

</div>

Hi @Sneha, what is the type of `versionList`? Your example doesn’t seem to reference `mapList` so I’m slightly unclear on where it fits in.

---

<div class="post-metadata">

**Author:** ![Sneha](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/sneha/32/543_2.png) [@Sneha](https://forum.canton.network/u/Sneha)\
**Post date:** [October 30, 2020, 4:29am UTC](https://forum.canton.network/t/how-to-achive-foldl-operation-for-map-data/1557/3 "2020-10-30T04:29:49Z")

</div>

Hi @cocreature, I have edited the example. Hope this would be clear now.

---

<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:** [October 30, 2020, 7:54am UTC](https://forum.canton.network/t/how-to-achive-foldl-operation-for-map-data/1557/4 "2020-10-30T07:54:43Z")

</div>

Here is one solution that satisfies the input you provided. I am not quite sure I understood your requirements correctly so if you were looking for something else, let us know:

```haskell
module Main where

import Daml.Script
import DA.Assert
import DA.Next.Map (Map)
import qualified DA.Next.Map as Map

data Version = Version with
    value : Text
  deriving (Eq, Show)

processMap : Map Text [Version] -> Map Text [Version]
processMap mapList = 
  Map.fromList 
    (map (\(mapKey, versionList) -> (mapKey, processList versionList))
         (Map.toList mapList))

processList : [Version] -> [Version]
processList versionList =
  concatMap f versionList
  where
    f version
      | version.value == "1000" = [version, Version "2000"]
      | otherwise = [version]

test = script do
  -- Original example
  processMap (Map.fromList [("amount", [Version "1000"])])
          === Map.fromList [("amount", [Version "1000", Version "2000"])]

  -- 2000 is inserted directly after 1000 not at the very end
  processMap (Map.fromList [("amount", [Version "1000", Version "3000"])])
          === Map.fromList [("amount", [Version "1000", Version "2000", Version "3000"])]

  -- This applies the same to all keys not just amount
  processMap (Map.fromList [("notamount", [Version "1000", Version "3000"])])
          === Map.fromList [("notamount", [Version "1000", Version "2000", Version "3000"])]

```

---

<div class="post-metadata">

**Author:** ![Sneha](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/sneha/32/543_2.png) [@Sneha](https://forum.canton.network/u/Sneha)\
**Post date:** [November 1, 2020, 8:11am UTC](https://forum.canton.network/t/how-to-achive-foldl-operation-for-map-data/1557/5 "2020-11-01T08:11:11Z")

</div>

Okay @cocreature Thank you for this approach.

May be I have complicated the question. Below is the simple example of the question (Additive operation on Map). Could you please help me.

Lets say there is a global map MapA, Now want to concat MapA with (MapB or MapC or Map D and so on) and add the result to MapA

MapA = MapA ++ MapB

---

<div class="post-metadata">

**Author:** ![Luciano](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/luciano/32/319_2.png) [@Luciano](https://forum.canton.network/u/Luciano)\
**Post date:** [November 2, 2020, 7:23am UTC](https://forum.canton.network/t/how-to-achive-foldl-operation-for-map-data/1557/6 "2020-11-02T07:23:26Z")

</div>

To achieve the above you can use the [merge](https://docs.daml.com/daml/stdlib/DA-Next-Map.html#function-da-next-map-merge-2656) function in `DA.Next.Map` module.

---

<div class="post-metadata">

**Author:** ![Sneha](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/sneha/32/543_2.png) [@Sneha](https://forum.canton.network/u/Sneha)\
**Post date:** [November 2, 2020, 10:08am UTC](https://forum.canton.network/t/how-to-achive-foldl-operation-for-map-data/1557/7 "2020-11-02T10:08:46Z")

</div>

Thank you @Luciano. I am trying to use the merge key but sorry unable to understand the structure.

[merge](https://docs.daml.com/daml/stdlib/DA-Next-Map.html#function-da-next-map-merge-2656)

: [MapKey](https://docs.daml.com/daml/stdlib/DA-Next-Map.html#class-da-next-map-mapkey-12781) k =\> (k -\> a -\> [Optional](https://docs.daml.com/daml/stdlib/Prelude.html#type-da-internal-prelude-optional-79838) c) -\> (k -\> b -\> [Optional](https://docs.daml.com/daml/stdlib/Prelude.html#type-da-internal-prelude-optional-79838) c) -\> (k -\> a -\> b -\> [Optional](https://docs.daml.com/daml/stdlib/Prelude.html#type-da-internal-prelude-optional-79838) c) -\> [Map](https://docs.daml.com/daml/stdlib/DA-Next-Map.html#type-da-next-map-map-36848) k a -\> [Map](https://docs.daml.com/daml/stdlib/DA-Next-Map.html#type-da-next-map-map-36848) k b -\> [Map](https://docs.daml.com/daml/stdlib/DA-Next-Map.html#type-da-next-map-map-36848) k c

How to read and understand the structure.  
Is there any example for merge key where I can refer to?

---

<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:** [November 2, 2020, 10:38am UTC](https://forum.canton.network/t/how-to-achive-foldl-operation-for-map-data/1557/8 "2020-11-02T10:38:39Z")

</div>

`merge` is a generalized version of [`union`](https://docs.daml.com/daml/stdlib/DA-Next-Map.html#function-da-next-map-union-6297). Whereas `union` always prefers elements of the second map over elements from the first if a key occurs in both, `merge` allows you to provide 3 functions to customize how the maps should be merged:

1. The first function is used if the key only occurs in the first map. Returning `None` means that such elements will be discarded.
2. The second function is used if the key only occurs in the second map. Same behavior for `None`.
3. The third function is used if the key occurs in both maps. You can define how the values should be combined (e.g., if you have numbers, maybe adding them is the right thing to do).

---

<div class="post-metadata">

**Author:** ![Sneha](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/sneha/32/543_2.png) [@Sneha](https://forum.canton.network/u/Sneha)\
**Post date:** [November 2, 2020, 10:45am UTC](https://forum.canton.network/t/how-to-achive-foldl-operation-for-map-data/1557/9 "2020-11-02T10:45:08Z")

</div>

Thank you @cocreature for the detail explaination.

---

<div class="post-metadata">

**Author:** ![Gary\_Verhaegen](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/gary_verhaegen/32/81_2.png) [@Gary\_Verhaegen](https://forum.canton.network/u/Gary_Verhaegen)\
**Post date:** [November 2, 2020, 2:25pm UTC](https://forum.canton.network/t/how-to-achive-foldl-operation-for-map-data/1557/10 "2020-11-02T14:25:19Z")

</div>

Here’s a small code snippet that may clarify how to use some of the `DA.Next.Map` functions:

```haskell
module Main where

import Daml.Script
import qualified DA.Next.Map as Map

setup : Script ()
setup = script do
  let map1 = Map.fromList [(1, "a"), (2, "b")]
  let map2 = Map.fromList [(2, "c"), (3, "d")]
  assert (
    Map.insert 5 "hello!" map1
    ==
    Map.fromList [(1, "a"), (2, "b"), (5, "hello!")]
    )
  assert (
    Map.insert 1 "changed!" map1
    ==
    Map.fromList [(1, "changed!"), (2, "b")]
    )
  assert (
    Map.delete 2 map1
    ==
    Map.fromList [(1, "a")]
    )
  assert (
    Map.union map1 map2
    ==
    Map.fromList [(1, "a"), (2, "b"), (3, "d")]
    )
  assert (
    Map.merge (\k v -> Some $ "from 1: " <> v)
              (\k v -> Some $ "from 2: " <> v)
              (\k v1 v2 -> Some $ "from both: " <> v1 <> ", " <> v2)
              map1
              map2
    ==
    Map.fromList [(1, "from 1: a"),
                  (2, "from both: b, c"),
                  (3, "from 2: d")]
    )

```
