El miércoles, 15 de julio de 2015, 14:03:16 (UTC-5), Mauricio Esteban Cuak escribió: > > Just in case it seems a bit more transparent, this also works: > > r = rand(5) > map( x -> x[1] + x[2], enumerate(r)) > > > I personally find the following array comprehension the most transparent option:
[i+v for (i,v) in enumerate(r)] It also has the benefit of being much faster (Julia 0.3.11-pre): julia> f1(r) = [i+v for (i,v) in enumerate(r)] f1 (generic function with 1 method) julia> f2(r) = map( x -> x[1] + x[2], enumerate(r)) f2 (generic function with 1 method) # warm-up: r = rand(5) f1(r) f2(r); r = rand(10^6) julia> @time f1(r); elapsed time: 0.007597053 seconds (8000144 bytes allocated) julia> @time f2(r); elapsed time: 0.313020208 seconds (160753984 bytes allocated, 35.39% gc time) > > El martes, 14 de julio de 2015, 20:20:14 (UTC-5), Yichao Yu escribió: >> >> On Tue, Jul 14, 2015 at 7:09 PM, Ritchie Lee <ritch...@gmail.com> wrote: >> > Sorry if this is a newbie question. I am trying to get map() to work >> with >> > with enumerate() but I am getting an error. >> > >> > r=rand(5) >> > map((i,v)->i+v, enumerate(r)) >> > ERROR: wrong number of arguments >> > in anonymous at none:1 >> > in map at abstractarray.jl:1207 >> > >> > collect(enumerate(r)) generates an array of 2-tuples as expected. >> >> `map` does not splat each element of the iteration and only pass each >> element as a single argument to the function >> >> ```julia >> julia> r = rand(5) >> map(x->((i, v) = x; i + v), enumerate(r)) >> 5-element Array{Any,1}: >> 1.6277 >> 2.41751 >> 3.34848 >> 4.70194 >> 5.89139 >> >> ``` >> >> > >> > Is the syntax incorrect? >> > >> > Thanks, >> > >> > Ritchie >> >