Announcements. CSCI 334: Principles of Programming Languages. Lecture 16: Intro to Scala. Announcements. Squeak demo. Instructor: Dan Barowy

Similar documents
n n Official Scala website n Scala API n

CS 2340 Objects and Design - Scala

...something useful to do with the JVM.

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

Announcements. CSCI 334: Principles of Programming Languages. Thanks! Lecture 23: Exam Review. Announcements. Announcements

Functional programming

Scala : an LLVM-targeted Scala compiler

Functions, Closures and Control Abstraction

CS205: Scalable Software Systems

INTRODUCTION. SHORT INTRODUCTION TO SCALA INGEGNERIA DEL SOFTWARE Università degli Studi di Padova

Announcements. CSCI 334: Principles of Programming Languages. Lecture 5: Fundamentals III & ML

Bibliography. Analyse et Conception Formelle. Lesson 5. Crash Course on Scala. Scala in a nutshell. Outline

Programs as Data. The Scala language

The Pyth Language. Administrivia

Some Scala problems for self

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

G Programming Languages - Fall 2012

SECOND EDITION SOURCE CODE ONLINE

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

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Imperative Programming 2: Traits

CS205: Scalable Software Systems

Object-oriented programming

Axiomatic Rules. Lecture 18: Axiomatic Semantics & Type Safety. Correctness using Axioms & Rules. Axiomatic Rules. Steps in Proof

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

Accelerating Information Technology Innovation

Homework 7. What To Turn In. Reading. Self Check. Handout 18 CSCI 334: Spring, 2017

Scala, Your Next Programming Language

CS Introduction to Computational and Data Science. Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Control Structures. Christopher Simpkins CS 3693, Fall Chris Simpkins (Georgia Tech) CS 3693 Scala / 1

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

CSE341: Programming Languages Lecture 11 Type Inference. Dan Grossman Spring 2016

Scala Scripting. Scala By the Bay, San Francisco, 12 Nov 2016

CompSci 220. Programming Methodology 12: Functional Data Structures

Using Scala in CS241

Practically Functional. Daniel Spiewak

First Programming Language in CS Education The Arguments for Scala

Scala. ~ a tragedy in two parts ~ the tragedy being we aren t using it all the time

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides

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

Linked lists and the List class

High-level Parallel Programming: Scala on the JVM

Ruby: Introduction, Basics

Introduction to Python

