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

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
NETLOGO LISTS Or… James says, fput THIS!. What are LISTS? Shopping list Address list DNA sequences Any collection of similar or dissimilar things Often.
ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
Ruby (on Rails) CSE 190M, Spring 2009 Week 2. Arrays Similar to PHP, Ruby arrays… – Are indexed by zero-based integer values – Store an assortment of.
CSCI 6962: Server-side Design and Programming Input Validation and Error Handling.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
Arrays, A1 COMP 401, Fall 2014 Lecture 4 8/28/2014.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
1 Arrays b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
Lists in Python.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Arrays BCIS 3680 Enterprise Programming. Overview 2  Array terminology  Creating arrays  Declaring and instantiating an array  Assigning value to.
If statements while loop for loop
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program.
Roles of Variables with Examples in Python ® © 2014 Project Lead The Way, Inc.Computer Science and Software Engineering.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
Slide 1 PHP Arrays and User Defined Functions ITWA133.
6.2 For…Next Loops General Form of a For…Next Loop
Operations on Strings. 8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Chapter 8: Loops, Arrays, Strings Loop statements –do –while –for Arrays –declaration, allocation, initialization, access –multi-dimensional –heterogeneous.
Chapter 11.  Large amounts of data are often stored in a database—an organized collection of data.  A database management system (DBMS) provides mechanisms.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
© 2010 Lawrenceville Press Slide 1 Chapter 5 The Do…Loop Statement  Loop structure that executes a set of statements as long as a condition is true. 
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
String and Lists Dr. José M. Reyes Álamo.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Containers and Lists CIS 40 – Introduction to Programming in Python
Racket CSC270 Pepper major portions credited to
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Object Oriented Programming in java
String and Lists Dr. José M. Reyes Álamo.
Logical Operations In Matlab.
CS2011 Introduction to Programming I Arrays (I)
Topics Sequences Introduction to Lists List Slicing
By Andrew Horn And Ryan Kluck
15-110: Principles of Computing
Topics Basic String Operations String Slicing
Topics Sequences Introduction to Lists List Slicing
Topics Basic String Operations String Slicing
Arrays.
Topics Basic String Operations String Slicing
Presentation transcript:

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

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.

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.

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:

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.

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.

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.