Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dicts with defaults, etc. #1529

Closed
StefanKarpinski opened this issue Nov 11, 2012 · 9 comments
Closed

Dicts with defaults, etc. #1529

StefanKarpinski opened this issue Nov 11, 2012 · 9 comments
Assignees
Milestone

Comments

@StefanKarpinski
Copy link
Member

This one is straight from Ruby: dicts with default values, provides either by a value (immutable) or a default value generator function. The Ruby style is (h,k)->... so that you can choose to cache the default value or not.

An interesting idea is having type-specific defaults for typed Dicts. Another interesting idea comes from @avibryant's talk at Hacker School: dicts naturally become a monoid if their values form a monoid.

Another generalization of Dict behavior would come from using different hashing functions. Not sure that's really a great idea though.

@ghost ghost assigned StefanKarpinski Nov 11, 2012
@johnmyleswhite
Copy link
Member

Glad you remembered this one. I considered implementing it one day, then wasn't sure whether it should be a new type or just a breaking change in the existing Dicts.

@StefanKarpinski
Copy link
Member Author

Yeah, I'm not sure. Kind of depends on if we can add this to Dicts without a performance penalty or not.

@StefanKarpinski
Copy link
Member Author

So the main reason I've not done this yet is that I've been mulling over (somewhat sluggishly), what the interface should look like. As mentioned above, one way you could do this is like Ruby:

dict = Dict((d,k)->d[k]=0)

However, I was considering doing something like this instead:

plus_dict = Dict(+)
max_dict = Dict(max)
min_dict = Dict(min)

The idea here is that the operator not only provides defaults, but also acts as a combining operation for dict merges. So Dict(+) has +() = 0 as its default value, and if you merge two Dict(+) objects, their values are added. Likewise for Dict(max) the default value should be -Inf, although it seems that max() is not defined that way. I kind of like this idea, but fear it may be too cute.

@avibryant
Copy link
Contributor

Makes sense to me; this is basically like the MapMonoid in algebird.

@StefanKarpinski
Copy link
Member Author

Yup. Very much inspired by your talk at Hacker School.

@toivoh
Copy link
Contributor

toivoh commented Dec 14, 2012

Sounds really interesting. I wouldn't be surprised if most uses of default dicts are motivated by an underlying monoid structure.

But what would the interface actually look like? If I have a d1 and d2 which are Dict(+), I would not be surprised if

d = merge(d1, d2)

would add the items together for keys present in both dicts. But how about

d[key] = value

Would that assign to d[key] (I hope so) or add to it?
And if it indeed assigns, how would I accumulate a value at a given key using the dict's monoid operator?

Also, why stop at monoids? It seems to me that a dict is pretty much a sparse vector.
One could define d1+d2 to do addition like with Dict(+), and d1*d2 to do multiplication like with Dict(*). On the other hand, that would use different default values for different operations, which seems kinda fishy. (Unless you really know that's what you want)

@johnmyleswhite
Copy link
Member

I should chime in and note that:

Dict(+)
d = merge(d1, d2)

is a special case of the Series type in Python's pandas library.

@kmsquire
Copy link
Member

kmsquire commented Feb 4, 2014

Of note: https://github.com/JuliaLang/DataStructures.jl contains DefaultDict and DefaultOrderedDict implementations.

StefanKarpinski added a commit that referenced this issue Feb 9, 2014
RFC: Add `get!` for Dicts (addresses #1529)
@kmsquire
Copy link
Member

kmsquire commented Feb 9, 2014

Addressed by #5519

@kmsquire kmsquire closed this as completed Feb 9, 2014
fredrikekre pushed a commit that referenced this issue Dec 13, 2019
…34091)

git log --oneline 0c2dddd40e4d7492d2a7337be54c345011e5f1e1^..8e236a7f993f1e732ffd0ab5c15736b2594e4109

8e236a7 (HEAD -> master, origin/master, origin/HEAD) Merge pull request #1544 from JuliaLang/sk/telemetry
90b8482 telemetry: factor out telemetry file loading
228fb97 CI telemetry: send indicators for common CI env vars
246dbd0 Pkg protocol: basic anonymous opt-out telemetry
e66a75f Introduce special REPL syntax for shared environments (#1488)
afeb1ee Merge pull request #1538 from JuliaLang/sk/pkg-client-auth
9c357bb Pkg client auth: support connecting to authenticated Pkg servers
6dd7f34 PlatformEngines: revert API part of headers support (broken)
6825b48 Merge pull request #1539 from 00vareladavid/00/fixes
3f1cf40 it is invalid to `add` a package with no commits
0766765 test: default environment should be created when the primary depot does not exist
43f46f8 check no overwrite is occuring when resolving from a project file
37b6853 handle primary depot as relative path
53fdf24 Check for duplicate name/UUID input
8a6387c Remove redundant precompile statement
4d0901e Dont throw error when autocompleting faulty input (#1530)
d69f6d7 Refactor and test `Pkg.test` (#1427)
8ca8b6d PlatformEngines: use `tar -m` to ignore mtimes (#1537)
6797928 Make sure sandbox's temp Project.toml and Manifest.toml files are writable (#1534)
f968cc9 clarify: stacked envs only affect top-level loading (#1529)
0dfef59 PlatformEngines.download: add header support (#1531)
49ab53e Fix tree hashing with nested empty directories (#1522)
0c2dddd fix #1514: install_archive call in backwards_compatible_isolation (#1517)
KristofferC pushed a commit that referenced this issue Apr 11, 2020
…34091)

git log --oneline 0c2dddd40e4d7492d2a7337be54c345011e5f1e1^..8e236a7f993f1e732ffd0ab5c15736b2594e4109

8e236a7 (HEAD -> master, origin/master, origin/HEAD) Merge pull request #1544 from JuliaLang/sk/telemetry
90b8482 telemetry: factor out telemetry file loading
228fb97 CI telemetry: send indicators for common CI env vars
246dbd0 Pkg protocol: basic anonymous opt-out telemetry
e66a75f Introduce special REPL syntax for shared environments (#1488)
afeb1ee Merge pull request #1538 from JuliaLang/sk/pkg-client-auth
9c357bb Pkg client auth: support connecting to authenticated Pkg servers
6dd7f34 PlatformEngines: revert API part of headers support (broken)
6825b48 Merge pull request #1539 from 00vareladavid/00/fixes
3f1cf40 it is invalid to `add` a package with no commits
0766765 test: default environment should be created when the primary depot does not exist
43f46f8 check no overwrite is occuring when resolving from a project file
37b6853 handle primary depot as relative path
53fdf24 Check for duplicate name/UUID input
8a6387c Remove redundant precompile statement
4d0901e Dont throw error when autocompleting faulty input (#1530)
d69f6d7 Refactor and test `Pkg.test` (#1427)
8ca8b6d PlatformEngines: use `tar -m` to ignore mtimes (#1537)
6797928 Make sure sandbox's temp Project.toml and Manifest.toml files are writable (#1534)
f968cc9 clarify: stacked envs only affect top-level loading (#1529)
0dfef59 PlatformEngines.download: add header support (#1531)
49ab53e Fix tree hashing with nested empty directories (#1522)
0c2dddd fix #1514: install_archive call in backwards_compatible_isolation (#1517)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants