Lab 8: Introduction to Scala 12:00 PM, Mar 14, 2018

Similar documents
Lecture 21: The Many Hats of Scala: OOP 10:00 AM, Mar 14, 2018

Lab 9: More Sorting Algorithms 12:00 PM, Mar 21, 2018

Lab 4: Imperative & Debugging 12:00 PM, Feb 14, 2018

Lab 7: OCaml 12:00 PM, Oct 22, 2017

Lecture 23: Priority Queues 10:00 AM, Mar 19, 2018

Lab 10: Sockets 12:00 PM, Apr 4, 2018

Lab 2: Object-Oriented Design 12:00 PM, Jan 31, 2018

Lab 10: OCaml sequences, comparators, stable sorting 12:00 PM, Nov 12, 2017

Homework 4: Hash Tables Due: 5:00 PM, Mar 9, 2018

Homework 2: Imperative Due: 5:00 PM, Feb 15, 2019

Homework 6: Heaps Due: 5:00 PM, Apr 9, 2018

Lecture 24: Implementing Heaps 10:00 AM, Mar 21, 2018

Homework 5: Fun with Scala Due: 5:00 PM, Mar 16, 2018

CIS 110: Introduction to Computer Programming. Lecture 2 Decomposition and Static Methods ( 1.4)

CS 2340 Objects and Design - Scala

Scala Style Guide Spring 2018

Lecture 23: Priority Queues, Part 2 10:00 AM, Mar 19, 2018

Using Scala in CS241

Lab 1: Introduction to Java

CS558 Programming Languages

Lecture 8: Iterators and More Mutation

Lab 5: Java IO 12:00 PM, Feb 21, 2018

Overview. Elements of Programming Languages. Objects. Self-Reference

Prelim 1, Solution. CS 2110, 13 March 2018, 7:30 PM Total Question Name Short answer

Good Coding Practices Spring 2018

Week 16: More on Collections and Immutability

CS558 Programming Languages

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Lecture 5: Implementing Lists, Version 1

Lecture 22: Garbage Collection 10:00 AM, Mar 16, 2018

...something useful to do with the JVM.

Lab 1: Setup 12:00 PM, Sep 10, 2017

Lecture 16: HashTables 10:00 AM, Mar 2, 2018

CS162 Week 1. Kyle Dewey. Friday, January 10, 14

COMPUTER SCIENCE IN THE NEWS (TWO YEARS AGO) CS61A Lecture 16 Mutable Data Structures TODAY REVIEW: OOP CLASS DESIGN 7/24/2012

Prelim 1. CS 2110, 13 March 2018, 7:30 PM Total Question Name Short answer

CompSci 220. Programming Methodology 12: Functional Data Structures

Notes from a Short Introductory Lecture on Scala (Based on Programming in Scala, 2nd Ed.)

Lecture 11: In-Place Sorting and Loop Invariants 10:00 AM, Feb 16, 2018

Lecture 8: Summary of Haskell course + Type Level Programming

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

def order(food): food = food.upper() print( Could I have a big + food + please? ) return fresh + food

Lecture 4: Inheritence and Abstract Classes

Kotlin for Android Developers

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Overview. Elements of Programming Languages. Objects. Self-Reference

Homework 6: Higher-Order Procedures Due: 11:59 PM, Oct 16, 2018

Lecture 12: Dynamic Programming Part 1 10:00 AM, Feb 21, 2018

CS61A Lecture 16 Mutable Data Structures. Jom Magrotker UC Berkeley EECS July 16, 2012

CS 5010 Program Design Paradigms Lesson 6.1

CS Problem Solving and Object-Oriented Programming

Stateful Objects. can I withdraw 100 CHF? may vary over the course of the lifetime of the account.

Object-oriented programming

CSCI-GA Final Exam

CS 051 Homework Laboratory #2

G Programming Languages - Fall 2012

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

Lab 6: P.S. it s a stack Due 11:59 pm Monday, Mar 31, 2008

CS152: Programming Languages. Lecture 24 Bounded Polymorphism; Classless OOP. Dan Grossman Spring 2011

CSE 143: Computer Programming II Winter 2019 HW6: AnagramSolver (due Thursday, Feb 28, :30pm)

(Provisional) Lecture 08: List recursion and recursive diagrams 10:00 AM, Sep 22, 2017

the rules The Goal Get all three of your monkeys around the board and into the Banana Grove before anyone else can!

Week - 01 Lecture - 04 Downloading and installing Python

Lecture 23: Priority Queues, Part 2 10:00 AM, Mar 19, 2018

Lecture 7: Lists, Version 2 (with Mutation)

CS/ENGRD 2110 SPRING Lecture 3: Fields, getters and setters, constructors, testing

For this chapter, switch languages in DrRacket to Advanced Student Language.

