How do I make double mapping in Daml?

Hello devs,

I am making 2 dimensional hashmap array, and I think I need to make map of map using DA.map.

How do I do this?

Hi @hskang9 welcome to the Daml forums!

Exactly as you suggest, you use nested maps:

import DA.Map as Map

...
  let
    x : Map.Map Text (Map.Map Text Int)
    x = Map.fromList [("foo", Map.fromList [("foofoo" , 1), ("foobar", 2)]), ("bar", Map.fromList [("barfoo", 3), ("barbar", 4)])]

    nestedLookup k1 k2 m = do
      inner <- Map.lookup k1 m
      Map.lookup k2 inner 

    -- y == Some 2
    y : Optional Int = nestedLookup "foo" "bar" x
2 Likes

how do you insert data on each layer?

Also, how do I update it? there are none of that example in the official documentation

You can write little helper functions for the nested operations:

let
    x : Map.Map Text (Map.Map Text Int)
    x = Map.fromList [("foo", Map.fromList [("foo" , 1), ("bar", 2)]), ("bar", Map.fromList [("foo", 3), ("bar", 4)])]

    nestedLookup k1 k2 m = do
      inner <- Map.lookup k1 m
      Map.lookup k2 inner 

    nestedSet k1 k2 v m = Map.insert k1 inner m
      where
        org_inner = fromOptional Map.empty (Map.lookup k1 m)
        inner = Map.insert k2 v org_inner

    -- y == Some 2
    y : Optional Int = nestedLookup "foo" "bar" x

    x' = nestedSet "foo" "baz" 5 x
    x'' = nestedSet "baz" "foo" 6 x'

    y' : Optional Int = nestedLookup "foo" "baz" x''
    y'' : Optional Int = nestedLookup "baz" "foo" x''
    y''' : Optional Int = nestedLookup "baz" "bar" x''

  ([y, y', y'', y'''] === [Some 2, Some 5, Some 6, None])
  return ()