Intro to Scala Lists - Columbus State University

advertisement
Intro to
Scala Lists
Scala Lists are always
immutable.
This means that a list in Scala,
once created, will remain the
same.
It is very easy to create a list in Scala.
You do not have to give the type of
list in Scala.
Scala can infer its type from the
object it is pointing to.
Here are some examples using the Scala interpreter:
Creating Lists
You can create an empty list, by
coding List()
This list contains nothing.
Creating a List of Integers
We define the list integerlist using “val” so we
create an immutable list, which is initialized with
a new List of integers with the values 1, 2, 3, 4
and 5.
Notice the result shows List[Int]. This is because
the list contains integers.
To Print the List of
Integers
Creating a List of Strings
This creates a field named listofstrings,
which is initialized with a new List of strings
with the values A, B, C, and D.
Notice the result shows List[java.lang.String].
This is because you have a list of strings.
Print the List
This creates a field named listofstrings
which is initialized with a new List of
strings with the values A, B, C, and D.
If you want to print the values on a separate
line. You could create a method printOut to loop
through the list .
The method reads in a List of type String.
Then in the for loop declaration, we have defined a new
variable i of inferred type Int. This variable contains the
current value of the list as we loop through until variable i
is equal to length of the list. Thus ending the loop.
Reversing a List
With Scala, you can also reverse the list of strings
and integers by using .reverse.
Reverse the list of integers:
Reverse the list of strings:
Concatenating a List
List has a method named ::: that concatenates a passed
List and the List on which ::: was invoked. Here's how you
use it to combine the integerlist and listofstrings lists:
Notice the result shows List[Any]. This is because you
have a list of both integers and strings. This is a
catchall type for Scala.
Also, you can use the :: operator to add an
element to the beginning of a list.
For example to add “Turtle” to a list of dog
names:
Accessing an Element
from the List
To access an element from the list, you
simply list the index for the element you
want to access.
Scala indexing starts at index 0.
For example to pull element 3 from a list:
Or to pull element 1 from a list:
If you try to access an element out of
range you will throw an exception.
Using the Filter Function
Lists can be manipulated very easily
with functions that you pass into the
list operators.
These functions can be built with very
short syntax.
For example, say we only want the numbers in the list
that are greater than the number 1.
This code filters out the numbers that fail the test “1 >
n”. The “{1>} builds a function that takes a single
number, compares it to 1 and returns true if it is greater
than 1.
So the List returns (2, 4,5).
If we only want the even numbers from the list:
This function takes a parameter “n” and returns "n mod
2 equals 0".
In this case, we specify the parameter name (but we
don't have to specify the type because we've got a list of
integers) and we perform operations on that parameter.
The result of the operation is used to determine if the
element is included in the newly built list.
Using the Map Method with List
Map applies a one-parameter function to every
element of a List, returning a new List.
In this example, we will create a list called “listmap”
and then we will use the map function on the list to
increase each value by 2.
This will not change listmap, but will return the new
values.
The above code adds 2 to each list element and "map" returns the
resulting list.
Also, you can nest map operations:
As you can see the nested map function returns a new list with the elements
in list2 increased by the first value(1) from list1 and then a second new list is
returned with the elements in list2 increased by the second value(2) in list1.
If you want to make changes to certain elements in the list then
you can use filter and map together.
In this code, the filter function pulls out elements in the list
greater than 0. (-3,-2,-1)
The map function then adds 1 to each of those elements
returning the new list(-2,-1,0).
Sorting Lists
In this example we will create a list sorted by the
second character of each element and put the
list in descending order.
The result is a new list in descending order by the
second character of each element. (charAt(1).
Foreach Method
foreach method is similar to a loop in that it will process for
each element in the list.
A list is created with types of fruit followed by how many of each we
have.
We use foreach on the fruit list to process each element printing the
number of fruit, a space and then the name of the fruit.
Another Example using Foreach
Put the elements in a list of names in Upper
Case format.
List Type Matching
Scala has a function match which allows
comparison of an expression against a number of
different types.
The format is similar to the switch statement in
Java.
The following slide is an example using match against a
list to pull out any integer or string types from the list.
This code uses a for loop with variable I which is inferred to be 0. It loops
through the list until the end of list. Each item in the list is matched against
type each case to see if there is a match.
If the list element is an integer then it should match on case b for Integer,
if string then case a for strings and print out the message.
If the element in the list is neither a String or Int then case other message
will be printed.
As you can see Scala Lists
has some very useful
methods available to make
programming with lists
very easy to do.
Download