Skip to content

Commit bad925e

Browse files
committed
Support all iterators again
1 parent f40839b commit bad925e

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

NEWS.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ New library features
9292
* New constructor `NamedTuple(iterator)` that constructs a named tuple from a key-value pair iterator.
9393
* A new `reinterpret(reshape, T, a::AbstractArray{S})` reinterprets `a` to have eltype `T` while potentially
9494
inserting or consuming the first dimension depending on the ratio of `sizeof(T)` and `sizeof(S)`.
95-
* Generators support `getindex` when the underlying iterator is a subtype of `AbstractArray` ([#37648]).
95+
* Generators support `getindex`, `firstindex`, and `lastindex` ([#37648]).
9696

9797
Standard library changes
9898
------------------------

base/generator.jl

+10
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ function getindex(g::Generator{<:AbstractArray}, I...)
6262
end
6363
end
6464

65+
function getindex(g::Generator, I...)
66+
A = collect(g.iter)
67+
subset = A[I...]
68+
if typeof(subset) == eltype(A)
69+
g.f(subset)
70+
else
71+
map(g.f, subset)
72+
end
73+
end
74+
6575
firstindex(g::Generator) = firstindex(g.iter)
6676
lastindex(g::Generator) = lastindex(g.iter)
6777

test/functional.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,5 +230,5 @@ end
230230

231231
@test (tuple(x) for x in ["a"])[1] == ("a",)
232232
@test (tuple(x) for x in [[0]])[1] == ([0],)
233-
@test_throws MethodError (x * y for (x, y) in Dict(3 => 4))[1]
233+
@test (x * y for (x, y) in Dict(3 => 4))[1] == 12
234234
end

0 commit comments

Comments
 (0)