question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add a map type to LF

See original GitHub issue

There have been several discussion to add a new type Map K V to DAML-LF in order to give us maps with arbitrary keys in DAML and a sensible representation in app languages. The issues that arise are:

  • The engine needs to be deterministic. Which means the representation of the map needs to be as well. @remyhaemmerle-da recommends defining an internal ordering on Value and using a tree map.
  • gRPC doesn’t have arbitrary maps so we need to represent maps as a [(K, V)] on the wire. Again, to get determinism, this needs to be ordered so we have complexity n log(n) somewhere.
  • JS doesn’t have a good map type for us to use in the bindings. @stefanobaghino-da recommends implementing our own.

If I got this right, the work would be

  1. Define/implement ordering on Value
  2. Implement LF type TreeMap K V and serialisation to and from [(K, V)] (with the write path doing the ordering and checking for duplicate keys)
  3. Implement JS Map type
  4. Translate [(K, V)] to and from idiomatic maps in Java and JS
  5. Write a DA.TreeMap DAML lib

And the main tradeoff is that our maps would be O(log(n)), but I find that perfectly acceptable. If anyone needs an O(1), they can always write a MapKey instance.

Anything I missed? Does this sound like a good way to go? If so, how much effort would this be?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
remyhaemmerle-dacommented, Aug 6, 2019

Proposal

We implement Insertion preserving map (called 'Map') where keys are a arbitrary serializable values.

We provides the following builtins (with expected builtins):

  • MAP_EMPTY : ∀ k v, 'Map' k v

    Returns the empty map. O(1)

  • MAP_INSERT : ∀ k v. k → v → 'Map' k v → 'Map' k v

    Inserts a new key and value in the map. If the key is already present in the map, the associated value is replaced with the supplied value.If the key is present do not change the key insertion order. O(log(n))

  • MAP_LOOKUP : ∀ k v. k → 'Map' k v → 'Optional' v

    Lookups the value at a key in the map. O(log(n))

  • MAP_DELETE : ∀ k v. k → 'Map' k v → 'Map' k v

    Deletes a key and its value from the map. When the key is not a member of the map, the original map is returned. o(log(n))

  • MAP_LIST : ∀ k v. 'Map' k v → 'List' ⟨ key: k, value: v ⟩

    Converts to a list of key/value pairs. The output list is sorted in the ordered the key have been inserted. O(n * log n)

  • MAP_FROM_LIST: ∀ k v. 'List' ⟨ key: k, value: v ⟩ → 'Map' k v

    Converts a list of key/value pairs into a map. The insertion order is from left o right. Throws an exception if a key is duplicated. O(n * log n)

  • MAP_SIZE : ∀ k v. 'Map' k v → 'Int64'

    Return the number of elements in the map. O(1)

In the ledger and in the ledger API, the map are represented as lists of pairs order as the insertion order.

0reactions
cocreaturecommented, Jun 1, 2021

added in lf 1.11

Read more comments on GitHub >

github_iconTop Results From Across the Web

add an option for ignore layout · Issue #274 · gokcehan/lf
hi i use two layout on my system. lf not detect my shortcut in second ... map y # unmaps the current default...
Read more >
Total and Partial Maps - Software Foundations
We build up to partial maps in two steps. First, we define a type of total maps that return a default value when...
Read more >
lf - command - Go Packages
Load modified files and directories. This command is automatically called when required. reload (default '<c-r>'). Flush the cache and reload all files and ......
Read more >
Map Functions and Operators — Presto 0.278.1 Documentation
Returns the union of all the given maps. If a key is found in multiple given maps, that key's value in the resulting...
Read more >
Understanding the Map Data Type in Dart - Section.io
Inserting new key-value pairs in a map can be implemented in two ways: Adding values to a Map Literal. The code below implements...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found