0

I'd like to assign the lesser of two values to a variable. In Ruby I would do something like:

my_var = [value_one, value_two].min

In Swift, of course, I can do this:

var myVar = 0.0
if valueOne < valueTwo {
    myVar = valueOne
} else {
    myVar = valueTwo
}

But, I'm wondering if there is a cleaner, more succinct solution.

Elliot Larson
  • 10,669
  • 5
  • 38
  • 57

1 Answers1

6
var myVar = min(valueOne, valueTwo)

min is a standard library function that takes the lesser of two (or least of several — it's variadic) Comparable values.

rickster
  • 124,678
  • 26
  • 272
  • 326