Skip to content

Commit b483c6d

Browse files
authored
Add doctest for pairs (#42504)
1 parent 7a8a39b commit b483c6d

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

base/abstractdict.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,38 @@ values(a::AbstractDict) = ValueIterator(a)
136136
Return an iterator over `key => value` pairs for any
137137
collection that maps a set of keys to a set of values.
138138
This includes arrays, where the keys are the array indices.
139+
140+
# Examples
141+
```jldoctest
142+
julia> a = Dict(zip(["a", "b", "c"], [1, 2, 3]))
143+
Dict{String, Int64} with 3 entries:
144+
"c" => 3
145+
"b" => 2
146+
"a" => 1
147+
148+
julia> pairs(a)
149+
Dict{String, Int64} with 3 entries:
150+
"c" => 3
151+
"b" => 2
152+
"a" => 1
153+
154+
julia> foreach(println, pairs(["a", "b", "c"]))
155+
1 => "a"
156+
2 => "b"
157+
3 => "c"
158+
159+
julia> (;a=1, b=2, c=3) |> pairs |> collect
160+
3-element Vector{Pair{Symbol, Int64}}:
161+
:a => 1
162+
:b => 2
163+
:c => 3
164+
165+
julia> (;a=1, b=2, c=3) |> collect
166+
3-element Vector{Int64}:
167+
1
168+
2
169+
3
170+
```
139171
"""
140172
pairs(collection) = Generator(=>, keys(collection), values(collection))
141173

0 commit comments

Comments
 (0)