# Quick 'for' and 'if' one-line loop

**URL:** https://discourse.julialang.org/t/quick-for-and-if-one-line-loop/43199
**Category:** New to Julia
**Tags:** question
**Created:** [July 16, 2020, 10:03pm UTC](https://discourse.julialang.org/t/quick-for-and-if-one-line-loop/43199 "2020-07-16T22:03:41Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![1634](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1634/32/12119_2.png) [@1634](https://discourse.julialang.org/u/1634)
#### Post date: [July 16, 2020, 10:03pm UTC](https://discourse.julialang.org/t/quick-for-and-if-one-line-loop/43199/1 "2020-07-16T22:03:41Z")

</div>

Hi,  
For some very simple function like +=1 if the element of this array is 0, is there any concise way?

```julia
c=[0,1,0]
map(x->x=0, c) #but I wish to add a **if** in this one-line for loop

#otherwise how verbose my usual solution is:
function plusone(c) #empirically saying, need to make it a function for outputing
#the changed variable to main macro (instead of losing) 
    for i in 1:length(c)
        if i == 0
            c[i] = 1
        end
        return c #output make no dif
    end
end

```

---

<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: [July 16, 2020, 10:15pm UTC](https://discourse.julialang.org/t/quick-for-and-if-one-line-loop/43199/2 "2020-07-16T22:15:29Z")

</div>

In general, you can use `&&` as an if condition as in `i==0 && c[i]+=1` (`||` as well)  
In this case, you could also do

```julia
for i in 1:length(c)
    c[i] += (c[i] == 0)
end

```

It should also be noted that the function is not the same as the original `map`. `map` does not modify `c` in place. You could do `map!(x -> x + (x==0), c, c)` for the same effect though.

It is almost always possible to condense short for loops and if conditions into a single line, but it’s not often worth it IMO.

---

<div class="post-metadata">

### Author: ![JonasIsensee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonasisensee/32/4704_2.png) [@JonasIsensee](https://discourse.julialang.org/u/JonasIsensee)
#### Post date: [July 16, 2020, 10:16pm UTC](https://discourse.julialang.org/t/quick-for-and-if-one-line-loop/43199/3 "2020-07-16T22:16:17Z")

</div>

`map(x-> x==0 ? 1 : x, c) `

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [July 16, 2020, 10:22pm UTC](https://discourse.julialang.org/t/quick-for-and-if-one-line-loop/43199/4 "2020-07-16T22:22:22Z")

</div>

Your code example does not match your question.

I will use your written question, which is to replace zeros with ones. In that case you should use the `replace!` function:

```julia
replace!(c, 0=>1)

```

---

<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 16, 2020, 11:38pm UTC](https://discourse.julialang.org/t/quick-for-and-if-one-line-loop/43199/5 "2020-07-16T23:38:52Z")

</div>

A generic approach

```julia
c .= ifelse.(c .== 0, 1, c)

```

---

<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: [July 17, 2020, 12:36am UTC](https://discourse.julialang.org/t/quick-for-and-if-one-line-loop/43199/6 "2020-07-17T00:36:31Z")

</div>

This has the downside of making several copies.

---

<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 17, 2020, 12:44am UTC](https://discourse.julialang.org/t/quick-for-and-if-one-line-loop/43199/7 "2020-07-17T00:44:12Z")

</div>

If you’re referring to the `c .= ...` solution [above](https://discourse.julialang.org/t/quick-for-and-if-one-line-loop/43199/5), that works in-place with no copies or temporary arrays.

---

<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: [July 17, 2020, 12:45am UTC](https://discourse.julialang.org/t/quick-for-and-if-one-line-loop/43199/8 "2020-07-17T00:45:23Z")

</div>

Yeah, I tried to delete, but messed up since I’m on a phone atm

---

<div class="post-metadata">

### Author: ![1634](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1634/32/12119_2.png) [@1634](https://discourse.julialang.org/u/1634)
#### Post date: [July 17, 2020, 9:57am UTC](https://discourse.julialang.org/t/quick-for-and-if-one-line-loop/43199/9 "2020-07-17T09:57:27Z")

</div>

#Comparison

```julia
using BenchmarkTools
c=[0,1,0]
@btime map!(x -> x + (x==0), c, c) #25.426 ns (0 allocations: 0 bytes)##perform the best
c=[0,1,0]
@btime map(x-> x==0 ? 1 : x, c) #288.844 ns (2 allocations: 128 bytes)
c=[0,1,0]
@btime c .= ifelse.(c .== 0, 1, c) # 774.766 ns (4 allocations: 96 bytes)

```

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [July 17, 2020, 10:55am UTC](https://discourse.julialang.org/t/quick-for-and-if-one-line-loop/43199/10 "2020-07-17T10:55:27Z")

</div>

Due to the small array and the very low timings these results are misleading:

```julia
julia> c=rand(Bool,100000);

julia> @benchmark map!(x -> x + (x==0), $c, $c)
BenchmarkTools.Trial:
  memory estimate: 0 bytes
  allocs estimate: 0
  --------------
  minimum time: 78.399 μs (0.00% GC)
  median time: 78.600 μs (0.00% GC)
  mean time: 84.175 μs (0.00% GC)
  maximum time: 284.300 μs (0.00% GC)
  --------------
  samples: 10000
  evals/sample: 1

julia> @benchmark map(x-> x==0 ? 1 : x, $c)
BenchmarkTools.Trial:
  memory estimate: 97.78 KiB
  allocs estimate: 3
  --------------
  minimum time: 91.101 μs (0.00% GC)
  median time: 116.000 μs (0.00% GC)
  mean time: 119.357 μs (3.91% GC)
  maximum time: 6.092 ms (95.87% GC)
  --------------
  samples: 10000
  evals/sample: 1

julia> @benchmark c .= ifelse.($c .== 0, 1, $c)
BenchmarkTools.Trial:
  memory estimate: 96 bytes
  allocs estimate: 4
  --------------
  minimum time: 90.599 μs (0.00% GC)
  median time: 90.700 μs (0.00% GC)
  mean time: 92.172 μs (0.00% GC)
  maximum time: 300.601 μs (0.00% GC)
  --------------
  samples: 10000
  evals/sample: 1

```

Still the first variant is best.

But what you actually do is:

```julia
julia> @benchmark trues(100000)
BenchmarkTools.Trial:
  memory estimate: 12.41 KiB
  allocs estimate: 2
  --------------
  minimum time: 460.000 ns (0.00% GC)
  median time: 1.710 μs (0.00% GC)
  mean time: 2.379 μs (14.36% GC)
  maximum time: 180.490 μs (98.80% GC)
  --------------
  samples: 10000
  evals/sample: 10

```

or in our case

```julia
julia> trues(3)
3-element BitArray{1}:
 1
 1
 1

```

I guess its only a MWE for something more complex 🙂

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [July 17, 2020, 10:58am UTC](https://discourse.julialang.org/t/quick-for-and-if-one-line-loop/43199/11 "2020-07-17T10:58:35Z")

</div>

I think you also need to interpolate the global variable into the benchmarking expression:

```julia
julia> c=rand(0:1, 100_000);

julia> @btime map!(x -> x + (x==0), $c, $c);
50.999 μs (0 allocations: 0 bytes)

julia> @btime map(x-> x==0 ? 1 : x, $c);
73.101 μs (3 allocations: 781.34 KiB)

julia> @btime $c .= ifelse.($c .== 0, 1, $c);
51.399 μs (0 allocations: 0 bytes)                

```

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [July 17, 2020, 11:01am UTC](https://discourse.julialang.org/t/quick-for-and-if-one-line-loop/43199/12 "2020-07-17T11:01:34Z")

</div>

Didn’t I? oh, I forgot one. Timings don’t change on my PC.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [July 17, 2020, 11:03am UTC](https://discourse.julialang.org/t/quick-for-and-if-one-line-loop/43199/13 "2020-07-17T11:03:17Z")

</div>

Sorry I actually missed that you did - I copied @1634’s example, and saw large differences between interpolated/not interpolated.

---

<div class="post-metadata">

### Author: ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)
#### Post date: [July 17, 2020, 6:14pm UTC](https://discourse.julialang.org/t/quick-for-and-if-one-line-loop/43199/14 "2020-07-17T18:14:15Z")

</div>

Why in second test case `map` is used instead of `map!`? It looks like different things are compared.

---

<div class="post-metadata">

### Author: ![1634](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1634/32/12119_2.png) [@1634](https://discourse.julialang.org/u/1634)
#### Post date: [July 17, 2020, 7:47pm UTC](https://discourse.julialang.org/t/quick-for-and-if-one-line-loop/43199/15 "2020-07-17T19:47:46Z")

</div>

What’s the purpose of having interpolation of global variable into the benchmark? Is this for mimicing the indexing of variable in a function or macro?

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [July 18, 2020, 10:51am UTC](https://discourse.julialang.org/t/quick-for-and-if-one-line-loop/43199/16 "2020-07-18T10:51:20Z")

</div>

[https://github.com/JuliaCI/BenchmarkTools.jl/blob/master/doc/manual.md#interpolating-values-into-benchmark-expressions](https://github.com/JuliaCI/BenchmarkTools.jl/blob/master/doc/manual.md#interpolating-values-into-benchmark-expressions)

---

<div class="post-metadata">

### Author: ![adamwillisMastery](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adamwillismastery/32/32240_2.png) [@adamwillisMastery](https://discourse.julialang.org/u/adamwillisMastery)
#### Post date: [March 9, 2022, 1:10am UTC](https://discourse.julialang.org/t/quick-for-and-if-one-line-loop/43199/17 "2022-03-09T01:10:53Z")

</div>

Thanks @nilshg for sharing the link. Unfortunately the link’s broken. Here’s a new one:  
[BenchmarkTools Manual, Sub-Heading:Interpolating values into benchmark expressions](https://github.com/JuliaCI/BenchmarkTools.jl/blob/4009a7a713d59f358f6f059b979a8c16fe58d9fc/docs/src/manual.md)
