# Dict: getindex and setindex in one function call

**URL:** <https://discourse.julialang.org/t/dict-getindex-and-setindex-in-one-function-call/6814>\
**Category:** Internals & Design\
**Tags:** proposal, dictionary\
**Created:** [October 31, 2017, 10:12pm UTC](https://discourse.julialang.org/t/dict-getindex-and-setindex-in-one-function-call/6814 "2017-10-31T22:12:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [October 31, 2017, 10:12pm UTC](https://discourse.julialang.org/t/dict-getindex-and-setindex-in-one-function-call/6814/1 "2017-10-31T22:12:14Z")

</div>

There is a common pattern for working with Dict which is to look up the value and modify it in some way. DataStructures.jl’s Accumulator uses the value as an accumulator to keep sum, StatsBase.countmap uses it to do a frequency count and there are implementation in FreqTable.jl and SplitApplyCombine.jl

The slow way to do this is

```julia
#let dict be a Dict{T,S}
szero = zero(S)
dict[key] = some_function(get(dict, key, szero))

```

In here the lookup happens twice one for `dict[key]` and one for `get(dict, key, szero)`. This method is used in DataStructures.jl accumulator, another method is used by `groupreduce` in SplitApplyCombine which uses Base.ht\_keyindex2 to avoid two lookups and the speed difference is huge in favor of the latter.

Given the commonality of this pattern, I think it’s worthwhile to have a function called `getindexmodify` that can get an index and apply a function to its value to update it in one lookup.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [November 1, 2017, 6:42am UTC](https://discourse.julialang.org/t/dict-getindex-and-setindex-in-one-function-call/6814/2 "2017-11-01T06:42:58Z")

</div>

See [https://github.com/JuliaLang/julia/issues/15630](https://github.com/JuliaLang/julia/issues/15630) and specifically the suggestion in [https://github.com/JuliaLang/julia/issues/15630#issuecomment-201855260](https://github.com/JuliaLang/julia/issues/15630#issuecomment-201855260)

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [November 2, 2017, 6:50am UTC](https://discourse.julialang.org/t/dict-getindex-and-setindex-in-one-function-call/6814/3 "2017-11-02T06:50:54Z")

</div>

There’s also the idea of tokens, I’m kind of wondering if `Associative` should have an interface that supports:

- Getting a token from a key
- Getting the key from a token (probably fast?)
- Getting and setting the value for a token (fast)

And if you think about it, linear indexing of arrays is quite a bit like a token system…

---

<div class="post-metadata">

**Author:** ![kevin.squire](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevin.squire/32/62_2.png) [@kevin.squire](https://discourse.julialang.org/u/kevin.squire)\
**Post date:** [November 2, 2017, 5:36pm UTC](https://discourse.julialang.org/t/dict-getindex-and-setindex-in-one-function-call/6814/4 "2017-11-02T17:36:54Z")

</div>

Note that `SortedDicts` and friends in DataStructures.jl use this notion of  
tokens.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [November 2, 2017, 6:10pm UTC](https://discourse.julialang.org/t/dict-getindex-and-setindex-in-one-function-call/6814/5 "2017-11-02T18:10:57Z")

</div>

Unfortunately in a way that conflicts with the standard API for Associative, so it would be nice if we had an official API for this so that those could be brought into agreement finally. Something like `keytoken` and then index `gettoken` and `settoken!` might work. Worth opening an issue!

---

<div class="post-metadata">

**Author:** ![kevin.squire](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevin.squire/32/62_2.png) [@kevin.squire](https://discourse.julialang.org/u/kevin.squire)\
**Post date:** [November 2, 2017, 11:19pm UTC](https://discourse.julialang.org/t/dict-getindex-and-setindex-in-one-function-call/6814/6 "2017-11-02T23:19:56Z")

</div>

Issue opened!  
[https://github.com/JuliaLang/julia/issues/24454](https://github.com/JuliaLang/julia/issues/24454)

Cheers, Kevin
