C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
First: The isImportant function receives an Int and returns a Boolean. It returns true if the argument size is greater than or equal to 10.
Second: The isNotImportant function uses a simpler syntax. Its return value (Boolean) is determined by the compiler.
Finally: We invoke both functions. We print their result Booleans to the screen with println.
Scala program that uses def, functions
def isImportant(size: Int): Boolean = {
// Return true if size is greater than or equal to 10.
return size >= 10
}
def isNotImportant(size: Int) = !isImportant(size)
// Test the isImportant function.
val result1 = isImportant(10)
val result2 = isImportant(0)
println(result1)
println(result2)
// Test the isNotImportant function.
val result3 = isNotImportant(10)
val result4 = isNotImportant(0)
println(result3)
println(result4)
Output
true
false
false
true
Map: We use the map() function on the List to demonstrate lambdas. This function requires a function argument.
ListFirst: We use the underscore variable (which is the argument passed to the lambda). We multiply that by 2 for the result.
Second: We use the expanded lambda expression syntax. The arrow symbol separates arguments from the return expression.
Tip: Both lambda expressions syntax forms are equivalent. The underscore is just a shortcut.
Scala program that uses lambda expressions
val codes = List(100, 200, 300)
println(codes)
// Multiply all elements by 2.
// ... Place in a new List.
val multiplied = codes map (_ * 2)
println(multiplied)
// Multiply all elements by 3, using a longer syntax form.
val multiplied2 = codes map (x => x * 3)
println(multiplied2)
Output
List(100, 200, 300)
List(200, 400, 600)
List(300, 600, 900)
Lambdas: These too can use an underscore or an explicit argument. A type (like Int) can also be supplied.
Scala program that shows function syntax variations
val numbers = List(10) // One-element list.
// Use no periods or parentheses.
numbers foreach println _
// Use curly brackets.
numbers foreach { println _ }
// Use periods and parentheses.
numbers.foreach(println(_))
// Use expanded lambda syntax.
numbers.foreach(x => println(x))
// Use type in lambda syntax.
numbers.foreach((x: Int) => println(x))
// Use curly brackets.
numbers.foreach({ (x: Int) => println(x) })
Output
10
10
10
10
10
10
Return: An empty "return" statement can be used in a method that returns a Unit type. This means "no value."
Here: The message() def prints a message to the screen based on its argument. It returns Unit—it is a void method.
Scala program that uses Unit return type
// The Unit type is used to indicate "void" or no return value.
def message(value: String): Unit = {
println("Message: " + value)
}
// Call void method.
message("cat")
message("bird")
Output
Message: cat
Message: bird
Note: We can avoid passing an argument when a default value is provided. We can call printSize with 0 or 1 arguments.
Scala program that uses default argument
// Uses a default value of "medium" when no argument provided.
def printSize(size: String = "medium") = println(size)
// Print default size string.
printSize()
// Print specific strings.
printSize("large")
printSize("small")
Output
medium
large
small
Note: The argument type "String*" is not a list. But it can be looped over with a for-loop.
ForScala program that uses variable-length arguments
def addAllLengths(values: String*): Int = {
// Add all string lengths in the string variable argument.
var total = 0
for (v <- values) {
total += v.length()
}
return total
}
// Use variable arguments.
val result = addAllLengths("cat", "bird")
println(result)
Output
7
Quote: The last value parameter of a parameter section may be suffixed by "*". The type of such a repeated parameter inside the method is then the sequence type scala.Seq[T].
Basic Declarations: scala-lang.org