Here is how I can check if a number is even/odd in Ruby:
def even_or_odd(number)
['Even', 'Odd'][number % 2]
end
The closest I have in Scala is the following:
def evenOrOdd(number: Int): String = {
val a = Array("Even", "Odd")
a(number % 2)
}
What I really want to do is something like this, but it won't compile:
def evenOrOdd(number: Int): String = {
("Even", "Odd")(number % 2)
}
Firstly, what is this type of 'anonymous' structure called? Secondly, what is an elegant way to use it? If you have a similar example that illustrates the power/conciseness/clarity of Scala, I'd like to see it.