Homework 6: Higher-Order Procedures Due: 10:00 PM, Oct 17, 2017

CSCI 355 Lab #2 Spring 2007

20 Subclassing and Mutation

CMSC201 Computer Science I for Majors

Week 8: Functions and States

Data Structures (list, dictionary, tuples, sets, strings)

Learner User Interface (LUI): Developing Themed Graphics to Enhance Your e-learning. Kevin Thorn, AutoZone

Readings for This Lecture

Lecture 5. Defining Functions

Object-Oriented Design Lecture 20 CS 3500 Spring 2011 (Pucella) Tuesday, Mar 29, 2011

CS 134 Programming Exercise 9:

ORDERS OF GROWTH, DATA ABSTRACTION 4

Inheritance and Polymorphism in Java

Numerical Conversions Intro to Strings in C/C++ CS 16: Solving Problems with Computers I Lecture #8

Lecture 02, Fall 2018 Friday September 7

Programming Principles

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Lecture 17: Implementing HashTables 10:00 AM, Mar 5, 2018

Lecture 7. Memory in Python

+ Abstract Data Types

Administrivia. Simple data types

(More) Fun with Pointers and Linked Lists! CS 16: Solving Problems with Computers I Lecture #17

Overview. Elements of Programming Languages. Advanced constructs. Motivating inner class example

CS 251 Intermediate Programming Methods and Classes

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

CS 103 Unit 11. Linked Lists. Mark Redekopp

CS 251 Intermediate Programming Methods and More

CS205: Scalable Software Systems

Homework 8: Matrices Due: 11:59 PM, Oct 30, 2018

CMSC201 Computer Science I for Majors

Exercise 6 Multiple Inheritance, Multiple Dispatch and Linearization November 3, 2017

Programming Paradigms

Transcription:

