Hacker News new | ask | show | jobs
by bburky 4357 days ago
More specifically, the the previous hash-table-like method is just assigning constants as values for a function.

    In[1]:= mymap["abc"]=1;
            mymap[2]=2;
            mpmap[hello]=3;
            DownValues[mymap]
    Out[4]= {HoldPattern[mymap[2]]:>2,HoldPattern[mymap[abc]]:>1}
Defining a function is really just a pattern is matched based on arguments, and is replaced with an expression with the arguments substituted into it. And you can set a constant key argument to a value of a constant value — creating the odd built in hash-table-alike which has been around forever.

That said, Association[] looks nice. Presumably it gets a speed improvement by bypassing the pattern matching. And I remember being annoyed by using JSON data in Mathematica structures, this looks nicer. The literal syntax is appreciated too. Actually I'm kind of surprised they didn't choose another weird unicode symbol like 〚, for the literal syntax.

1 comments

Well, unlike adding and changing definitions for symbols in the symbol table, associations are actually immutable. So you can pass them around as values, mutate them, and the old versions are still available unchanged. That's just not true of using downvalues of symbols.

Also, using definitions on symbols to store key-values is incredibly limiting, because you can't do reverse lookups without getting into very low level (and slow) hackery via DownValues. Or list the keys or values. Or even know how many keys you have.

Associations really will replace basically all uses of symbol downvalues for storing key-value pairs. And Associations have really nice general-purpose functions like GroupBy, Counts, Merge, and so on (mentioned below).

Associations even support function application syntax, so you should be able to drop them in into existing code that expects symbol downvalues as you're using them above.

Associations are the single most significant language-level improvement we've had in many versions. And we've integrated them really nicely across the system -- they're not just some special-purpose data structure.

Yeah, using definitions on symbols to store key-values is indeed incredibly limiting.

Association seems very similar to List: immutable, really good Part syntax for accessing things in deeper levels, etc. I've started to read some of the docs on it too, it looks very nice.