Skip to content

Commit c0f27e8

Browse files
author
Colin Leach
committed
moved from wip, config.json added
1 parent 003ed5f commit c0f27e8

File tree

6 files changed

+142
-1
lines changed

6 files changed

+142
-1
lines changed

concepts.wip/pairs-and-dicts/introduction.md

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# Introduction
2+
3+
## Pairs
4+
5+
A `Pair` is just two items joined together.
6+
The items are then imaginatively called `first` and `second`.
7+
8+
Create them either with the `=>` operator or the `Pair()` constructor.
9+
10+
```julia-repl
11+
julia> p1 = "k" => 2
12+
"k" => 2
13+
14+
julia> p2 = Pair("k", 2)
15+
"k" => 2
16+
17+
# Both forms of syntax give the same result
18+
julia> p1 == p2
19+
true
20+
21+
# Each component has its own separate type
22+
julia> dump(p1)
23+
Pair{String, Int64}
24+
first: String "k"
25+
second: Int64 2
26+
27+
# Get a component using dot syntax
28+
julia> p1.first
29+
"k"
30+
31+
julia> p1.second
32+
2
33+
```
34+
35+
## Dicts
36+
37+
A `Vector` of Pairs is like any other array: ordered, homogeneous in type, and stored consecutively in memory.
38+
39+
```julia-repl
40+
julia> pv = ['a' => 1, 'b' => 2, 'c' => 3]
41+
3-element Vector{Pair{Char, Int64}}:
42+
'a' => 1
43+
'b' => 2
44+
'c' => 3
45+
46+
# Each pair is a single entry
47+
julia> length(pv)
48+
3
49+
```
50+
51+
A `Dict` is superficially similar, but storage is now implemented in a way that allows fast retrieval by key, even when the number of entries grows large.
52+
53+
```julia-repl
54+
julia> pd = Dict(pv)
55+
Dict{Char, Int64} with 3 entries:
56+
'a' => 1
57+
'c' => 3
58+
'b' => 2
59+
60+
julia> pd['b']
61+
2
62+
63+
# Key must exist
64+
julia> pd['d']
65+
ERROR: KeyError: key 'd' not found
66+
67+
# Generators are accepted in the constructor (and note the unordered output)
68+
julia> Dict(x => x^2 for x in 1:5)
69+
julia> Dict(x => 1 / x for x in 1:5)
70+
Dict{Int64, Float64} with 5 entries:
71+
5 => 0.2
72+
4 => 0.25
73+
2 => 0.5
74+
3 => 0.333333
75+
1 => 1.0
76+
```
77+
78+
In other languages, something very similar to a `Dict` might be called a dictionary (Python), a Hash (Ruby) or a HashMap (Java).
79+
80+
For Pairs, whether in isolation or in a Vector, there are few constraints on the type of each component.
81+
82+
To be valid in a `Dict`, the `Pair` must be a `key => value` pair, where the `key` is "hashable".
83+
Most importantly, this means the `key` must be _immutable_, so `Char`, `Int`, `String`, `Symbol`, and `Tuple` are all fine, but `Vector` is not allowed.
84+
85+
### Modifying a Dict
86+
87+
Entries can be added with a new key or overwritten with an existing key.
88+
89+
```julia-repl
90+
# Add
91+
julia> pd['d'] = 4
92+
4
93+
94+
# Overwrite
95+
julia> pd['a'] = 42
96+
42
97+
98+
julia> pd
99+
Dict{Char, Int64} with 4 entries:
100+
'a' => 42
101+
'c' => 3
102+
'd' => 4
103+
'b' => 2
104+
```
105+
106+
To remove an entry, use the `delete!()` function, which will change the Dict if the key exists and silently do nothing otherwise.
107+
108+
```julia-repl
109+
julia> delete!(pd, 'd')
110+
Dict{Char, Int64} with 3 entries:
111+
'a' => 42
112+
'c' => 3
113+
'b' => 2
114+
```
115+
116+
### Checking if a key or value exists
117+
118+
There are different approaches.
119+
To check a key, there is a `haskey()` function:
120+
121+
```julia-repl
122+
julia> haskey(pd, 'b')
123+
true
124+
```
125+
126+
Alternatively, search either the keys or the values:
127+
128+
```julia-repl
129+
julia> 'b' in keys(pd)
130+
true
131+
132+
julia> 43 in values(pd)
133+
false
134+
135+
julia> 42 ∈ values(pd)
136+
true
137+
```

config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,11 @@
12671267
"uuid": "95b8b60b-fd78-4ec1-9d1c-ac3f11107727",
12681268
"slug": "chars",
12691269
"name": "Chars"
1270+
},
1271+
{
1272+
"uuid": "0d312606-7738-4b0a-9c33-564c40c6f35d",
1273+
"slug": "pairs-and-dicts",
1274+
"name": "Pairs And Dicts"
12701275
}
12711276
],
12721277
"key_features": [

0 commit comments

Comments
 (0)