intro to scala lists. scala lists are always immutable. this means that a list in scala, once...

26
Intro to Scala Lists

Upload: meredith-council

Post on 30-Mar-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same

Intro toScala Lists

Page 2: Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same

Scala Lists are always immutable.  

This means that a list in Scala, once created, will remain the same.

Page 3: 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. 

Page 4: Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same

Here are some examples using the Scala interpreter:

Creating ListsYou can create an empty list, by coding List()

This list contains nothing.

Page 5: Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same

Creating a List of IntegersWe 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.

Page 6: Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same

To Print the List of Integers

Page 7: Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same

This creates a field named listofstrings, which is initialized with a new List of strings with the  values A, B, C, and D.

Creating a List of Strings

Notice the result shows List[java.lang.String]. This is because you have a list of strings.

Page 8: Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same

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. 

Page 9: Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same

 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. 

Page 10: Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same

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:

Page 11: Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same

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.

Page 12: Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same

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:

Page 13: Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same

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. 

Page 14: Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same

Or to pull element 1 from a list:

If you try to access an element out of range you will throw an exception.

For example to pull element 3 from a list:

Page 15: Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same

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. 

Page 16: Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same

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).

For example, say we only want the numbers in the list that are greater than the number 1.

Page 17: Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same

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.

Page 18: Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same

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. 

Page 19: Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same

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.

Page 20: Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same

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).  

Page 21: Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same

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).  

Page 22: Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same

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. 

Page 23: Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same

Another Example using Foreach

Put the elements in a list of names in Upper Case format.

Page 24: Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same

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.

Page 25: Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same

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.  

Page 26: Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same

As you can see Scala Lists has some very useful 

methods available to make programming with lists 

very easy to do.