An Overview of Scala. Philipp Haller, EPFL. (Lots of things taken from Martin Odersky's Scala talks)

.consulting.solutions.partnership. Clojure by Example. A practical introduction to Clojure on the JVM

Session Scala. Multiparty Session Programming with Scala, Scribble and AMQP.

Classical Themes of Computer Science

The DOT Calculus. (Dependent Object Types) Nada Amin. May 12, flatmap(oslo)

List are immutable Lists have recursive structure Lists are homogeneous

Kotlin, Start? Start! (pluu) Android Developer GDG Korea Android Organizer

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15

CSE 130 [Winter 2014] Programming Languages

CSCI-GA Scripting Languages

The SCAlable LAnguage

Swift. Introducing swift. Thomas Woodfin

An introduction introduction to functional functional programming programming using usin Haskell

Life in a Post-Functional World

Clojure. The Revenge of Data. by Vjeran Marcinko Kapsch CarrierCom

The DOT Calculus. (Dependent Object Types) Nada Amin. June 18, Scala Days

GETTING STARTED WITH Kotlin INTRODUCTION WHERE TO START CODING BASIC SYNTAX

CSE 130, Fall 2006: Final Examination

Beyond Blocks: Python Session #1

Appendix A. Scala Tools. A.1 Command Line

Scala. Fernando Medeiros Tomás Paim

Scala in Martin Odersky

Outline. Collections Arrays vs. Lists Functions using Arrays/Lists Higher order functions using Arrays/Lists

Accelerating Information Technology Innovation

Lecture 7. Memory in Python

Lecture 8. Conditionals & Control Flow

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

SWIFT! init(title: String) { self.title = title } // required initializer w/ named parameter

Advanced Object-Oriented Languages Scala

CS Introduction to Computational and Data Science. Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017

Company Predecessor Domain Concepts

The fringe of a binary tree are the values in left-to-right order. For example, the fringe of the following tree:

Type Systems. Today. 1. Organizational Matters. 1. Organizational Matters. Lecture 1 Oct. 20th, 2004 Sebastian Maneth. 1. Organizational Matters

Introduction to Scala: Objects, Classes, and Traits

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

Introduction to Python

CIS192 Python Programming. Robert Rand. August 27, 2015

Ruby: Introduction, Basics

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

CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Dan Grossman Fall 2011

CIS192: Python Programming

The Kotlin Programming Language. Andrey Breslav

CSCI-GA Final Exam

Introduction to Python: Data types. HORT Lecture 8 Instructor: Kranthi Varala

CS153: Compilers Lecture 14: Type Checking

CS Programming Languages: Scala

Modern Web Development with Kotlin

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

SSEA Computer Science: CS106A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Fall 2011

CSE 341, Spring 2011, Final Examination 9 June Please do not turn the page until everyone is ready.

CSCI 2041: Functions, Mutation, and Arrays

Lecture 15 CIS 341: COMPILERS

LECTURE 1. Getting Started with Python

Object-oriented programming in...

Transcription:

Announcements CSCI 334: Principles of Programming Languages Lecture 16: Intro to Scala HW7 sent out as promised. See course webpage. Instructor: Dan Barowy Announcements No class on Tuesday, April 17. Squeak demo

The Programming World Today Scala! Tower of Babel OO vs Functional Tradeoff REPL Operation Doctor Nurse Orderly Print Print Doctor Print Nurse Print Orderly Pay Pay Doctor Pay Nurse Pay Orderly Functional programming makes it easy to add operations. OO programming makes it easy to add data. Scala: Why not have both functional and OO? $ scala Welcome to Scala 2.12.5 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_144). Type in expressions for evaluation. Or try :help. scala> scala> "hello world!" res0: String = hello world! scala> :quit

Semicolons are optional Scala is object-oriented scala> println("hello world!") Hello world! scala> println("hello world!"); Hello world! scala> class Apple defined class Apple scala> val a = new Apple a: Apple = Apple@31b7d869 Everything is an object! Scala is functional Scala is functional Supports many of your favorite HOFs (and then some!) scala> val xs = List(1,2,3,4,5) scala> xs.map(e => e + 1) res0: List[Int] = List(2, 3, 4, 5, 6) scala> xs.map(_ + 1) res1: List[Int] = List(2, 3, 4, 5, 6) scala> xs.foldleft (0)((acc,x) => acc + x) res0: Int = 15 scala> xs.zip(xs) res1: List[(Int, Int)] = List((1,1), (2,2), (3,3), (4,4), (5,5)) scala> val m = xs.groupby(x => x > 3) m: scala.collection.immutable.map[boolean,list[int]] = Map(false -> List(1, 2, 3), true -> List(4, 5)) scala> m(false) res2: List[Int] = List(1, 2, 3) scala> m(true) res3: List[Int] = List(4, 5) scala> m(true).head res4: Int = 4

