It’s about Generator Expressions.
I used to only know about sum(i for i in 1:I)
.
But today I happened to discover that we can also write minimum(i for i in 1:I)
.
I’m thus intrigued:
- Do all function who takes an Vector arg can comprehend grammar like this?
I then came up with a counter-example which fails
julia> a
4-element Vector{Bool}:
0
0
1
1
julia> b
4-element Vector{Bool}:
0
1
0
1
julia> findall(a .& b)
1-element Vector{Int64}:
4
julia> [(i & j) for (i, j) in zip(a, b)]
4-element Vector{Bool}:
0
0
0
1
julia> findall( (i & j) for (i, j) in zip(a, b) )
ERROR: MethodError: no method matching keys(::Base.Iterators.Zip{Tuple{Vector{Bool}, Vector{Bool}}})
The function `keys` exists, but no method is defined for this combination of argument types.
Closest candidates are:
keys(::Cmd)
@ Base process.jl:716
keys(::Base.TermInfo)
@ Base terminfo.jl:232
keys(::BenchmarkTools.BenchmarkGroup)
@ BenchmarkTools K:\judepot1114\packages\BenchmarkTools\1i1mY\src\groups.jl:75
...
Stacktrace:
[1] keys(g::Base.Generator{Base.Iterators.Zip{Tuple{Vector{Bool}, Vector{Bool}}}, var"#331#332"})
@ Base .\generator.jl:55
[2] pairs(collection::Base.Generator{Base.Iterators.Zip{Tuple{Vector{Bool}, Vector{Bool}}}, var"#331#332"})
@ Base .\abstractdict.jl:178
[3] findall(A::Base.Generator{Base.Iterators.Zip{Tuple{Vector{Bool}, Vector{Bool}}}, var"#331#332"})
@ Base .\array.jl:2691
[4] top-level scope
@ REPL[393]:1