Learn Scala - More Functions

Question Click to View Answer

What does the following code print?

def triple(x: Int): Int = x * 3
val tripleCopy: (Int) => Int = triple
println(tripleCopy(5))

15 is printed.

The triple() function is defined to take an integer as input and return an integer. The tripleCopy() function is then assigned to the triple function. Scala is a functional language, so functions can be treated like any other value.

The following syntax will not work:

val tripleCopy = triple

Scala requires that the function inputs and outputs are explicitly defined.

Once tripleCopy() is defined, it is invoked with the argument 5.

What does the following code print?

def quadruple(x: Int): Int = x * 4
val quadrupleCopy = quadruple _
println(quadrupleCopy(5))

20 is printed.

This placeholder syntax replaces named parameters with the wildcard operator (_). This is equivalent to the following explicit syntax:

def quadruple(x: Int): Int = x * 4
val quadrupleCopy: (Int) => Int = quadruple
println(quadrupleCopy(5))

What does the following code print?

def m(x: Int, y: Int): Int = {
  if (x > y) x else y
}
val max: (Int, Int) => Int = m
println(max(88, 99))

99 is printed.

The m function takes two integer inputs and returns an integer. When the max function is assigned to m, the inputs and outputs are specified to differentiate the syntax from function invocation.

What does the following code print? Explain the syntax.

var fullName = (first: String, last: String) => {
  s"$first $last"
}
println(fullName("bob", "loblaw"))

bob loblaw is printed.

The variable fullName is assigned to an anonymous function (aka function literal). Functions in Scala don't need to have a name.

This syntax also works.

((first: String, last: String) => {
  s"$first $last"
})("bob", "loblaw")

What does the following code print? Explain the syntax.

var min = (a: Int, b: Int) => {
  if (a > b) b else a
}
println(min(78, 44))

44 is printed.

The variable min is assigned to an anonymous function. The anonymous function is invoked and returns the smallest integer.

What does the following code print? Explain the syntax.

val hi = () => "howdy!"
println(hi())

howdy! is printed.

The hi value is assigned to an anonymous function that does not take any input arguments and returns a string. The empty parenthesis () are used to indicate the lack of an input parameter.

What does the following code print? Explain the syntax.

def sad = "meow"
val catCry = sad
println(catCry())

This code throws an error. The catCry method can be assigned to the sad function with the wildcard operator:

def sad = "meow"
val catCry = sad _
println(catCry())

What does the following code print? Explain the syntax.

def play(thing: String): String = {
  s"Let's play with $thing"
}
def funify(thing: String, f: String => String): String = {
  f(thing) + " and have fun"
}
println(funify("cats", play))

Let's play with cats and have fun is printed.

The play function takes a string as an input and returns a string. The funify function takes a string and a function as inputs. funify is referred to a higher order function because it takes another function as an argument.

Higher order functions are a critical part of functional programming and we'll see more of them.