# What does "k for k = 1:3" expands out to be?

**URL:** <https://discourse.julialang.org/t/what-does-k-for-k-1-3-expands-out-to-be/42014>\
**Category:** New to Julia\
**Created:** [June 24, 2020, 11:19pm UTC](https://discourse.julialang.org/t/what-does-k-for-k-1-3-expands-out-to-be/42014 "2020-06-24T23:19:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [June 24, 2020, 11:19pm UTC](https://discourse.julialang.org/t/what-does-k-for-k-1-3-expands-out-to-be/42014/1 "2020-06-24T23:19:51Z")

</div>

I am confused here

```julia
julia> sum([1,2,3])
6

julia> sum([k for k = 1:3])
6

julia> sum(k for k = 1:3)
6

```

But when I expand out “k for k = 1:3” manually it does not work

```julia
julia> sum(1,2,3)
ERROR: MethodError: no method matching sum(::Int64, ::Int64, ::Int64)
Closest candidates are:
  sum(::Any, ::Any) at reduce.jl:486
  sum(::Any) at reduce.jl:503
  sum(::Any, ::AbstractArray; dims) at reducedim.jl:653
Stacktrace:
 [1] top-level scope at REPL[42]:1

julia> sum(1.0,2.0,3.0)
ERROR: MethodError: no method matching sum(::Float64, ::Float64, ::Float64)
Closest candidates are:
  sum(::Any, ::Any) at reduce.jl:486
  sum(::Any) at reduce.jl:503
  sum(::Any, ::AbstractArray; dims) at reducedim.jl:653
Stacktrace:
 [1] top-level scope at REPL[43]:1

```

Why doesn’t sum(1,2,3) give me 6?

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [June 24, 2020, 11:35pm UTC](https://discourse.julialang.org/t/what-does-k-for-k-1-3-expands-out-to-be/42014/2 "2020-06-24T23:35:30Z")

</div>

Are you trying to use a generator?

```julia
julia> (k for k = 1:3)
Base.Generator{UnitRange{Int64},typeof(identity)}(identity, 1:3)

julia> sum((k for k = 1:3))
6

```

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [June 24, 2020, 11:37pm UTC](https://discourse.julialang.org/t/what-does-k-for-k-1-3-expands-out-to-be/42014/3 "2020-06-24T23:37:34Z")

</div>

To answer the question, `sum(1,2,3)` doesn’t work, because, as the error message tells you, there is no `sum` method which takes three arguments like that (or more for that matter). What you want is to sum a generator.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [June 24, 2020, 11:49pm UTC](https://discourse.julialang.org/t/what-does-k-for-k-1-3-expands-out-to-be/42014/4 "2020-06-24T23:49:05Z")

</div>

> [@StevenSiew](#):
>
> `sum(1,2,3)`

To achieve this you need to define

```julia-auto
sum(vals...) = sum(vals)

```

You need something to “slurp” all the `vals` into one tuple/array.

---

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [June 24, 2020, 11:49pm UTC](https://discourse.julialang.org/t/what-does-k-for-k-1-3-expands-out-to-be/42014/5 "2020-06-24T23:49:44Z")

</div>

Easier way to visualize this is that

`sum(x for x = 1:3)` is not equal to `sum(1, 2, 3)`. The former only has one argument (the generator) and the latter has 3 arguments for which sum is not defined.

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [June 24, 2020, 11:55pm UTC](https://discourse.julialang.org/t/what-does-k-for-k-1-3-expands-out-to-be/42014/6 "2020-06-24T23:55:51Z")

</div>

Maybe I misunderstood the question: I think the more interesting question is why does `sum(k for k = 1:3)` work at all, given that `k for k = 1:3` on its own does not work. And that’s because (I assume) if a generator-looking thing is passed directly to a function, even **without** enclosing parens, it is still parsed as a generator. The method that gets called is therefore `sum(g::Generator)`, **not** `sum(1,2,3)`.

Interestingly, it also works when there are multiple function arguments:

```julia
julia> sum(abs, k for k = -1:3)
7

```

You can see how this is parsed similarly with tuples (with and without parens):

```julia
julia> (1,2,3,(k for k = -1:3))
(1, 2, 3, Base.Generator{UnitRange{Int64},typeof(identity)}(identity, -1:3))

julia> (1,2,3,k for k = -1:3)
(1, 2, 3, Base.Generator{UnitRange{Int64},typeof(identity)}(identity, -1:3))

```
