julia> Dict(("$i*$j", i*j) for i in 1:3, j in 9:10)["2*9"=>18,"3*10"=>30,"1*10"=>10,"3*9"=>27,"2*10"=>20,"1*9"=>9]
julia> typeof(ans)Dict{Union(UTF8String,ASCIIString),Int64} (constructor with 3 methods)
julia> g = (i*j for i = 2:2:10, j=1:2:10)5x5 Generator{Int64,2}: (i * j for i = 2:2:10, j = 1:2:10)
julia> sum(g)750
julia> full(g)5x5 Array{Int64,2}: 2 6 10 14 18 4 12 20 28 36 6 18 30 42 54 8 24 40 56 72 10 30 50 70 90
julia> ntuple(i*2 for i = -5:5)(-10,-8,-6,-4,-2,0,2,4,6,8,10)
julia> sum(i for i=1:1); @time sum(i for i=1:100000000)elapsed time: 7.959239089 seconds (3199996408 bytes allocated)5000000050000000
julia> sum([i for i=1:1]); @time sum([i for i=1:100000000])elapsed time: 0.816201815 seconds (800000048 bytes allocated)5000000050000000
Hi Matt,I plan to take a look, but I probably don't know enough about the Julia back end to add anything constructive. I would like to say that 1) I'm impressed that you got this up and running, and 2) I would love to see this functionality in Julia.
julia> macro generator(thunk, assgn)
esc(quote
@task for $(assgn.args[1]) = $(assgn.args[2]); produce(($thunk)); end;
end)
end
julia> sum(i for i=1:1); @time sum(i for i=1:100000000)
elapsed time: 72.190015069 seconds (3200067900 bytes allocated)
5000000050000000
This is very good. I certainly want to add this feature. We should be
able to get much better performance with the following approach:
1. The function part should look like (x::T)->(...), where T is the
element type of the iterable (considering only the 1d case for now)
2. Use static_typeof on the body of the function to provide the type
parameter for the Generator. This is the secret feature used by
comprehensions.
julia> f(v,n) = (z = 0; for i=1:n; z+=v(i)::Int; end; z)
f (generic function with 1 method)
julia> f(i->i, 1); @time f(i->i, 100_000_000)
elapsed time: 3.585196659 seconds (1599992896 bytes allocated)
5000000050000000
julia> macro generator(thunk, assignment)
arg = assignment.args[1]
itr = assignment.args[2]
esc(quote
Generator("",($arg::eltype($itr)) -> ($thunk), ($itr,))
end)
end
julia> sum(i for i=1:1); @time sum(i for i=1:100_000_000)
elapsed time: 12.632105811 seconds (4000010452 bytes allocated)
5000000050000000
Yes, that's a desired feature. The idea would be that e.g. sum(x^2 for x in v) would be the equivalent of sum([x^2 for x in v]) but without having to materialize the array.
It would be nice if reduce functions (like sum) can be implemented only once.
julia> include("base/generator.jl");
julia> sum(i for i=1:1); @time sum(i for i=1:100000000)
elapsed time: 3.550339048 seconds (1599997360 bytes allocated, 48.60% gc time)
5000000050000000
julia> sum([i for i=1:1]); @time sum([i for i=1:100000000])
elapsed time: 0.66174807 seconds (800000048 bytes allocated)
5000000050000000