Skip to content

Commit

Permalink
keys(::Generator) pass through (#34678)
Browse files Browse the repository at this point in the history
* keys(::Generator) pass-through

Let keys pass through to allow lightweigth/lazy find* and arg* functions

* Update test/functional.jl

Co-authored-by: Matt Bauman <mbauman@gmail.com>

Co-authored-by: Matt Bauman <mbauman@gmail.com>
Co-authored-by: Simeon Schaub <simeondavidschaub99@gmail.com>
  • Loading branch information
3 people authored Dec 27, 2020
1 parent 556cee7 commit 5d2fcdb
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions base/generator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ length(g::Generator) = length(g.iter)
size(g::Generator) = size(g.iter)
axes(g::Generator) = axes(g.iter)
ndims(g::Generator) = ndims(g.iter)
keys(g::Generator) = keys(g.iter)


## iterator traits
Expand Down
5 changes: 5 additions & 0 deletions test/functional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ let gen = ((x,y) for x in 1:10, y in 1:10 if x % 2 == 0 && y % 2 == 0),
@test collect(gen) == collect(gen2)
end

# keys of a generator for find* and arg* (see #34678)
@test keys(x^2 for x in -1:0.5:1) == 1:5
@test findall(!iszero, x^2 for x in -1:0.5:1) == [1, 2, 4, 5]
@test argmin(x^2 for x in -1:0.5:1) == 3

# inference on vararg generator of a type (see #22907 comments)
let f(x) = collect(Base.Generator(=>, x, x))
@test @inferred(f((1,2))) == [1=>1, 2=>2]
Expand Down

0 comments on commit 5d2fcdb

Please sign in to comment.