Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: using tuples as dictionary keys

I have a situation where I want to create a mapping from a tuple to an integer. In python, I would simply use a tuple (a,b) as the key to a dictionary,

Does Javascript have tuples? I found that (a,b) in javascript as an expression just returns b (the last item). Apparently this is inherited from C.

So, as a workaround, I thought I can use arrays instead,

my_map[[a,b]] = c 

I tried it at the Firebug console and it seemed to work. Is that a good way to do it?

Another alternative I thought of is to create a string out of the tuples

my_map[""+a+":"+b] = c 

So the question is: is there any problem with any of these methods? Is there a better way?

EDIT:

Small clarification: in my case, a,b,c are all integers

like image 730
hasen Avatar asked Sep 15 '09 04:09

hasen


People also ask

Can I use tuple as dictionary key?

A tuple containing a list cannot be used as a key in a dictionary. Answer: True. A list is mutable. Therefore, a tuple containing a list cannot be used as a key in a dictionary.

Can we pass list or tuple as a key in dictionary?

We can use integer, string, tuples as dictionary keys but cannot use list as a key of it .

Can dictionary value be a tuple?

Python Dictionaries Each item of a dictionary has a key:value pair and are written within curly brackets separated by commas. The values can repeat, but the keys must be unique and must be of immutable type(string, number or tuple).

Can you use tuples in JavaScript?

In JavaScript, tuples are created using arrays. In Flow you can create tuples using the [type, type, type] syntax. When you are getting a value from a tuple at a specific index, it will return the type at that index.


2 Answers

EcmaScript doesn't distinguish between indexing a property by name or by [], eg.

a.name 

is literally equivalent to

a["name"] 

The only difference is that numbers, etc are not valid syntax in a named property access

a.1 a.true 

and so on are all invalid syntax.

Alas the reason all of these indexing mechanisms are the same is because in EcmaScript all property names are strings. eg.

a[1] 

is effectively interpreted as

a[String(1)] 

Which means in your example you do:

my_map[[a,b]] = c 

Which becomes

my_map[String([a,b])] = c 

Which is essentially the same as what your second example is doing (depending on implementation it may be faster however).

If you want true value-associative lookups you will need to implement it yourself on top of the js language, and you'll lose the nice [] style access :-(

like image 105
olliej Avatar answered Sep 17 '22 13:09

olliej


You could use my jshashtable and then use any object as a key, though assuming your tuples are arrays of integers I think your best bet is one you've mentioned yourself: use the join() method of Array to create property names of a regular object. You could wrap this very simply:

function TupleDictionary() {  this.dict = {}; }  TupleDictionary.prototype = {  tupleToString: function(tuple) {   return tuple.join(",");  },   put: function(tuple, val) {   this.dict[ this.tupleToString(tuple) ] = val;  },   get: function(tuple) {   return this.dict[ this.tupleToString(tuple) ];  } };  var dict = new TupleDictionary(); dict.put( [1,2], "banana" ); alert( dict.get( [1,2] ) ); 
like image 41
Tim Down Avatar answered Sep 20 '22 13:09

Tim Down