25

Clarification

Although I may be missing his point I currently feel that Leonid's comments below are misleading. I am not looking for anything that is not already a part of Association functionality other than a way to define what is returned for a missing key on a per-association basis. I thought that the Block example below made this clear but perhaps not.

I am seeking a way to do something like this:

asc = <|"a" -> 1, "b" -> 2, _ -> 0|>;

asc["x"]

(* desired output: 0 *)

(* actual output: Missing["KeyAbsent", "x"] *)

Critically I am not looking for general pattern matching of Key names however; I only want a way to define one default value for missing keys.


Original ramblings

It can be very useful to define background or default value for an object that can be incremented or otherwise modified. A simple (and for me, common) use is a counter:

count[_] = 0;

++count[#] & /@ {"a", "a", "b", "a", "b", "a"}
{1, 2, 1, 3, 2, 4}

Since there are advantages to Association such as ease of copying and direct manipulation of keys and values I would like to port this method to new function. However I cannot think of a clean, practical way to define a default. (I consider directly overloading System functions such as Increment undesirable.)

I note that it is possible to increment a missing value and then clean it up afterward:

asc = <||>;

++asc[#] & /@ {"a", "a", "b", "a", "b", "a"} /. _Missing -> 0
{1, 2, 1, 3, 2, 4}

This isn't really the same as setting a default value however and it limits the way this method can be used. Somewhat better I think is to temporarily redefine Missing. This at least gives values that are up-to-date while the operation is performed.

asc = <||>;

Block[{Missing},
  Missing["KeyAbsent", _] = 0;
  ++asc[#] & /@ {"a", "a", "b", "a", "b", "a"}
]

asc
{1, 2, 1, 3, 2, 4}

<|"a" -> 4, "b" -> 2|>

This could be packaged a bit more nicely but it still feels like a bit of a kluge.

Is there an approach I am failing to consider? Or by some slim chance is there a hidden way to do this?

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • 3
    I think that by nature of associations (immutability, in the first place), mutating them in-place isn't by far as natural as mutating DownValues. And the fact that what you request is problematic in this approach is simply another facet of that: the ability to use defaults has been transferred from association itself to the Lookup function, but then Lookup can not be used to mutate things, bacause it knows nothing about where a given assoc is stored (and doesn't care about it). If you need mutation, why use associations? You can keep using DownValues in such cases. – Leonid Shifrin Jul 06 '15 at 04:05
  • Also the title seems to be a bit misleading currently: what you ask for is not just default, but mutable default - which makes quite a difference. – Leonid Shifrin Jul 06 '15 at 04:07
  • @Leonid (1) It may be as natural but the functionality exists and I see no reason not to try to use it. I realize that ultimately performance issues may or may not limit use but I disagree that this is somehow an inherent limitation if I infer that correctly. (2) Does the existence of special default handling in Lookup prevent other handling? I wouldn't think so. (3) Do all of the benefits of associations cease to exist for some reason? Easy copying and manipulation of values, nested associations, etc. still apply do they not? – Mr.Wizard Jul 06 '15 at 05:45
  • @Leonid Regarding the title I tried to see it from that perspective but I cannot. The default value is not being changed; it is only being used in subsequent computations. Maybe tomorrow I can bring some fresh eyes to it but at the moment it feels like you are critiquing a straw man. – Mr.Wizard Jul 06 '15 at 06:05
  • I use Lookup precisely because there is no default. – rcollyer Jul 06 '15 at 16:09
  • @rcollyer That works in many cases but it prevents the (IMHO) natural use of operators such as Increment or AddTo. I see no reason that a default in the Association and a specified default in Lookup cannot coexist. Am I mistaken? – Mr.Wizard Jul 06 '15 at 16:15
  • Allowing Lookup to supply a default when one existed within an Association would require some tighter knitting between the two, otherwise how would Lookup know when to supply its default? – rcollyer Jul 06 '15 at 16:28
  • @rcollyer You seem to be implying that Lookup doesn't already have this knitting but I believe it does; it is not relying on Missing to detect a missing value. I believe it finds it directly. I mean that Lookup is not doing something like /. _Missing -> 0 in my second example. What therefore is the complication? – Mr.Wizard Jul 06 '15 at 16:39
  • @Mr.Wizard Re: 1 - I largely answered this here, but to summarize, the main advantage of associations with respect to DownValues or System`Utilities`HashTable is that associations are immutable, but cheap to copy and modify. In the linked post I described why immutability is valuable (statelessness, garbage collection, etc). The ability to mutate assocs in place does exist (like with lists), but its support in the language is limited to very simple things like part assignments. Since there are no true references in Mathematica, ... – Leonid Shifrin Jul 06 '15 at 18:00
  • @Mr.Wizard ... most other functions working with assocs will not have the information about where a given assoc is stored (in which variable or variables), and will operate on assoc directly, producing new assoc or whatever. So, every time when you need to add mutability, you have to do some special tricks, like you did with Missing. There is simply no way for Lookup, for example, to do some in-place modifications related to the default value. So, my point has been that what you ask for would go into directions with very weak support from the core language, and would need more work then. – Leonid Shifrin Jul 06 '15 at 18:03
  • @Mr.Wizard Re: (2): No, it doesn't, but Lookup is the only built-in function providing the default mechanism. One can surely implement one's own handling, but that would be more work, again (3)."Do all of the benefits of associations cease to exist for some reason? Easy copying and manipulation of values, nested associations, etc. still apply do they not" - yes, they do not apply any more as long as you want your assoc to be mutable and carry a state. That's precisely the point. You have to then define a new data type that would carry that state together with the assoc, and integrate ... – Leonid Shifrin Jul 06 '15 at 18:06
  • @Mr.Wizard ... that new data type into the language. Quite possible, but a lot of work. I might post some version of such solution later, if I have the time. This brings us to an important topic of the lack of subtyping mechanism for core data structures in Mathematica - which would allow one to e.g. subtype an assoc and add some state to it (like one can do in e.g. Python). – Leonid Shifrin Jul 06 '15 at 18:06
  • @Mr.Wizard Re - default value: indeed, you are right, it doesn't change. So, I take that critique back. In fact, one constructive suggestion that comes out of this is to be able to specify a default value, or generally, default function, when association is constructed. In fact, at some point during development such thing was discussed internally, but then for some reason it has been discarded. When I get a chance, I will ask, what was the rationale behind not having it. – Leonid Shifrin Jul 06 '15 at 22:05
  • @Mr.Wizard To add more to my argument, a principal difference between associations and (mutable) hash tables is that assocs can be used with functions like Map etc for data transformations, while hash tables can not, because they are stateful. Now, while we can define e.g. the semantics of Map acting on an assoc with a default: Map[f, AssocWithDefault[assoc, default]] -> AssocWithDefault[Map[f,assoc], f[default]], what would we do with say MapIndexed (which is defined on assocs)? There are also other operators for which the semantics of an assoc with default isn't clear. – Leonid Shifrin Jul 07 '15 at 20:06
  • 1
    Valid question! It would be nice to be able to do something like: asso /: asso[key_] := Lookup[asso, key, "NoAv"]. P.s. I'd delete everything up to "clarification" :) – Kuba Sep 28 '15 at 10:43
  • Not exactly what you ask for but might be interesting in the general context, try this : asc = <||>;Query[++asc[#] & /@ # &]@{"a", "a", "b", "a", "b", "a"}, compared to asc = <||>; Query[++asc[#] & /@ # &, MissingBehavior -> None]@{"a", "a", "b", "a", "b", "a"}. The default setting of MissingBehavior is Automatic. – SquareOne Sep 28 '15 at 13:45

2 Answers2

6

It seems to me that merely defining a global default value enables the kind of application for associations that you are discussing.

asc = <||>; $asc = 0;

(KeyExistsQ[#][asc] || (asc[#] = $asc); ++asc[#]) & /@ 
   {"a", "a", "b", "a", "b", "a"}
{1, 2, 1, 3, 2, 4}
asc
<|"a" -> 4, "b" -> 2|>
bsc = <||>; $bsc = 1;

(KeyExistsQ[#[[1]]][bsc] || (bsc[#[[1]]] = $bsc); bsc[#[[1]]] = bsc[#[[1]]] #[[2]]) & /@ 
   MapThread[Rule, {{"a", "a", "b", "a", "b", "a"}, Range[6]}]
{1, 2, 3, 8, 15, 48}
bsc
<|"a" -> 48, "b" -> 15|>

I am prepared to be instructed on how what I have presented here misses the point.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
  • This is certainly a valid approach. My issue with it is the extra code needed for every operation; ++asc[#] becoming (KeyExistsQ[#][asc] || (asc[#] = $asc); ++asc[#]) is a pretty bad hit to clean coding. It would be possible to define new operators e.g. associationIncrement (possibly with a shorter name) that handle this but one loses commonality in code. I don't know of any satisfying solution but that is why I posted the question. – Mr.Wizard Jul 06 '15 at 15:53
2

Simply:

asc["x"]/._Missing->0
Frank
  • 286
  • 1
  • 6