Skip to content

Commit 1aa2b90

Browse files
iamed2ararslan
authored andcommitted
Implement pairs like PR 22907 (#428)
1 parent d2b7093 commit 1aa2b90

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ Currently, the `@compat` macro supports the following syntaxes:
201201

202202
* `IOContext` accepting key-value `Pair`s ([#23271]).
203203

204+
* `pairs` for iterating over key-value `Pair`s ([#22907]).
205+
204206
* `get` do-block syntax supported when using `ENV` ([#23412]).
205207

206208
## Renaming
@@ -368,6 +370,7 @@ includes this fix. Find the minimum version from there.
368370
[#22751]: https://github.com/JuliaLang/julia/issues/22751
369371
[#22761]: https://github.com/JuliaLang/julia/issues/22761
370372
[#22864]: https://github.com/JuliaLang/julia/issues/22864
373+
[#22907]: https://github.com/JuliaLang/julia/issues/22907
371374
[#23051]: https://github.com/JuliaLang/julia/issues/23051
372375
[#23235]: https://github.com/JuliaLang/julia/issues/23235
373376
[#23271]: https://github.com/JuliaLang/julia/issues/23271

src/Compat.jl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,51 @@ end
673673
export partialsort, partialsort!, partialsortperm, partialsortperm!
674674
end
675675

676+
# 0.7.0-DEV.1660
677+
@static if !isdefined(Base, :pairs)
678+
pairs(collection) = Base.Generator(=>, keys(collection), values(collection))
679+
pairs(a::Associative) = a
680+
681+
# 0.6.0-dev+2834
682+
@static if !isdefined(Iterators, :IndexValue)
683+
include_string(@__MODULE__, """
684+
immutable IndexValue{I,A<:AbstractArray}
685+
data::A
686+
itr::I
687+
end
688+
""")
689+
690+
Base.length(v::IndexValue) = length(v.itr)
691+
Base.indices(v::IndexValue) = indices(v.itr)
692+
Base.size(v::IndexValue) = size(v.itr)
693+
@inline Base.start(v::IndexValue) = start(v.itr)
694+
Base.@propagate_inbounds function Base.next(v::IndexValue, state)
695+
indx, n = next(v.itr, state)
696+
item = v.data[indx]
697+
(indx => item), n
698+
end
699+
@inline Base.done(v::IndexValue, state) = done(v.itr, state)
700+
701+
Base.eltype{I,A}(::Type{IndexValue{I,A}}) = Pair{eltype(I), eltype(A)}
702+
703+
Base.iteratorsize{I}(::Type{IndexValue{I}}) = iteratorsize(I)
704+
Base.iteratoreltype{I}(::Type{IndexValue{I}}) = iteratoreltype(I)
705+
706+
Base.reverse(v::IndexValue) = IndexValue(v.data, reverse(v.itr))
707+
else
708+
const IndexValue = Iterators.IndexValue
709+
end
710+
711+
pairs(::IndexLinear, A::AbstractArray) = IndexValue(A, linearindices(A))
712+
pairs(::IndexCartesian, A::AbstractArray) = IndexValue(A, CartesianRange(indices(A)))
713+
714+
Base.keys(a::AbstractArray) = CartesianRange(indices(a))
715+
Base.keys(a::AbstractVector) = linearindices(a)
716+
Base.keys(s::IndexStyle, A::AbstractArray, B::AbstractArray...) = eachindex(s, A, B...)
717+
718+
Base.values(itr) = itr
719+
end
720+
676721
# 0.7.0-DEV.1721
677722
@static if !isdefined(Base, :AbstractRange)
678723
const AbstractRange = Range

test/runtests.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,34 @@ let x = fill!(StringVector(5), 0x61)
741741
@test pointer(x) == pointer(String(x))
742742
end
743743

744+
# PR 22907
745+
using Compat: pairs
746+
747+
# keys, values, pairs
748+
for A in (rand(2), rand(2,3))
749+
local A
750+
for (i, v) in pairs(A)
751+
@test A[i] == v
752+
end
753+
@test collect(values(A)) == collect(A)
754+
end
755+
756+
let A = Dict(:foo=>1, :bar=>3)
757+
for (k, v) in pairs(A)
758+
@test A[k] == v
759+
end
760+
@test sort!(collect(pairs(A))) == sort!(collect(A))
761+
end
762+
763+
let
764+
A14 = [11 13; 12 14]
765+
R = CartesianRange(indices(A14))
766+
@test [a for (a,b) in pairs(IndexLinear(), A14)] == [1,2,3,4]
767+
@test [a for (a,b) in pairs(IndexCartesian(), A14)] == vec(collect(R))
768+
@test [b for (a,b) in pairs(IndexLinear(), A14)] == [11,12,13,14]
769+
@test [b for (a,b) in pairs(IndexCartesian(), A14)] == [11,12,13,14]
770+
end
771+
744772
# Val(x)
745773
# 0.7
746774
begin

0 commit comments

Comments
 (0)