Scala is functional But Scala is also pragmatic Values are immutable You can also use mutable variables scala> class Thing { val i = 1 def increment() { i += 1 <console>:13: error: value += is not a member of Int Expression does not convert to assignment because receiver is not assignable. def increment() { i += 1 ^ scala> class Thing { var i = 1 def increment() { i += 1 defined class Thing scala> val t = new Thing t: Thing = Thing@28d728f1 scala> t.increment scala> t.i res0: Int = 2 Scala has great documentation Ordinary Functions scala> def succ(x: Int) = x + 1; succ: (x: Int)Int scala> succ(12); res0: Int = 13

Lambda (Anonymous) Functions Recursive Functions scala> val succ = (x : Int) => x + 1; succ: Int => Int = $$Lambda$1514/322302398@2fe12b04 scala> succ(3) res0: Int = 4 scala> def fact(n: Int) : Int = if (n == 0) 1 else n * fact(n-1) fact: (n: Int)Int scala> fact(4) res0: Int = 24 Scala is built on top of Java Scala has a rich set of built-in types In general, Java classes and methods are available. scala> val sb = new StringBuilder sb: StringBuilder = scala> sb.append("hello") res0: StringBuilder = hello scala> sb.append("world") res1: StringBuilder = helloworld scala> println(sb.tostring) helloworld

Scala has a rich set of built-in types Most types fully compatible with Java scala> true res0: Boolean = true scala> false res1: Boolean = false scala> 3 res2: Int = 3 scala> 43.3 res3: Double = 43.3 scala> "moo" res8: java.lang.string = moo scala> val str = "cow" str: java.lang.string = cow scala> str.length() res9: Int = 3 scala> str.touppercase() res10: java.lang.string = COW Lightweight tuple syntax (like SML!) You can abbrev. no-param calls scala> (1,"hello") res0: (Int, String) = (1,hello) scala> str.length() res9: Int = 3 scala> str.touppercase() res10: java.lang.string = COW scala> str.touppercase res11: java.lang.string = COW scala> str touppercase res12: java.lang.string = COW

Scala has pattern matching Scala has generics scala> val thing : Option[Int] = Some(3) thing: Option[Int] = Some(3) scala> thing match { case None => println("it was nothing") case Some(i) => println(i) 3 scala> def foo[t](data: T) { println(data) foo: [T](data: T)Unit scala> foo(1) 1 scala> foo("hello") hello scala> foo((1,"hello")) (1,hello) Scala has lighter syntax than Java Scala programs can just be scripts No need for boilerplate. Also supports traditional structure Scala programs can also be compiled just like Java println( Helloworld! ) $ scala hello.scala Helloworld! object App { def main(args: Array[String]) { println("helloworld!") $ scalac hello.scala $ scala App Helloworld! hello.scala

Scala doesn t care where you put classes Doesn t have Java s restrictive one class per file rule class Apple { def whatami = "apple" object App { def main(args: Array[String]){ val apple = new Apple println(apple.whatami) $ scalac cool.scala $ scala App apple Scala doesn t care where you put classes You can even nest classes arbitrarily class Apple { def whatami = "apple" object App { class Orange { def whatami = "orange" def main(args: Array[String]){ val apple = new Apple val orange = new Orange println(apple.whatami + " " + orange.whatami) $ scalac hello.scala $ scala App apple orange Scala has powerful facilities for abstraction Anonymous classes trait Fruit { def name: String trait Box { def fruit: Fruit def contains(afruit: Fruit) = fruit == afruit trait Color { def color: String scala> val apple = new Apple apple: Apple = Apple@4f8659d0 scala> val ab = new Box { def fruit = apple ab: Box{def fruit: Apple = $anon$1@1c011855 class Apple extends Fruit { def name = "Apple" class AppleBox(apple: Apple) extends Box with Color { def fruit = apple def color = brown

We can even refine types We can even refine types F must be a subtype of Fruit trait Box[F <: Fruit] { def fruit: F def contains(afruit: Fruit) = fruit == afruit F must be a subtype of Fruit trait Box[F <: Fruit] { def fruit: F def contains(afruit: Fruit) = fruit == afruit But now this doesn t work. Why? val box: Box[Fruit] = new Box[Apple] { def fruit = apple Box is not covariant What we want: F <: Fruit Box[F] <: Box[Fruit] This is not true in Scala by default (but the fix is simple) trait Box[+F <: Fruit] { def fruit: F def contains(afruit: Fruit) = fruit == afruit val box: Box[Fruit] = new Box[Apple] { def fruit = apple