CS18 Integrated Introduction to Computer Science Fisler Lab 8: Introduction to Scala 12:00 PM, Mar 14, 2018 Contents 1 Getting Started 1 2 Vals vs Vars 1 3 Team Rocket s Shenanigans 2 4 Lunch Time! 3 5 Just for Fun: Currying Foods (pun intended) 5 Objectives By the end of this lab, you will know: ˆ mutable and immutable data types in Scala ˆ Scala traits By the end of this lab, you will be able to: ˆ use vals and vars, as well as mutable and immutable data structures ˆ use and combine traits effectively in Scala 1 Getting Started First, copy over our source files: cp /course/cs0180/src/lab08/src/* /course/cs0180/workspace/scalaproject/src/lab08/src cp /course/cs0180/sol/lab08/sol/* /course/cs0180/workspace/scalaproject/sol/lab08/sol 2 Vals vs Vars There are two keywords for declaring variables in Scala: val and var. Val is used for immutable data the kind we studied in CS111, CS 17 and CS 19. The latter is used for mutable data the kind we have focused on in CS 18. That sounds simple enough: use val when declaring a variable whose value will never be reassigned, and use var otherwise.

Scala also has immutable and mutable collections (i.e. Maps, Sets, Lists). You can tell which type of collection you are using by checking if it is in the scala.collection.mutable or scala.collection. immutable package. You should always double-check to see which version you have imported! Mutable collections can be changed (i.e. updated, added to, removed from) in place, meaning that you modify the original copy. Immutable collections cannot be modified in place, meaning that if you wish to change an immutable collection, you need to create a new instance. For example, each time you add an element to an immutable hashmap, the add method returns a new instance of a hashmap, containing all the elements of the old hashmap plus the new element. As you can guess, immutable data structures can become inconvenient if you often modify their data. In your upcoming Search project, you will want to be sure that you use mutable hashmaps so that you can modify your data in place. Should you declare your collection as a val or a var? The answer is found in the following table: Var Val Immutable Collection Yes! This is what you should use when you need to modify a collection. Because it is immutable, changing the contents wont change the original, so it s safer. This is the safest! But won t allow you to change your data. Mutable Collection Don t do this you can already modify the contents of a mutable collection, so you never need to reassign the var. You should instead make this a val or immutable. Sure! Always declare mutable collections as val. Be aware other functions could modify the data! However, this is more space-efficient than an immutable structure. Again: var is reassignable and val is not reassignable. If you are wondering why a mutable collection can be declared as a val then read on! It turns out that when a val refers to a collection, it refers to a location in memory representing that collection. Because it is a val, not a var, the val can never be changed to refer to a different location in memory. However, the contents of that memory the actual elements in the collection are mutable, so the collection can still change! For example, if you declare a mutable hashmap, say val map1, then map1 refers to the hashmap, not the data contained within the hashmap. Because map1 is a val, you cannot assign map1 to refer to any other hashmap. However, because the hashmap itself is a mutable data type, you can change the contents of map1 regardless. Because you can easily update val mutable collections, you should not ever use var mutable collections. 3 Team Rocket s Shenanigans Jessie and James have hijacked the system of a nearby Pokecenter and they want to change the address of all pokeball deliveries to Team Rocket s headquarters! They decide to make a class containing a list of all the pokemons to transfer, but they aren t quite sure whether they should use a val or a var in the class, and also whether the data structure containing the names should be mutable or immutable. Help them work out the pros and cons of the different implementations before they have to resort to mass transporting all the pokeballs with their not-so-trustworthy Meowth balloon (or other slow transit systems)! Task: Go ahead and open our source file, Names.scala. You will be working with this file for this section. Take 1: Val Array Jessie decides to first try putting names in an array: 2

Array("Pidgey", "Piplup") Recall that arrays are mutable. Jessie wants to make a Names class whose constructor takes in a private val array of pokemons to kidnap. Here is the class declaration: class Names(nameList: Array[String]) Task: Fill in the class Names, which contains the method address(name: String). This method should change all instances of name in the array to Team Rocket. Hint: Even though your array is stored as a val, that only means the array itself (i.e. its size and type) cannot change, but the contents of the array can arrays are mutable data structures! Note: We did not use the keywords private or val in this class declaration, because that is the default for Scala! Take 2: Var Immutable List After looking at the pokemon list, James figures that he might want to add more pokemons to his list in the future. He realizes that an immutable List might be better suited for this task than an array! Task: Fill in the class Names2 which takes in a List of names as a var and includes an address method that works with the list of names. Note: Since Jessie and James do not want anyone else to see their list of pokemons and foil their plan, the List of pokemons is declared as a private var in the constructor: class Names2(private var namelist: List[String]) Note: We had to use the private keyword in this class declaration, because if a class parameter in Scala is a var, the default for it is to be public. Hint: Try using map (the higher order procedure)! Take 3: Val Immutable List Jessie and James realize that they don t want just anybody modifying the list of names. Make one more class, Names3 which takes in just a List of names as a val. Task: Since the namelist is now completely immutable, you cannot modify its contents, nor can you reassign it to a modified list. Jessie and James need you to write address to return a new instance of Names3 with the correct internal list. Now their data is completely secure! Task: Why do we now need to return a new instance of Names3, instead of having address return void as it did in the other two implementations? Write this down on a piece of paper. Task: Use NamesTest.scala to test your code: Warning: Although you could technically write Scala programs with only vars and no vals, you should avoid this. Not only will you be penalized in CS 18 if you use var when you could use val instead, but val increases the clarity of your code, because you dont need to worry about all those variables values changing when you dont want them to. You ve reached a checkpoint! Please call over a lab TA to review your work. 4 Lunch Time! It is lunch time and Brock is cooking up a storm with his new collection of yummy sandwiches! However, with Ash s growing pokemon family, there are just too many mouths to feed. In this problem, you will use stackable modifications to help Brock make sandwiches. That is, you will write multiple traits, and then you will mix them in to make a variety of sandwiches. 3

Task: In a new Scala file, define a class Lunch with a single field ingredients, represented as a list of Strings. Task: Within the Lunch class, define the makesandwich method, which just returns ingredients. Now, you can make some delicious, albeit simple, sandwiches for lunch, like this: scala> val dessert = new Lunch(List("Marshmallows", "Nutella", "Apples")) scala> dessert.makesandwich List("Marshmallows", "Nutella", "Apples") Next, you will define a few traits that you will mix in to your Lunch class to augment the functionality of makesandwich. Task: Write the following traits, each of which extends Lunch and overrides the makesandwich method as described: ˆ PeanutButter: returns all the ingredients, with "Peanut Butter" prepended. ˆ Bread: returns the ingredients with "Bread" added to both ends. ˆ HoldTheAnchovies: returns the ingredients, without any "Anchovies". ˆ DoubleIngredients: doubles every ingredient, such that each appears twice in a row. Hint: You can (and should!) define all these traits in the same Lunch.scala file! Hint: To help get you started, if one of our requirements was TinySandwich, which removes the first half of the ingredients provided, we would have: trait TinySandwich extends Lunch { override def makesandwich = super.makesandwich.drop(super. makesandwich.length / 2) } Hint: As always, you should strive to produce elegant and concise programs. To that end, consider using higher-order functions to implement some of these traits, if appropriate. By mixing in these traits, you can make still more delicious sandwiches, like this one: scala> val lunch = new Lunch(List("Jelly")) with PeanutButter with Bread scala> lunch.makesandwich List("Bread", "Peanut Butter", "Jelly", "Bread") Although we use traits to model interfaces in Scala, traits are richer than Java interfaces. In Java, a class can implement multiple interfaces, and no conflict arises if two of those interfaces declare identical methods with identical signatures because interfaces do not (generally) include implementations. In Scala, however, traits do (generally) include implementations. Scala resolves any conflicting method definitions by linearizing them: the rightmost mixin receives the initial method call. Moreover, each time a trait is mixed in, it overrides any existing methods with conflicting type signatures. Hint: All your traits should override makesandwich. To mix in more than one of them, your makesandwich methods should call super.makesandwich. Hint: Here s how you can fold in Scala: 4

mylist.foldright(list[string]())((curr, rest) => /*something with curr and rest*/)) On that line, the (List[String]()) is the base case for fold, and curr is referring to the current element you re folding over, whereas rest is the result of the rest of the fold! Task: Now it s your turn to make some sandwiches! Here s how. First, in the same Lunch.scala file, create a Lunch object, containing a val List("Anchovies", "Cream Cheese", "Pickles"). Then, create new Lunches that take in this list, mixing in various sequences of the traits you just defined, to create the following delicious sandwiches: ˆ List("Bread", "Anchovies", "Cream Cheese", "Pickles", "Bread") ˆ List("Bread", "Anchovies", "Anchovies", "Cream Cheese", "Cream Cheese", "Pickles ", "Pickles", "Bread") ˆ List("Bread", "Bread", "Cream Cheese", "Cream Cheese", "Pickles", "Pickles ", "Bread", "Bread") ˆ List("Peanut Butter", "Cream Cheese", "Pickles") ˆ List("Bread", "Peanut Butter", "Peanut Butter", "Cream Cheese", "Cream Cheese ", "Pickles", "Pickles", "Bread") Note: Put these sandwiches in a companion object to your Lunch class. Name the sandwiches monday, tuesday,... friday, respectively, so that a call to monday.makesandwich returns List("Bread", " Anchovies", "Cream Cheese", "Pickles", "Bread"), and so on. Here s an example to get you started: object Lunch { val mylist = List("Anchovies", "Cream Cheese", "Pickles") val monday = new Lunch(myList) with??? } Task: Using our LunchTest file, test your implementation with the days of the week. Then, write at least 2 more sandwiches below the weekday Lunches (and corresponding tests)! You ve reached a checkpoint! Please call over a lab TA to review your work. 5 Just for Fun: Currying Foods (pun intended) Ash, Misty and Brock are frustrated, because their regular favorite meals just don t taste the same anymore! They refuse to eat any food item that has an inadequate tastiness ranking. They realize they need an automated way of sorting through foods based on their tastiness ranking, so they can still eat a good hearty meal. A Scala program should do it! We represent a food using the following class: class Foods(val name: String, val tastiness: Int) We have written a function called goodfood, which takes a list of Foods and outputs a list of all the Foods that have more than 20 tastiness. Task: Look at our implementation of this function. What would we need to do to our code if our heroes became very hungry and had lower standards for the tastiness of the food? Discuss this with your partner and write it down on a piece of paper. 5

