-
-
Notifications
You must be signed in to change notification settings - Fork 74
Dict Functions Concept [DRAFT] #994
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
Draft
colinleach
wants to merge
49
commits into
exercism:main
Choose a base branch
from
colinleach:dict-functions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+207
−0
Draft
Changes from all commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
28f91a8
concepts.wip exercises changed to deprecated
b822547
Revert "concepts.wip exercises changed to deprecated"
02ef965
Merge remote-tracking branch 'origin/main'
9e7ebc7
Merge remote-tracking branch 'origin/main'
09dfe5d
Change annelyns-infiltration slug for old version
17f24b1
Revert "Change annelyns-infiltration slug for old version"
5b7c4b9
Merge remote-tracking branch 'origin/main'
ac53662
Merge remote-tracking branch 'origin/main'
2af0aca
Merge remote-tracking branch 'origin/main'
c99cb29
Merge remote-tracking branch 'origin/main'
44d5047
Merge remote-tracking branch 'origin/main'
c368c5c
Merge remote-tracking branch 'origin/main'
1ff8c7e
Merge remote-tracking branch 'origin/main'
5b9492e
Merge remote-tracking branch 'origin/main'
f44fc08
Merge remote-tracking branch 'origin/main'
74542cc
Merge remote-tracking branch 'origin/main'
0eac864
Merge remote-tracking branch 'origin/main'
316fb7e
Merge remote-tracking branch 'origin/main'
ba5e3ad
Merge remote-tracking branch 'origin/main'
33da2a6
Merge remote-tracking branch 'origin/main'
d45f584
Merge remote-tracking branch 'origin/main'
b806247
Merge remote-tracking branch 'origin/main'
7eded14
Merge remote-tracking branch 'origin/main'
c037eca
Merge remote-tracking branch 'origin/main'
40cf0fd
Merge remote-tracking branch 'origin/main'
50c3513
Merge remote-tracking branch 'origin/main'
9ab3aa9
Merge remote-tracking branch 'origin/main'
a6f1f00
Merge remote-tracking branch 'origin/main'
91c8c1b
Merge remote-tracking branch 'origin/main'
e7dd0ca
Merge remote-tracking branch 'origin/main'
cf9fce0
Merge remote-tracking branch 'origin/main'
e3f1c5b
Merge remote-tracking branch 'origin/main'
0d00a9a
Merge remote-tracking branch 'origin/main'
58cacc5
Merge remote-tracking branch 'origin/main'
aca118b
Merge remote-tracking branch 'origin/main'
db48b69
Merge remote-tracking branch 'origin/main'
c4f4961
Merge remote-tracking branch 'origin/main'
0674736
Merge remote-tracking branch 'origin/main'
1a46425
Merge remote-tracking branch 'origin/main'
17bbfd8
Merge remote-tracking branch 'origin/main'
0b943b6
Merge remote-tracking branch 'origin/main'
f7d03cb
Merge remote-tracking branch 'origin/main'
161c53e
Merge remote-tracking branch 'origin/main'
3207029
Merge remote-tracking branch 'origin/main'
863f44d
Merge remote-tracking branch 'origin/main'
b16461c
Merge remote-tracking branch 'origin/main'
23c1e23
Merge remote-tracking branch 'origin/main'
803b095
`dict-functions` Concept
72699f2
response to reviewer
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"authors": [ | ||
"colinleach" | ||
], | ||
"contributors": [], | ||
"blurb": "Julia supplies an extensive variety of functions to help us work with dictionaries." | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
# About | ||
|
||
The [Pairs and Dicts][pairs-dicts] Concept discussed the basics of Julia's dictionary type. | ||
|
||
Because [`Dict`s][dict] are an important and often-used collection type, there are also many functions to manipulate them. | ||
|
||
## Getting entries | ||
|
||
The simplest way to retrieve an entry is by key (as in many languages). | ||
However, asking for a non-existent key is a `KeyError`. | ||
|
||
```julia-repl | ||
julia> d = Dict(i => ('a':'c')[i] for i in 1:3) | ||
Dict{Int64, Char} with 3 entries: | ||
2 => 'b' | ||
3 => 'c' | ||
1 => 'a' | ||
|
||
julia> d[2] | ||
'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase) | ||
|
||
# missing key | ||
julia> d[4] | ||
ERROR: KeyError: key 4 not found | ||
``` | ||
|
||
We could test for the key before trying to retrieve it (with something like `haskey(d, 4)` or `4 ∈ keys(d)`), but it can be useful to just get a default value for missing keys. | ||
|
||
For this, we have the [`get()`][get] function and its mutating variant [`get!()`][get-bang]. | ||
These differ in whether the "missing" key is added to the `Dict`. | ||
|
||
```julia-repl | ||
julia> get(d, 4, '!') | ||
'!': ASCII/Unicode U+0021 (category Po: Punctuation, other) | ||
|
||
# get() leaves Dict unchanged | ||
julia> d | ||
Dict{Int64, Char} with 3 entries: | ||
2 => 'b' | ||
3 => 'c' | ||
1 => 'a' | ||
|
||
julia> get!(d, 4, '!') | ||
'!': ASCII/Unicode U+0021 (category Po: Punctuation, other) | ||
|
||
# get!() adds new key to Dict | ||
julia> d | ||
Dict{Int64, Char} with 4 entries: | ||
4 => '!' | ||
2 => 'b' | ||
3 => 'c' | ||
1 => 'a' | ||
``` | ||
|
||
We saw in the earlier Concept that `delete!()` will remove an entry by key. | ||
This can be combined with retrieving the value, by using the `pop!()` function. | ||
|
||
```julia-repl | ||
julia> d | ||
Dict{Int64, Char} with 4 entries: | ||
4 => '!' | ||
2 => 'b' | ||
3 => 'c' | ||
1 => 'a' | ||
|
||
# popped value is returned | ||
julia> pop!(d, 3) | ||
'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase) | ||
|
||
# Dict is shrunk | ||
julia> d | ||
Dict{Int64, Char} with 3 entries: | ||
4 => '!' | ||
2 => 'b' | ||
1 => 'a' | ||
``` | ||
|
||
## Iteration | ||
|
||
Iterating over a `Dict` defaults to yielding `key => value` pairs. | ||
The components are accessible with the usual `.first` and `.second` nomenclature. | ||
|
||
```julia-repl | ||
# d is from the previous example | ||
julia> [(kv.first, kv.second) for kv in d] | ||
3-element Vector{Tuple{Int64, Char}}: | ||
(4, '!') | ||
(2, 'b') | ||
(1, 'a') | ||
``` | ||
|
||
Often, we want to use just the keys or just the values, which is easy. | ||
|
||
```juliajulia> [k for k in keys(d)] | ||
julia> [k for k in keys(d)] | ||
3-element Vector{Int64}: | ||
4 | ||
2 | ||
1 | ||
|
||
julia> [v for v in values(d)] | ||
3-element Vector{Char}: | ||
'!': ASCII/Unicode U+0021 (category Po: Punctuation, other) | ||
'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase) | ||
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase) | ||
``` | ||
|
||
## Merging | ||
|
||
Combining two `Dict`s can be useful, but we need to be clear about what we intend to do with keys that are present in more than one input. | ||
|
||
The [`merge()`][merge] and [`merge!()`][merge-bang] functions will combine an arbitrary number of supplied `Dict`s in a left-to right fashion. | ||
|
||
Existing keys have their values simply overwritten, so the right-most `Dict` "wins". | ||
|
||
```julia-repl | ||
julia> d3 = Dict("color" => "red", "shape" => "circle") | ||
Dict{String, String} with 2 entries: | ||
"shape" => "circle" | ||
"color" => "red" | ||
|
||
julia> d4 = Dict("color" => "blue", "coords" => (3, 2)) | ||
Dict{String, Any} with 2 entries: | ||
"color" => "blue" | ||
"coords" => (3, 2) | ||
|
||
# color from d3 is lost | ||
julia> merge(d3, d4) | ||
Dict{String, Any} with 3 entries: | ||
"shape" => "circle" | ||
"color" => "blue" | ||
"coords" => (3, 2) | ||
``` | ||
|
||
The [`mergewith()`][mergewith] and [`mergewith!()`][mergewith-bang] functions lets us supply a function as the first argument, to define how repeated keys should be merged. | ||
Addition is a simple example. | ||
|
||
```julia-repl | ||
julia> d5 = Dict("color" => "red", "area" => 3) | ||
Dict{String, Any} with 2 entries: | ||
"area" => 3 | ||
"color" => "red" | ||
|
||
julia> d6 = Dict("shape" => "circle", "area" => 5) | ||
Dict{String, Any} with 2 entries: | ||
"shape" => "circle" | ||
"area" => 5 | ||
|
||
# area values are added, not overwritten | ||
julia> mergewith(+, d5, d6) | ||
Dict{String, Any} with 3 entries: | ||
"shape" => "circle" | ||
"area" => 8 | ||
"color" => "red" | ||
``` | ||
|
||
Concatenating two vectors is another popular operation when merging. | ||
For this, Julia uses the [`vcat()`][vcat] function, for reasons that will become clearer in the [`Multi Dimensional Arrays`][multidim] Concept. | ||
|
||
```julia-repl | ||
julia> d7 = Dict("vals" => [1, 2, 3]) | ||
Dict{String, Vector{Int64}} with 1 entry: | ||
"vals" => [1, 2, 3] | ||
|
||
julia> d8 = Dict("vals" => [4, 5]) | ||
Dict{String, Vector{Int64}} with 1 entry: | ||
"vals" => [4, 5] | ||
|
||
julia> mergewith(vcat, d7, d8) | ||
Dict{String, Vector{Int64}} with 1 entry: | ||
"vals" => [1, 2, 3, 4, 5] | ||
``` | ||
|
||
For multiple [`Set`s][sets], the corresponding merge function is [`union`][union]. | ||
|
||
|
||
[pairs-dicts]: https://exercism.org/tracks/julia/concepts/pairs-and-dicts | ||
[dict]: https://docs.julialang.org/en/v1/base/collections/#Dictionaries | ||
[get]: https://docs.julialang.org/en/v1/base/collections/#Base.get | ||
[get-bang]: https://docs.julialang.org/en/v1/base/collections/#Base.get! | ||
[merge]: https://docs.julialang.org/en/v1/base/collections/#Base.merge | ||
[merge-bang]: https://docs.julialang.org/en/v1/base/collections/#Base.merge! | ||
[mergewith]: https://docs.julialang.org/en/v1/base/collections/#Base.mergewith | ||
[mergewith-bang]: https://docs.julialang.org/en/v1/base/collections/#Base.mergewith! | ||
[multidim]: https://exercism.org/tracks/julia/concepts/multi-dimensional-arrays | ||
[vcat]: https://docs.julialang.org/en/v1/base/arrays/#Base.vcat | ||
[union]: https://docs.julialang.org/en/v1/base/collections/#Base.union | ||
[sets]: https://exercism.org/tracks/julia/concepts/sets |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Introduction |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[ | ||
{ | ||
"url": "https://docs.julialang.org/en/v1/base/collections/#Dictionaries", | ||
"description": "Julia manual: Dictionaries" | ||
} | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.