# Coding pattern for "min with side effects"

**URL:** https://discourse.julialang.org/t/coding-pattern-for-min-with-side-effects/76207
**Category:** Offtopic
**Created:** [February 11, 2022, 3:58am UTC](https://discourse.julialang.org/t/coding-pattern-for-min-with-side-effects/76207 "2022-02-11T03:58:11Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)
#### Post date: [February 11, 2022, 3:58am UTC](https://discourse.julialang.org/t/coding-pattern-for-min-with-side-effects/76207/1 "2022-02-11T03:58:11Z")

</div>

Many algorithms require computing `min(a,b)` and then executing some side effects depending on whether `a < b`. I’m struggling to come up with a good coding pattern for this type of logic.

The problem is best described by means of an example. Consider the following function for finding the smallest element in a vector `v`.

```julia
function minimum(v)
   @assert !isempty(v)
    x = v[1]
    for i = 2:length(v)
       x = min(x, v[i])
    end
    return x
end

```

If in addition to finding the minimum we also want to find the index of the minimum, then this function has to be changed as follows.

```julia
function findmin(v)
   @assert !isempty(v)
    x = v[1]
    j = 1
    for i = 2:length(v)
        if v[i] < x
            x = v[i]
            j = i
        end
    end
    return x,j
end

```

My beef with this type of code is that `v[i]` occurs both in the `if` statement and the line just after the `if` statement. I believe this inefficient (repeated index lookups can’t be optimised by the compiler AFAIK), and if the statement is more complicated than simply `v[i]`, then repeating the same complicated statement is a likely source of typos. Of course, these problems could be avoided by introducing a temporary variable, but temporaries are often ugly and make the code harder to read. So my question is, is there a way to write this logic without repeating `v[i]` and without introducing a temporary?

* * *

I’m aware that this is a very OCD type of question and that in the real world you would just do whatever works and move on. But I thought it could be instructive and fun to hear what other people think about this.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [February 11, 2022, 4:16am UTC](https://discourse.julialang.org/t/coding-pattern-for-min-with-side-effects/76207/2 "2022-02-11T04:16:16Z")

</div>

you have a few options but no clear winner if you only use `v[i]` twice…

1. make `vi = v[i]` immediate after `for i = 2:length(v)`.
2. use `enumerate`

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [February 16, 2022, 6:57am UTC](https://discourse.julialang.org/t/coding-pattern-for-min-with-side-effects/76207/3 "2022-02-16T06:57:20Z")

</div>

```julia
function minimum(v)
    m = Inf
    i = 0
    for (k, e) in enumerate(v)
        if e ≥ m
            continue
        end
        m = e
        i = k
    end
    return (m, i)
end

```

This saves you linear indexing at all, which is good if `v` is, for instance, not an array.

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [February 16, 2022, 7:08am UTC](https://discourse.julialang.org/t/coding-pattern-for-min-with-side-effects/76207/4 "2022-02-16T07:08:44Z")

</div>

This is not, however, guaranteed to be typestable (if the array is empty or all its elements are `>Inf` it will return a `Float64`, otherwise `eltype(v)`). Possibly a better way is to use

```julia
((i, m), it) = Iterators.peel(enumerate(v)) # not sure this nested destructuring works
for (k, e) in it
    ...

```
