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
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])
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:
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"])]
merge is a generalized version of union. 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:
The first function is used if the key only occurs in the first map. Returning None means that such elements will be discarded.
The second function is used if the key only occurs in the second map. Same behavior for None.
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).