I will continue exploring more features in Scala here.
1. Parameterize Array with types:
val big = new java.math.BigInteger("12345")
$ cat 6_arrays.scala
val greetScala = new Array[String](3)
greetScala(0) = "Hi "
greetScala(1) = args(0) + ", "
greetScala(2) = "Welcome to Scala!\n"
for(str <- greetScala)
$ scala 6_arrays.scala Rasesh
Hi Rasesh, Welcome to Scala!
Use of parenthesis makes a call to method apply of the first object passing values in the parenthesis as arguments to method called apply.
In the previous example, greetScala(0) is translated to greetScala.apply(0). It is true for any object that has method called apply.
Also, Scala has no operators in traditional sense. Its just functions names +,-,/,*. It seems weird if you are not familiar with functional programming but this is how its done in most languages like Lisp and Scheme.
1+2 is a call to method called ‘+’ and its passed 2 as argument. Its same as
res0: Int = 3
res1: Int = 3
Similarly, when used on left side of ‘=’, parenthesis are replaced by a call to update method as in
greetScala(0) = "Hi " is equivalent to
greetScala.update(0, "Hi ")
Note: Everything in Scala, from arrays to expressions are objects with methods.
Initialize an array without specifying datatype:
val numNames = Array("zero", "one", "two")
This is similar to calling Array.apply(“zero”,”one”,”two”). We can assume apply to be a static function in Array class.
2. Using Lists:
List are immutable elements as in functional programming languages Lisp, Scheme etc. It means once created list elements do not change. It is similar to String class in Java.
scala> val oneTwo=List(1,2)
oneTwo: List[Int] = List(1, 2)
scala> val numStr=List(1,"str")
numStr: List[Any] = List(1, str)
List has a method ‘:::‘ for list concatenation. As it is a method, it can also be used with dot operator as mentioned previously (a catch here, explained after :: operator) and explained in example below:
scala> val concatenated = oneTwo ::: numStr
concatenated: List[Any] = List(1, 2, 1, str)
List(1, 2, 1, str)
scala> val con2=(oneTwo).:::(numStr)
con2: List[Any] = List(1, str, 1, 2)
Original List oneTwo is not mutated.
Another operator on list is ‘::‘ which has exactly the same purpose as ‘cons’ in Scheme, if you are familiar with it. It considers the left element as the first element of the new list appended by the list on the right side of the operator.
scala> val newList = 0 :: oneTwo
newList: List[Int] = List(0, 1, 2)
scala> val nestedList = oneTwo :: numStr
nestedList: List[Any] = List(List(1, 2), 1, str)
res2: List[Any] = List(List(1, str), 1, 2)
The output of
oneTwo :: numStr should be same as
oneTwo.::(numStr) as per our understanding so far. But, we see something else here. In Scala, when a method name ends with a ‘:’, the calling object is on the right side and the passed object in the one on left. This means
oneTwo :: numStr is equivalent to
‘Nil’ or List() is used to specify empty list which is equivalent of ‘() or empty in Scheme. To create a list 1,2,3 in Scheme, one can write (cons 1 (cons 2 (cons 3 ‘()))). Similarly, in Scala, you can write
scala> val oneTwoThree = 1 :: 2 :: 3 :: Nil
oneTwoThree: List[Int] = List(1, 2, 3)
This is just an introduction to list. There are many more list operations supported by Scala.
3. Using Tuples:
Tuple is a combination of multiple values of any data type similar to ‘pair’ in C++ STL but not limited to 2 values. In Java, you need to create a class of 2 variables to return multiple values from a method, but it can be done using tuples in Scala.
Tuples are immutable like List.
scala> val pair = (1, "Hi", 4.5)
pair: (Int, java.lang.String) = (1,Hi,4.5)
First element is referred by _1 element and second one by _2 and so on.
Further exploration of language in next posts.
<= Previous Post Next Post =>