# Purpose of reduce(Array, Array)

**URL:** <https://discourse.julialang.org/t/purpose-of-reduce-array-array/39294>\
**Category:** General Usage\
**Tags:** question, function\
**Created:** [May 11, 2020, 4:20pm UTC](https://discourse.julialang.org/t/purpose-of-reduce-array-array/39294 "2020-05-11T16:20:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bpou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bpou/32/14320_2.png) [@bpou](https://discourse.julialang.org/u/bpou)\
**Post date:** [May 11, 2020, 4:20pm UTC](https://discourse.julialang.org/t/purpose-of-reduce-array-array/39294/1 "2020-05-11T16:20:53Z")

</div>

Hello,  
I’m wondering what the purpose of `reduce` is when given two arrays as only arguments. Here’s what I’ve observed:

> julia\> reduce([1], [4])  
> 4  
> julia\> reduce([1, 2, 3], [4])  
> 4  
> julia\> reduce([1, 2, 3], [1])  
> 1  
> julia\> reduce([1, 2, 3], [4, 5])  
> ERROR: MethodError: objects of type Array{Int64,1} are not callable  
> Use square brackets for indexing an Array.  
> Stacktrace:  
> [1] \_mapreduce(::typeof(identity), ::Array{Int64,1}, ::IndexLinear, ::Array{Int64,1}) at ./reduce.jl:311  
> [2] \_mapreduce\_dim at ./reducedim.jl:305 [inlined]  
> [3] #mapreduce#538 at ./reducedim.jl:301 [inlined]  
> [4] mapreduce at ./reducedim.jl:301 [inlined]  
> [5] #reduce#539 at ./reducedim.jl:345 [inlined]  
> [6] reduce(::Array{Int64,1}, ::Array{Int64,1}) at ./reducedim.jl:345  
> [7] top-level scope at none:0

It seems to me that, given a singleton Array as the second argument, it returns the value in this array, otherwise, it throws a (rather uninformative) error. Apparently, the first argument doesn’t matter at all. However, I’ve been unable to find any documentation of this behavior.

If anyone can explain this, I would be most grateful.

PS. Upon further experimentation, it seems to throw an error if the first argument is empty. Also, it seems to have inherited this behavior from `foldl`. I’m still confused.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [May 11, 2020, 4:32pm UTC](https://discourse.julialang.org/t/purpose-of-reduce-array-array/39294/2 "2020-05-11T16:32:02Z")

</div>

Reduce takes two arguments — a function `f` and a collection `A`. The function `f` must take two arguments, and then `reduce` goes through the collection and one-element-at-a-time it updates `result = f(result, elt)`. So if there are two elements in `A`, the result is:

```nohighlight
f(A[1], A[2])

```

If there are three elements in `A`, the result is:

```nohighlight
result = f(A[1], A[2])
result = f(result, A[3])

```

And so on.

But what happens if there’s only one element in `A`? Well, you can’t call `f` at all because it might not have an applicable one-argument method defined! Thus the function argument is ignored completely and we just take a shot in the dark and return the one element itself. If there are no elements in `A`, then there’s nothing we can do but error. In neither case if `f` even attempted to be called, and thus it can be anything at all.

---

<div class="post-metadata">

**Author:** ![bpou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bpou/32/14320_2.png) [@bpou](https://discourse.julialang.org/u/bpou)\
**Post date:** [May 11, 2020, 4:37pm UTC](https://discourse.julialang.org/t/purpose-of-reduce-array-array/39294/3 "2020-05-11T16:37:56Z")

</div>

Thank you, I think that makes sense. However, I don’t see why this is permitted in the first place. I don’t see why anyone would want to do this. Wouldn’t it be better to give `reduce` a type that disallowed this usage?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [May 11, 2020, 4:44pm UTC](https://discourse.julialang.org/t/purpose-of-reduce-array-array/39294/4 "2020-05-11T16:44:12Z")

</div>

In general Julia code is not strictly typed. The reason for this is that it lets other people use your functions with their types, which sometimes produces really unexpected and useful results. An example are using numbers that track uncertainty with `DifferentialEquations`. No one designed for it to work, but by not strictly typing, it let someone try putting the two together and getting state of the art results.

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [May 11, 2020, 10:06pm UTC](https://discourse.julialang.org/t/purpose-of-reduce-array-array/39294/5 "2020-05-11T22:06:35Z")

</div>

> [@bpou](#):
>
> Wouldn’t it be better to give `reduce` a type that disallowed this usage?

It would, but any type can be made callable, so that it’s impossible to restrict the type of a callable argument in a generic way.

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [May 12, 2020, 3:45am UTC](https://discourse.julialang.org/t/purpose-of-reduce-array-array/39294/6 "2020-05-12T03:45:10Z")

</div>

I also agree that this behaviour is quite confusing - it would make sense to raise an error when no `init` is given and the array has only one element. Currently it is not even type-stable, if my understanding is correct: the return type of `reduce` for a singleton array is the `eltype` of that array, but for a longer array it the the return type of `f`. I got caught by this once or twice, but mostly use `reduce` with `init` anyway.
