Scala is a programming language meant to address scalability issues that confront a large number of developers today. It is a statically typed language, meaning that it binds a type to a variable for the variable’s lifetime, whereas dynamically typed languages bind the type to the value referenced by a variable. Simply, statically typed variables are immutable, whereas dynamically typed variables are mutable. Scala also supports both functional and object-oriented programming. It seems that these attributes would conflict with one another, but the argument is that both attributes in conjunction lead to synergies in both performance and architecture.
Let’s get started on the introduction.
1 2 3 4
val is used to declare a read-only variable named
book. In the next exercise, let’s create a class that will take a number of parameters and then upcase them. We’ll also refactor this function to make it more efficient and readable. Let’s do it in the space below:
1 2 3 4 5
We’ve defined the class
Upper, and specified its parameter
strings as an undefined number of string values, evidenced by the splat in
String*. Then we specify the return type
Seq[String] as a collection (or array) of strings. Then inside of the method’s body, where we call the
map method on the strings array, and passed in
(s:String) => s.toUpperCase() as a function literal. In other words, it executes that function for each mapped parameter of the array
Now let’s initialize the Upper Case Function below:
1 2 3
upper method in the
Upper class converts each parameter into an uppercase string, and returns them in an array.
Now let’s refactor.
1 2 3 4 5
We’ve now declared an
object, which in Scala is a singleton. In this situation, we only ever need one instance of Upper run at a time, so a singleton works appropriately. The
_ element inside of the
map method is a placeholder variable that each string element will be assigned to before
toUpperCase() is applied.