# Shorthand for \[\[1,2\],\[3,4\]\] .+ 1

**URL:** <https://discourse.julialang.org/t/shorthand-for-1-2-3-4-1/12526>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 20, 2018, 8:29am UTC](https://discourse.julialang.org/t/shorthand-for-1-2-3-4-1/12526 "2018-07-20T08:29:23Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [July 20, 2018, 8:29am UTC](https://discourse.julialang.org/t/shorthand-for-1-2-3-4-1/12526/1 "2018-07-20T08:29:23Z")

</div>

Is there a short alternative for `[[1,2],[3,4]] .+ 1` now that `+(::Vector,::Number)` is deprecated:

```julia
julia> x = [[1,2], [3,4]]
2-element Array{Array{Int64,1},1}:
 [1, 2]
 [3, 4]

julia> x .+ 1
┌ Warning: `a::AbstractArray + b::Number` is deprecated, use `a .+ b` instead.
│ caller = _broadcast_getindex_evalf at broadcast.jl:579 [inlined]
└ @ Core ./broadcast.jl:579
┌ Warning: `a::AbstractArray + b::Number` is deprecated, use `a .+ b` instead.
│ caller = _broadcast_getindex_evalf at broadcast.jl:579 [inlined]
└ @ Core ./broadcast.jl:579
2-element Array{Array{Int64,1},1}:
 [2, 3]
 [4, 5]

julia> (.+).(x,1)
┌ Warning: .+ is no longer a function object, use `broadcast(+, ...)` instead.
│ caller = ip:0x0
└ @ Core :-1
2-element Array{Array{Int64,1},1}:
 [2, 3]
 [4, 5]

julia> x ..+ 1
ERROR: syntax: invalid operator "..+"

julia> x .(.+) 1
ERROR: syntax: space before "." not allowed in "x ."

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 20, 2018, 8:42am UTC](https://discourse.julialang.org/t/shorthand-for-1-2-3-4-1/12526/2 "2018-07-20T08:42:50Z")

</div>

IMO we should really consider `..` as recursive broadcasting. I don’t have a good answer to this other than that feature request.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 20, 2018, 9:10am UTC](https://discourse.julialang.org/t/shorthand-for-1-2-3-4-1/12526/3 "2018-07-20T09:10:14Z")

</div>

OTOH, `..` is really useful as an operator, eg [IntervalSets.jl](https://github.com/JuliaMath/IntervalSets.jl).

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 20, 2018, 9:11am UTC](https://discourse.julialang.org/t/shorthand-for-1-2-3-4-1/12526/4 "2018-07-20T09:11:18Z")

</div>

> [@Tamas\_Papp](#):
>
> OTOH, `..` is really useful as an operator, eg [IntervalSets.jl](https://github.com/JuliaMath/IntervalSets.jl).

Name it whatever, but I find myself wanted recursive behavior in broadcast often so I’m surprised there’s not an easy way to do it.

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [July 20, 2018, 9:47am UTC](https://discourse.julialang.org/t/shorthand-for-1-2-3-4-1/12526/5 "2018-07-20T09:47:17Z")

</div>

Shortest I can think of is

```julia
((x, y) -> x .+ y).([[1,2],[3,4]], 1)

```

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [July 20, 2018, 10:17am UTC](https://discourse.julialang.org/t/shorthand-for-1-2-3-4-1/12526/6 "2018-07-20T10:17:11Z")

</div>

> [@ChrisRackauckas](#):
>
> Name it whatever, but I find myself wanted recursive behavior in broadcast often so I’m surprised there’s not an easy way to do it.

How about this: `broadcast(::Function)` is the broadcasted version of a function:

```julia
fd = broadcast(f)
fd(x,y) === f.(x,y)

```

Then we can do:

```julia
[[1,2],[3,4]] .broadcast(+) 1

