DOC PREVIEW
TRINITY CSCI 1320 - Collection Methods

This preview shows page 1-2-3 out of 10 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Collection Methods 9 29 2010 Opening Discussion Do you have any questions about the quiz Solutions to the interclass problem Minute essay comments If you feel confusion you should try to schedule a time to come talk to me Why are array lengths fixed Are there variable length arrays Parametric Types You should notice that when we make an array or a list the type is followed by square brackets These types are parametric So they take type arguments In Scala type parameters are placed in square brackets Using Arrays We can get to the elements in an array by putting an index in parentheses arr 5 This syntax can be used in expressions to read values It can also be used in assignments to store values in the array This is what it means to be mutable Let s look at some examples of this Using Lists You can do direct access on lists but it is inefficient The better method is to use the head and tail methods The elements in a list can t be changed However you can efficiently add new elements at the front of the list Standard Methods There are lots of methods on collections The API can help us see all of them Part of collections Boolean tests contains endsWith isEmpty nonEmpty startsWith Searching drop init last slice splitAt take takeRight indexOf lastIndexOf Other mkString reverse zip zipWithIndex Other Methods If the elements in a list support addition or multiplication you can use the sum and product methods If they are ordered you can do min and max Having sum and length makes averages really easy With min you can even drop a grade easily Higher Order Methods The most powerful methods are ones you can pass functions into exist forall Boolean checks like for math filter partition separate collection based on Boolean map apply function to all the elements reduceLeft apply function moving through collection foldLeft apply function moving through but allows initial value so it can return a different type This is curried Let s Put These Into Action I want to spend the rest of the class time playing with these methods and seeing what we can do with them A String is a collection so you can do these things with a String as well Minute Essay Assume you have an array called numbers Write code to take the average of all the numbers less than 100 in the array This is easily done in two lines No class on Friday Interclass problem Write a function that takes an array of numeric grades determines a letter grade for each and calculates a GPA


View Full Document

TRINITY CSCI 1320 - Collection Methods

Documents in this Course
Functions

Functions

10 pages

Functions

Functions

10 pages

Graphics

Graphics

10 pages

Graphics

Graphics

11 pages

Loops

Loops

4 pages

Loops

Loops

3 pages

Strings

Strings

9 pages

Functions

Functions

10 pages

Loops

Loops

11 pages

Graphics

Graphics

11 pages

Graphics

Graphics

12 pages

Sorting

Sorting

11 pages

Sorting

Sorting

10 pages

Arrays

Arrays

10 pages

Loops

Loops

18 pages

Load more
Download Collection Methods
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Collection Methods and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Collection Methods and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?