Skip to content

Commit

Permalink
jldoctest context
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Jan 28, 2020
1 parent e7635e1 commit 386318a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
14 changes: 6 additions & 8 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Given a single iterable argument, constructs a [`Dict`](@ref) whose key-value pa
are taken from 2-tuples `(key,value)` generated by the argument.
# Examples
```jldoctest
```jldoctest dictexamples
julia> Dict([("A", 1), ("B", 2)])
Dict{String,Int64} with 2 entries:
"B" => 2
Expand All @@ -68,15 +68,15 @@ Dict{String,Int64} with 2 entries:
Alternatively, a sequence of pair arguments may be passed.
```jldoctest
```jldoctest dictexamples
julia> d = Dict("A"=>1, "B"=>2)
Dict{String,Int64} with 2 entries:
"B" => 2
"A" => 1
```
After creation of a Dict you can use `d[x]` for getting the values and `d[x]=y` for changing the values.
```jldoctest
```jldoctest dictexamples
julia> d["A"]
1
Expand All @@ -86,7 +86,7 @@ julia> d["B"] = 3
You can use [Generator](https://docs.julialang.org/en/v1/manual/arrays/#Generator-Expressions-1) and [Comprehension][https://docs.julialang.org/en/v1/manual/arrays/#Comprehensions-1] syntax to create dictionaries:
```jldoctest
```jldoctest dictexamples
julia> Dict(x => x^2 for x = 0:1:4)
Dict{Int64,Int64} with 5 entries:
0 => 0
Expand All @@ -99,7 +99,7 @@ Dict{Int64,Int64} with 5 entries:
You can also create and fill dictionaries dynamically (will be slower than the other ways).
First, you need to create an empty dictionary using:
```jldoctest
```jldoctest dictexamples
julia> Dict{Int64,Any}() # type specified
Dict{Int64,Any} with 0 entries
Expand All @@ -108,16 +108,14 @@ Dict{Any,Any} with 0 entries
```
After creating an empty dictionary, you can fill it via a for loop:
```jldoctest
```jldoctest dictexamples
mydict = Dict{Int64,Any}()
animals = ["cat", "fish", "elephant"]
for (i, x) in enumerate(animals)
mydict[i] = [x, length(x)]
end
```
```jldoctest
julia> mydict
Dict{Int64,Any} with 3 entries:
2 => Any["fish", 4]
Expand Down
5 changes: 3 additions & 2 deletions doc/src/base/collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,9 @@ Dictionaries may also be created with generators. For example, `Dict(i => f(i) f

Given a dictionary `D`, the syntax `D[x]` returns the value of key `x` (if it exists) or throws
an error, and `D[x] = y` stores the key-value pair `x => y` in `D` (replacing any existing value
for the key `x`). Multiple arguments to `D[...]` are converted to tuples; for example, the syntax
`D[x,y]` is equivalent to `D[(x,y)]`, i.e. it refers to the value keyed by the tuple `(x,y)`.
for the key `x`).

Multiple arguments to `D[...]` are converted to tuples; for example, the syntax `D[x,y]` is equivalent to `D[(x,y)]`, i.e. it refers to the value keyed by the tuple `(x,y)`.

```@docs
Base.AbstractDict
Expand Down

0 comments on commit 386318a

Please sign in to comment.