```

We could even have `.+` return `broadcast(+)` so that the following works:

```julia
[[1,2],[3,4]] .(.+) 1

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 20, 2018, 11:19am UTC](https://discourse.julialang.org/t/shorthand-for-1-2-3-4-1/12526/7 "2018-07-20T11:19:29Z")

</div>

```julia
julia> +ᵥ(x,y) = x .+ y
+ᵥ (generic function with 1 method)

julia> [[1,2],[3,4]] .+ᵥ 1
2-element Array{Array{Int64,1},1}:
 [2, 3]
 [4, 5]

```

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [July 20, 2018, 2:40pm UTC](https://discourse.julialang.org/t/shorthand-for-1-2-3-4-1/12526/8 "2018-07-20T14:40:43Z")

</div>

I usually end up using one of the following approaches,

```julia
((x, y) -> x .+ y).(x, 1)
map(elem -> elem .+ 1, x)
[elem .+ 1 for elem ∈ x]

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 20, 2018, 3:20pm UTC](https://discourse.julialang.org/t/shorthand-for-1-2-3-4-1/12526/9 "2018-07-20T15:20:07Z")

</div>

If it wasn’t for infix, one could generalize this neatly:

```julia
julia> bc(f) = (args...) -> f.(args...)
bc (generic function with 1 method)

julia> (bc(+)).([[1,2],[3,4]], 1)
2-element Array{Array{Int64,1},1}:
 [2, 3]
 [4, 5]

```

And then `bc(bc(f))`, etc.

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [July 21, 2018, 5:48pm UTC](https://discourse.julialang.org/t/shorthand-for-1-2-3-4-1/12526/10 "2018-07-21T17:48:28Z")

</div>

That’s another example of Julia trying to be perfect. If a similar effort is being exerted to make it simple, Julia can place itself in the Top 5 list in just a few years. Before 0.7, this question wouldn’t have been asked. I mentioned this point [in a previous long thread](https://discourse.julialang.org/t/1-0-annoyances-and-matlab-comparison/9433).

Past century languages allowed `A + 1` by default without complaining. Fortran, Matlab, R, Numpy, APL, Perl, and others were created and used by genius mathematicians and no one, AFAIK, complained about adding a scalar to an array. My comment may seem negative but I love Julia and I try to be honest. These tiny perfections can accumulate and yield a perfect language that no one likes to use. The moral of the story? Simplicity, simplicity, simplicity, and putting the end-user in focus, always choosing the **good approximation** approach.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [July 21, 2018, 8:17pm UTC](https://discourse.julialang.org/t/shorthand-for-1-2-3-4-1/12526/11 "2018-07-21T20:17:13Z")

</div>

It may surprise you (based on your apparently dim view of our decision making process), that the deprecation of `A + 1` was not done just to annoy people and be pedantic, but with careful deliberation for mathematical reasons. Mathematicians tend to like `+` to be associative, which was no longer the case when `A + 1` with Matlab-like behavior was allowed in combination with uniform scaling objects (`I`)—a uniquely Julian and very powerful feature. (It is left as an exercise for the reader to figure out how this causes associativity to fail or to find the issue where this decision was made.) In the showdown between `A + 1` and `A + I` the former loses out because broadcasting behavior for scalars is not really mathematically sound: in generic code `A + 1` should really behave the same as `A + I` instead of like `A + ones(n,n)` because `I` Is the multiplicative identity for arrays, not `ones(n,n)`. The broadcasting behavior of `+` really only makes sense in the old Matlab (et al.) mindset where vectorization is necessary for performance, a view point that is neither mandatory nor even beneficial in Julia.

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [July 21, 2018, 11:30pm UTC](https://discourse.julialang.org/t/shorthand-for-1-2-3-4-1/12526/12 "2018-07-21T23:30:55Z")

</div>

I tend to agree with you that double dot might be too much syntax, and Julia needs to be careful not to introduce too much syntax. But dot broadcasting is a bad example to use.

My R using colleagues _love_ Julias dot broadcasting. It’s so much clearer what is actually happening, and so much more powerful because the math is explicit and correct.

---

<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:** [July 22, 2018, 3:18am UTC](https://discourse.julialang.org/t/shorthand-for-1-2-3-4-1/12526/13 "2018-07-22T03:18:05Z")

</div>

If one needs to nest down many levels you can do

```julia
julia> function bc(f::Function, n::Int)
           if n > 1
               bc(f, n-1)
           elseif n == 1
               (args...) -> f.(args...)
           else
               throw("n must be a positive integer")
           end
       end
bc (generic function with 2 methods)

julia> bc(+, 2).([[1,2],[3,4]], 1)
2-element Array{Array{Int64,1},1}:
 [2, 3]
 [4, 5]

```

ie. the depth you need to broadcast is just specified by `n`. If the recursion is worrisome, perhaps using `foldl` is the way to go.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 22, 2018, 6:41pm UTC](https://discourse.julialang.org/t/shorthand-for-1-2-3-4-1/12526/14 "2018-07-22T18:41:36Z")

</div>

How do you make it automatically go to the leaf?

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [July 22, 2018, 6:54pm UTC](https://discourse.julialang.org/t/shorthand-for-1-2-3-4-1/12526/15 "2018-07-22T18:54:07Z")

</div>

> [@ChrisRackauckas](#):
>
> How do you make it automatically go to the leaf?

Is there a clear definition of “leaf” here?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 22, 2018, 7:00pm UTC](https://discourse.julialang.org/t/shorthand-for-1-2-3-4-1/12526/16 "2018-07-22T19:00:13Z")

</div>

Broadcasting scalar type

---

<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:** [July 22, 2018, 7:41pm UTC](https://discourse.julialang.org/t/shorthand-for-1-2-3-4-1/12526/17 "2018-07-22T19:41:06Z")

</div>

Are you asking how to automatically find the depth needed or are you asking how to work with an array of variable depth (ie. [1, [1,2]])?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 22, 2018, 8:55pm UTC](https://discourse.julialang.org/t/shorthand-for-1-2-3-4-1/12526/18 "2018-07-22T20:55:42Z")

</div>

Automatically find the depth and broadcast at that depth.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [July 22, 2018, 9:03pm UTC](https://discourse.julialang.org/t/shorthand-for-1-2-3-4-1/12526/19 "2018-07-22T21:03:36Z")

</div>

For this to work, we have to assume that `f(x::Int)` doesn’t also have a definition `f(x::Vector)`. Otherwise, we wouldn’t know where to stop, right?

Otherwise we would have to pass a type signature or something. `mean(x)` could work on scalars or arrays.
