# Think Julia Exercise 10.1

**URL:** https://discourse.julialang.org/t/think-julia-exercise-10-1/50098
**Category:** General Usage
**Created:** [November 13, 2020, 3:59pm UTC](https://discourse.julialang.org/t/think-julia-exercise-10-1/50098 "2020-11-13T15:59:30Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![AKL](https://avatars.discourse-cdn.com/v4/letter/a/ba9def/32.png) [@AKL](https://discourse.julialang.org/u/AKL)
#### Post date: [November 13, 2020, 3:59pm UTC](https://discourse.julialang.org/t/think-julia-exercise-10-1/50098/1 "2020-11-13T15:59:30Z")

</div>

Hey,  
I am fairly new to Julia and coding. Thus, i still struggle on a lot of things in Julia.  
I am trying to solve the exercise 10.1 and would like some help on this, at least where to start.

\*Write a function called `nestedsum` that takes an array of arrays of integers and adds up the elements from all of the nested arrays. For example : \*  
_julia\> t = [[1, 2], [3], [4, 5, 6]];_

_julia\> nestedsum(t)_  
_21_

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [November 13, 2020, 4:24pm UTC](https://discourse.julialang.org/t/think-julia-exercise-10-1/50098/2 "2020-11-13T16:24:20Z")

</div>

Start here:

[https://docs.julialang.org/en/v1/manual/control-flow/#man-loops](https://docs.julialang.org/en/v1/manual/control-flow/#man-loops)

---

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [November 13, 2020, 4:24pm UTC](https://discourse.julialang.org/t/think-julia-exercise-10-1/50098/3 "2020-11-13T16:24:57Z")

</div>

Well, the first question to ask yourself is which approach you would like to take. There are at least three different possible approaches:

- Use a `for` loop
- Use broadcasting (dot syntax)
- Use the [`map`](https://docs.julialang.org/en/v1/base/collections/#Base.map) function

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [November 13, 2020, 4:41pm UTC](https://discourse.julialang.org/t/think-julia-exercise-10-1/50098/4 "2020-11-13T16:41:01Z")

</div>

```julia
sum(vcat(t...))

```

---

<div class="post-metadata">

### Author: ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)
#### Post date: [November 13, 2020, 4:43pm UTC](https://discourse.julialang.org/t/think-julia-exercise-10-1/50098/5 "2020-11-13T16:43:51Z")

</div>

A solution using `mapreduce` looks pretty appealing as well! There is also the option of "`flatten`"ing the array.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [November 13, 2020, 4:47pm UTC](https://discourse.julialang.org/t/think-julia-exercise-10-1/50098/6 "2020-11-13T16:47:05Z")

</div>

> [@rafael.guerra](#):
>
> `sum(vcat(t...))`

Nobody should start Julia with this, for two reasons: 1) It is just using some functions without understanding what they do. 2) It is much slower than a simple implementation using loops:

```julia
julia> @btime sum(vcat($t...))
  66.714 ns (1 allocation: 128 bytes)
21

julia> @btime nestedsum($t)
  6.242 ns (0 allocations: 0 bytes)
21

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [November 13, 2020, 5:03pm UTC](https://discourse.julialang.org/t/think-julia-exercise-10-1/50098/7 "2020-11-13T17:03:39Z")

</div>

```julia
sum(sum.(t))

```

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [November 13, 2020, 5:10pm UTC](https://discourse.julialang.org/t/think-julia-exercise-10-1/50098/8 "2020-11-13T17:10:35Z")

</div>

```julia
julia> @btime sum(sum.($t))
  31.337 ns (1 allocation: 112 bytes)
21

```

Not yet 🙂

---

<div class="post-metadata">

### Author: ![fabiangans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fabiangans/32/2624_2.png) [@fabiangans](https://discourse.julialang.org/u/fabiangans)
#### Post date: [November 13, 2020, 5:14pm UTC](https://discourse.julialang.org/t/think-julia-exercise-10-1/50098/9 "2020-11-13T17:14:35Z")

</div>

Another possible solution: Look at this method of the sum function:

```julia
  sum(f, itr)

  Sum the results of calling function f on each element of itr.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> sum(abs2, [2; 3; 4])
  29

 

```

---

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [November 13, 2020, 5:17pm UTC](https://discourse.julialang.org/t/think-julia-exercise-10-1/50098/10 "2020-11-13T17:17:21Z")

</div>

@rafael.guerra I think the OP is asking for help in approaching a Julia programming problem as a learning exercise. So it’s better if we provide guidance without directly providing the answer.

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [November 13, 2020, 5:30pm UTC](https://discourse.julialang.org/t/think-julia-exercise-10-1/50098/11 "2020-11-13T17:30:41Z")

</div>

> [@CameronBieganek](#):
>
> - Use a `for` loop
> - Use broadcasting (dot syntax)
> - Use the [`map`](https://docs.julialang.org/en/v1/base/collections/#Base.map) function

Just to add to this, recursion and multiple dispatch is a really nice way to handle things like this (though rarely the most performant).

E.g. to demonstrate a related example, lets find the largest element of an arbitrarily nested set of arrays:

```julia
julia> mymaximum(iter) = maximum(mymaximum.(iter))
mymaximum (generic function with 1 method)

julia> mymaximum(x::Number) = x
mymaximum (generic function with 2 methods)

julia> mymaximum(t)
8

```

A more performant approach would probably be to use

```julia
mymaximum(iter) = mapreduce(mymaximum, max, iter)

```

but things like `mapreduce` tend to take a little time for new users to understand.

---

<div class="post-metadata">

### Author: ![AKL](https://avatars.discourse-cdn.com/v4/letter/a/ba9def/32.png) [@AKL](https://discourse.julialang.org/u/AKL)
#### Post date: [November 13, 2020, 5:45pm UTC](https://discourse.julialang.org/t/think-julia-exercise-10-1/50098/12 "2020-11-13T17:45:00Z")

</div>

Hi everyone and thank you all for your answers, i’m starting to build an answer, yet i am far from understanding everything you said ^^"

for now this is how it looks like for me :

function nestedsum()

```
for i=1:3
	sum = sum(cumsum(t[i], dims=1))
	println(sum)
end 

```

end

Yet, i am still getting an error code when i try to run the function.  
“MethodError: no method matching nestedsum(::Array{Array{Int64,1},1})”

I think that i have trouble selecting an array in an array of t, like t[1] would be [1,2],… then adding it together

---

<div class="post-metadata">

### Author: ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)
#### Post date: [November 13, 2020, 5:48pm UTC](https://discourse.julialang.org/t/think-julia-exercise-10-1/50098/13 "2020-11-13T17:48:12Z")

</div>

Looks like `nestedsum` is a function that takes no arguments, as in `f()`. You are calling it as in `nestedsum(t)`, which is throwing an error. If you defined it with `t` as an input, it would work.

Note that the use of `cumsum` is not quite correct (I’ll leave that to you to debug). More egregiously though, you are defining `sum` on the left hand side when the right hand side… uses the `sum` function. This is obviously very dire, as on iteration 2 you will get the error “objects of type Int64 are not callable”

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [November 13, 2020, 5:48pm UTC](https://discourse.julialang.org/t/think-julia-exercise-10-1/50098/14 "2020-11-13T17:48:21Z")

</div>

Forget about `sum`, `cumsum` etc. Try to solve that only using loops.

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [November 13, 2020, 5:55pm UTC](https://discourse.julialang.org/t/think-julia-exercise-10-1/50098/15 "2020-11-13T17:55:28Z")

</div>

To expand on the previous poster’s comment, if you can build it with lower-level loops code, then you have the tools to build anything from first principles. Later you can make the code more concise with everything else mentioned above.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [November 13, 2020, 6:02pm UTC](https://discourse.julialang.org/t/think-julia-exercise-10-1/50098/16 "2020-11-13T18:02:30Z")

</div>

The concise options are important to learn but, in this particular case, if one actually faces that problem in real life in a performance-critical code, one should write our own function with loops anyway. It is the fastest approach possible and, if the vectors are large, amenable to vectorization, parallelization, etc. Therefore, writing the loops is not a simple exercise, in my opinion is even the way that problem should be tackled if actually faced in a real code.

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [November 13, 2020, 6:39pm UTC](https://discourse.julialang.org/t/think-julia-exercise-10-1/50098/17 "2020-11-13T18:39:53Z")

</div>

One thing to keep in mind with the `sum` function, is that it’s more accurate than a naive loop if I’m not mistaken.

Anyway, you don’t necessarily have to avoid functional approaches in favor of for-loops, just to avoid allocations and gain performance:

```julia
nestedsum(vec) = sum(sum, vec)

julia> @btime nestedsum($t)
  9.471 ns (0 allocations: 0 bytes)

```

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [November 13, 2020, 9:13pm UTC](https://discourse.julialang.org/t/think-julia-exercise-10-1/50098/18 "2020-11-13T21:13:05Z")

</div>

Just to provoke, the loop version can be loop-vectorized to be 40% faster than that option:

```julia
julia> t = [rand(1:1000,1000), rand(1:1000,500), rand(1:1000,700)]

julia> @btime sum(sum, $t)
  152.859 ns (0 allocations: 0 bytes)
1094847

julia> @btime nestedsum($t)
  93.723 ns (0 allocations: 0 bytes)
1094847

```

But of course, there are functional approaches that are very efficient, and many of which normally one would not know how to write something better. This is not one of such cases, though. And the possibility of writing our custom functions with great performance is what makes Julia great in relation to scripting languages.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [November 13, 2020, 10:53pm UTC](https://discourse.julialang.org/t/think-julia-exercise-10-1/50098/20 "2020-11-13T22:53:02Z")

</div>

Could you please revise your input **t**? As is, the result of `sum(sum,t)` is over 20 million.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [November 13, 2020, 11:21pm UTC](https://discourse.julialang.org/t/think-julia-exercise-10-1/50098/21 "2020-11-13T23:21:01Z")

</div>

> [@rafael.guerra](#):
>
> your input **t**

done, sorry, wrong line copied

[Next page](https://discourse.julialang.org/t/think-julia-exercise-10-1/50098.md?page=2)
