Skip to content

Commit

Permalink
Fixing output of code in docstrings
Browse files Browse the repository at this point in the history
Using Julia 1.5 to get the examples output

Indentation fix

JuliaLang#33936 (comment)

Fixing jldoctests
  • Loading branch information
aminya committed Feb 6, 2020
1 parent 18b026e commit 79a07e9
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 27 deletions.
19 changes: 14 additions & 5 deletions base/abstractset.jl
Expand Up @@ -45,8 +45,13 @@ Set{Int64} with 3 elements:
```jldoctest
julia> A = Set(["yellow", "orange"]); B = Set(["blue", "black", "orange"]);
julia> union(A, B)
Set(["orange", "blue", "yellow, orange", "black"])
Set{String} with 4 elements:
"yellow"
"orange"
"blue"
"black"
```
"""
function union end
Expand Down Expand Up @@ -106,7 +111,7 @@ end
intersect(s, itrs...)
∩(s, itrs...)
Construct the intersection of sets. Maintain order with arrays.
Construct the intersection of sets/collections. Maintain order with arrays.
The intersect of two set `A` and `B` is a set that contains every element that belongs to both `A` and `B`.
Expand All @@ -130,7 +135,8 @@ Set{Int64} with 1 element:
julia> A = Set(["yellow", "orange"]); B = Set(["blue", "black", "orange"]);
julia> intersect(A, B)
Set(["orange"])
Set{String} with 1 element:
"orange"
```
"""
intersect(s::AbstractSet, itr, itrs...) = intersect!(intersect(s, itr), itrs...)
Expand Down Expand Up @@ -175,10 +181,13 @@ julia> setdiff([1,2,3], [3,4,5])
julia> A = Set(["yellow", "orange"]); B = Set(["blue", "black", "orange"]);
julia> setdiff(A, B)
Set(["yellow"])
Set{String} with 1 element:
"yellow"
julia> setdiff(B, A)
Set(["blue", "black"])
Set{String} with 2 elements:
"blue"
"black"
```
"""
setdiff(s::AbstractSet, itrs...) = setdiff!(copymutable(s), itrs...)
Expand Down
31 changes: 21 additions & 10 deletions base/dict.jl
Expand Up @@ -111,24 +111,35 @@ Dict{Any,Any} with 0 entries
After creating an empty dictionary, fill it via a for loop:
```jldoctest dictexamples
julia> animaldict = Dict()
animaldict = Dict();
animals = ["cats", "fishes", "elephants"];
animal_numbers = [229, 165, 65];
julia> animals = ["cats", "fishes", "elephants"]
julia> for (i, x) in enumerate(animals)
animaldict[x] = [i, rand(UInt8)]
for (i, x) in enumerate(animals)
animaldict[x] = [i, animal_numbers[i]]
end
animaldict
# output
Dict{Any,Any} with 3 entries:
"fishes" => [2, 165]
"elephants" => [3, 65]
"cats" => [1, 229]
```
To loop through key and value pairs use:
```jldoctest dictexamples
julia> for (key, value) in animaldict
println("$key are in group $(value[1]), and we have $(value[2]) number of them")
for (key, value) in animaldict
println("\$key are in group \$(value[1]), and we have \$(value[2]) number of them")
end
fishes are in group 2, and we have 136 number of them
elephants are in group 3, and we have 135 numberof them
cats are in group 1, and we have 52 number of them
# output
fishes are in group 2, and we have 165 number of them
elephants are in group 3, and we have 65 number of them
cats are in group 1, and we have 229 number of them
```
"""
mutable struct Dict{K,V} <: AbstractDict{K,V}
Expand Down
22 changes: 16 additions & 6 deletions base/set.jl
Expand Up @@ -21,13 +21,21 @@ for sets of arbitrary objects.
# Examples
```jldoctest
julia> animals = Set{String}(["cat","fish","dog","bird"])
Set(["cat","fish","dog","bird"])
Set{String} with 4 elements:
"fish"
"cat"
"bird"
"dog"
```
If the element type is not given, it is computed by intersecting the types of the arguments:
```jldoctest
julia> animals = Set(["cat","fish","dog","bird"])
Set(["cat","fish","dog","bird"])
Set{String} with 4 elements:
"fish"
"cat"
"bird"
"dog"
julia> typeof(animals)
Set{String}
Expand All @@ -36,16 +44,18 @@ Set{String}
A common way of incrementally creating a `Set` is to first create an empty set with `Set()` and then add the elements using the `push!` function:
```jldoctest
julia> int_container = Set{Int64}() # type specified
Set(Int64[])
Set{Int64} with 0 elements
julia> push!(int_container, 2)
Set([2])
Set{Int64} with 1 element:
2
julia> any_container = Set() # type not specified
Set(Any[])
Set{Any} with 0 elements
julia> push!(any_container, "hi!")
Set(Any["hi!"])
Set{Any} with 1 element:
"hi!"
```
"""
Set(itr) = _Set(itr, IteratorEltype(itr))
Expand Down
27 changes: 21 additions & 6 deletions doc/src/base/collections.md
Expand Up @@ -240,13 +240,22 @@ A [`Set`](@ref) is a collection of elements with no duplicated elements and keys

```jldoctest set_collections
julia> colors = Set(["yellow", "blue", "green", "red"])
Set(["yellow", "blue", "green", "red"])
Set{String} with 4 elements:
"yellow"
"blue"
"green"
"red"
```

Use [`push!`](@ref) to add elements to a set:
```jldoctest set_collections
julia> push!(colors, "black")
Set(["yellow", "blue", "green", "black", "red"])
Set{String} with 5 elements:
"yellow"
"blue"
"green"
"black"
"red"
```

Since sets don't store repeated elements, `push!` will not have any effect for pushing the elements that are already in the set. Also because `Set` doesn't have a concept of "first" (unlike `Array`), using [`pushfirst!`](@ref) to insert an item at the beginning of a set will result in an error.
Expand All @@ -260,21 +269,27 @@ true
To access each element of a `Set`, you can loop over the elements:
```jldoctest set_collections
julia> for color in colors
println(color)
end
println(color)
end
yellow
blue
green
black
red
```

Use [`filter`](@ref) for filtering the elements based on a given function:
```jldoctest set_collections
julia> filter(c -> c != "yellow", colors)
Set(["blue", "green", "red"])
Set{String} with 4 elements:
"blue"
"green"
"black"
"red"
julia> filter(c -> in(c, ["yellow", "purple"]), colors)
Set(["yellow"])
Set{String} with 1 element:
"yellow"
```

```@docs
Expand Down

0 comments on commit 79a07e9

Please sign in to comment.