The three realize that as they grow older, their food needs may change over time, but they don t want to have to rewrite the code each time that happens. In comes currying! 1 In Scala, a function can be defined with multiple parameter lists: def addn(n: Int)(x: Int) = x + n val add2 = addn(2)_ println(add2(3)) // prints 5 If you call the curried function with only the first parameter list, it will return another function that takes in the second parameter list. Task: Create a new file with a class in it called Currying. It should not take in any arguments in the constructor. Task: Write a curried function mintastiness, which takes in a number n and a list of Food, and returns a list of all its elements with at least n tastiness. Task: Now, curry mintastiness into a new function, tastinessover20, which takes a list of Food and returns a list of all its elements with at least 20 tastiness. Task: Write a curried function sumtastiness, which takes a number n and a list of Food. If the total tastiness in the list is at least n, it should return the original list. Otherwise, return a new (empty) Food list. Task: Curry sumtastiness into a new function, sumtastinessover50, which takes a list of Food and returns the list if the sum total tastiness is at least 50, and an empty list otherwise. Hint: Your curried functions (mintastiness and sumtastiness) should look very similar to the function you looked at earlier that used hard-coded values. Hint: To put our above example in the language of this exercise, add is a curried function that takes two numbers and outputs their sum. add is then curried into add2, which takes a number and adds 2 to it. Note: As always, be sure to test all the functions you have written! Once a lab TA signs off on your work, you ve finished the lab! Congratulations! Before you leave, make sure both partners have access to the code you ve just written. Please let us know if you find any mistakes, inconsistencies, or confusing language in this or any other CS18 document by filling out the anonymous feedback form: https://cs.brown.edu/courses/cs018/ feedback. 1 http://docs.scala-lang.org/tutorials/tour/currying.html 6