I'm going through a few functional programming languages, learning things of interest, and I'm looking at Scala now. What I'm trying to do is figure out the simplest way to write a function called double
which take one argument and doubles it. What I've come up with so far is:
def double = (x:Int) => x*2
or
def double(x:Int) = x*2
This works, but I'm looking for the simplest way. In Haskell, I could simply do this:
double = (*2)
Because it's a partially applied function, there's no need to name the variable or specify any types (I'm sure the *
function takes care of that). Is there a similar way to do this using Scala? I've tried a few, especially using _
instead of x
, but none seemed to work.