Tutorial on Writing Modular Programs in Scala



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

Analyse et Conception Formelle. Lesson 5. Crash Course on Scala

A Scala Tutorial for Java programmers

Fundamentals of Java Programming

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Scala Programming Language

Aspect-Oriented Programming

Moving from CS 61A Scheme to CS 61B Java

An Overview of the Scala Programming Language

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Software Engineering Techniques

Java Interview Questions and Answers

Java (12 Weeks) Introduction to Java Programming Language

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

The C Programming Language course syllabus associate level

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Java Programming Fundamentals

History OOP languages Year Language 1967 Simula Smalltalk

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

CPLEX Tutorial Handout

Java CPD (I) Frans Coenen Department of Computer Science

Java SE 8 Programming

AP Computer Science Java Subset

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

Introduction to Java

Crash Course in Java

CS 111 Classes I 1. Software Organization View to this point:

Chapter 1 Java Program Design and Development

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Java Programming Language

Konzepte objektorientierter Programmierung

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

3 Pillars of Object-oriented Programming. Industrial Programming Systems Programming & Scripting. Extending the Example.

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

AP Computer Science A - Syllabus Overview of AP Computer Science A Computer Facilities

Comp 411 Principles of Programming Languages Lecture 34 Semantics of OO Languages. Corky Cartwright Swarat Chaudhuri November 30, 20111

Java Application Developer Certificate Program Competencies

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

core. Volume I - Fundamentals Seventh Edition Sun Microsystems Press A Prentice Hall Title ULB Darmstadt

LAB4 Making Classes and Objects

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Inheritance, overloading and overriding

CSC 551: Web Programming. Spring 2004

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

Programming Languages CIS 443

Chapter 1 Fundamentals of Java Programming

Computer Programming I

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

CS 106 Introduction to Computer Science I

How Scala Improved Our Java

The Sun Certified Associate for the Java Platform, Standard Edition, Exam Version 1.0

Glossary of Object Oriented Terms

How To Use The Command Pattern In Java.Com (Programming) To Create A Program That Is Atomic And Is Not A Command Pattern (Programmer)

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Computer Programming I & II*

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

Getting Started with the Internet Communications Engine

Programmation 2. Introduction à la programmation Java

Object-Oriented Software Specification in Programming Language Design and Implementation

Getting t Ge o K tting t no o K w Scala no for fo r Da D t a a t a Sc S i c e i n e c n e

TypeScript for C# developers. Making JavaScript manageable

Android Application Development Course Program

CS193j, Stanford Handout #10 OOP 3

Object-Oriented Programming in Java

#820 Computer Programming 1A

Syllabus for CS 134 Java Programming

Basic Programming and PC Skills: Basic Programming and PC Skills:

S C A L A & D E S I G N P A T T E R N S

C# programming. Introduction. By Per Laursen

Habanero Extreme Scale Software Research Project

Java EE Web Development Course Program

Java Programming. Binnur Kurt Istanbul Technical University Computer Engineering Department. Java Programming. Version 0.0.

Lecture 7 Notes: Object-Oriented Programming (OOP) and Inheritance

TECHNOLOGY Computer Programming II Grade: 9-12 Standard 2: Technology and Society Interaction

A Case Study in DSL Development

The Scala Language Specification Version 2.9

Characteristics of Java (Optional) Y. Daniel Liang Supplement for Introduction to Java Programming

J a v a Quiz (Unit 3, Test 0 Practice)

Introducing Variance into the Java Programming Language DRAFT

Java Classes. GEEN163 Introduction to Computer Programming

Description of Class Mutation Mutation Operators for Java

Java Crash Course Part I

Lecture 12: Abstract data types

Introduction to Object-Oriented Programming

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

C Compiler Targeting the Java Virtual Machine

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

6.170 Tutorial 3 - Ruby Basics

CS170 Lab 11 Abstract Data Types & Objects

CEC225 COURSE COMPACT

Unit-testing with JML

Kotlin for Android Developers

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Transcription:

Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 13 September 2006 Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 1 of 45

Welcome to the Scala tutorial at JMLC 2006 A half-day tutorial on the Scala programming language. A rapid, no-frills, presentation of Scala as a language for writing modular programs. For Java or related programmers. Be advised, you will work too: this is an interactive hands-on tutorial: get your computer ready! Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 2 of 45

Higher-order This afternoon s plan 1 Meeting Scala 2 3 4 5 Higher-order Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 3 of 45

Higher-order Scala as a Java clone Pushing the envelope Scala vs. Java At first glance, Scala is similar to Java (or C ). or rather everything Java has to offer can be found in Scala. Scala is object-oriented, statically typed, throws exceptions, etc. Scala s syntax will look familiar to Java programmers. Scala compiles to Java bytecode: it runs on any JVM. Scala even shares Java s libraries: all classes and methods defined as a Java libraries are transparently accessible from Scala code. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 4 of 45

Higher-order Scala as a Java clone Pushing the envelope The two following classes have the same behaviour. In Java: class PrintOptions { } public static void main(string[] args) { } System.out.println("Opts selected:"); for (int i = 0; i < args.length; i++) if (args[i].startswith("-")) System.out.println( " "+args[i].substring(1)); You might notice some similarities. In Scala: class PrintOptions { } def main(args: Array[String]: Unit) = { } System.out.println("Opts selected:") for (val arg <- args) if (arg.startswith("-")) System.out.println( " "+arg.substring(1)) Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 5 of 45

Higher-order Scala as a Java clone Pushing the envelope Basic differences This first section will describe some basic differences between Scala and Java you need to be aware of. These include syntactic differences, different class member definitions, a purely object model and differences in the class model. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 6 of 45

Syntactic differences Meeting Scala Higher-order Scala as a Java clone Pushing the envelope Scala uses an identifier-colon-type notation for member or parameter definitions. int age (String name) becomes def age (name: String): Int Semi-colons are optional. There is no pre-defined syntax for for loops. Comprehensions are provided instead. Blocks such as {...} are required only to group statements. Single expressions can be defined outside a block. def x = if (p) a else b is a legal declaration. All definitions can be arbitrarily nested. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 7 of 45

Higher-order Scala as a Java clone Pushing the envelope Everything is an object All native types (int, double, bool) are classes, define methods etc., but they are not passed as references, they are subclasses of AnyVal (as opposed to other classes that extend AnyRef). Arrays are objects and array-specific syntax does not exist (c.f. API). The void pseudo-type is replaced by the Unit class. Instances of Unit can be created with (). Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 8 of 45

A new class hierarchy Meeting Scala Higher-order Scala as a Java clone Pushing the envelope scala.any scala.anyval scala.scalaobject scala.anyref (java.lang.object) scala.double scala.unit scala.float scala.boolean scala.seq java.lang.string scala.long scala.char scala.list (other Java classes) scala.int scala.option scala.short (other Scala classes) scala.byte scala.null scala.nothing Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 9 of 45

Higher-order Scala as a Java clone Pushing the envelope The object value Objects can even be created as such, without defining a class. object Scala extends Language { val creator = LAMP } Objects replace the singleton pattern, There are no static members, instead objects can be created directly. An object with the same name as a class is called companion module and can access private class members. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 10 of 45

Higher-order Scala as a Java clone Pushing the envelope Richer class members Java only allows fields and methods in classes. Scala has a richer semantic for class members. def defines a method. Parameters are allowed, but optional. def f(a: Int): String or def f: String are legal definitions. val defines a constant value. A value can also override a (non parameterized) def. This is required for writing functional (i.e. invariant) classes. var defines a variable, like a Java field. object and class are legal members in a class. type members also exist but will not be covered in this tutorial. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 11 of 45

Higher-order Scala as a Java clone Pushing the envelope Class body as constructor In Java, constructors are special (smalltalk-ish) methods. Scala has a different approach. The class or object body is executed at instance creation. Class declarations have parameters. class Human (soul: Soul) { } soul.insufflate(creator.getlife) val me = new Human(new Soul) This is the primary constructor, others can be defined as def this (...) =... Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 12 of 45

Higher-order Scala as a Java clone Pushing the envelope You will now test some of the described concepts yourself. Installing and running Scala. Creating classes and objects. Using constructors. And generally getting familiar with the Scala syntax. The accompanying documentation describes your task in more detail. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 13 of 45

Higher-order Case classes Matching on case classes Class hierarchies as ADT OO languages use class hierarchies for representing data types. Content is encapsulated in the object and accessed through methods. Algebraic data types are a common concept in functional languages. Data is accessed through decomposing the value by pattern matching. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 14 of 45

Higher-order Case classes Matching on case classes ADT and class hierarchies have complementary strength and weaknesses. ADTs allow easy extension of operations supported by the data while class hierarches allow easy addition of data variants. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 15 of 45

Higher-order Case classes Matching on case classes Scala case classes ADTs can be encoded using case classes. Case classes are like normal classes. Instance constructors can be recovered by pattern matching. Structural equality is used for comparison. The new keyword is optional for instance creation. case class ClockTime (hour: Int, min: Int) is a valid case class definition. ClockTime(10,30) creates an instance. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 16 of 45

Higher-order Case classes Matching on case classes Scala s pattern matching A case class can be decomposed using a match construct, like the following. time match { case ClockTime(hour, min) =>... case SwatchTime(beats) =>... case Sunset =>... case Sunrise =>... } All lower-case identifiers in the pattern will bind the decomposed value and are available on the right-hand side of the pattern. Order is important: a first-match policy is used. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 17 of 45

Higher-order Case classes Matching on case classes Constant values can be used in patterns to restrict matching. case ClockTime(10, min) will only match any time in the 10th hour (and bind minutes to min). case "ten o clock" will match the ten o clock string. A name starting with a capital letter will also be treated as a constant. case ClockTime(Ten, min) will behave as above if Ten == 10. Richer conditions can be defined with guards. case ClockTime(hour, min) if hour > min is a guard. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 18 of 45

Higher-order Case classes Matching on case classes When no pattern matches a value, the match statement throws a MatchError. A default case can be added, using the wildcard pattern. case _ will match any value. Wildcards can also be used as a component of a pattern. case SwatchTime(_) will match any time defined in Swatch-beat time. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 19 of 45

Higher-order Case classes Matching on case classes without case classes Scala s patterns even extend to non-case classes. case x: String will bind x (of type string) to any value of type string. Of course, deconstruction isn t available on type patterns. Instead, this is a rich way to do type casts or type tests. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 20 of 45

Higher-order Case classes Matching on case classes You will now test some of the described concepts yourself. Matching on case-class ADTs. Matching on values. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 21 of 45

Higher-order for modularisation Scala supports lightweight syntax for anonymous functions. (x: Int) => x + 1 defines a successor function on integers. are first-class values, and can be stored or passed. val succ = (x: Int) => x + 1 succ(44) applies the successor function an returns 45. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 22 of 45

Higher-order for modularisation Lifting functions A method can easily be transformed into a function by not providing it with its parameters, and by flagging it with a &. class Number (value: Int) { def add (other: Number) =... } Can be used as a function value in unrelated code val addone = &new Number(1).add Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 23 of 45

as objects Meeting Scala Higher-order for modularisation As mentioned earlier, Scala is purely object-oriented. Since functions are values, they must be objects too. A function is instance of class Function0 or Function1 or... There exists one function class for all number of parameters. A class (or object) implementing FunctionX must define an apply method with the correct number of parameters. object addone extends Function1[Int, Int] { } def apply(num: Int): Int = num + 1 addone(23) will return 24. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 24 of 45

The type of function Meeting Scala Higher-order for modularisation A shorthand syntax for writing the type of functions also exists. Function0[Int] becomes () => Int Function1[String, Person] becomes String => Person Function2[Int, Int, Int] becomes (Int, Int) => Int Int => Int => Int is Function1[Int, Function1[Int, Int]] Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 25 of 45

Higher-order for modularisation Taking advantage of function A functional programming style offers real benefits for modular programs. A module can be parameterized by function, not only by state. can be passed from module to module. Scala s functions-as-objects allow an easy integration of functions in a traditional OO environment. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 26 of 45

Higher-order for modularisation Functional programming style Writing in functional style can be difficult for seasoned OO programmers. Behaviour is no longer attached to an object but moves freely. State becomes less important: there are no methods depending on it. Immutable objects become natural: why deal with state when a function can simply return a new object? In other words, use state sparingly in Scala, functions and immutable objects (think val) help structure messy code. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 27 of 45

Higher-order for modularisation You will now test some of the described concepts yourself. Use functions as values. Define anonymous functions. Turn a class into a function. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 28 of 45

Higher-order Mixin inheritance Using traits Modular systems with single inheritance In single class inheritance languages merging behaviours of different classes (or modules) is tricky, adaptor code is required, which make the relation brittle. Often, module reengineering is required. Java s interfaces provide some help, but are clearly insufficient. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 29 of 45

Higher-order Mixin inheritance Using traits Full multiple inheritance is often too complex to be of great use. Scala provides mixins as a compromise. A class can inherit from multiple traits. A trait is a special kind of class which implements some behaviour. There must be a common parent class with the inherited mixin. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 30 of 45

Mix-ins? Meeting Scala Higher-order Mixin inheritance Using traits Vehicle Vehicle Car Flying Diving Car JamesBondsCar Diving JamesBondsCar Flying Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 31 of 45

Higher-order Mixin inheritance Using traits Traits A trait is defined like a class, but using the trait keyword instead. trait Flying extends Vehicle { } def takeoff =... // concrete def land: Unit // abstract All members inherited from Vehicle can be used: this trait will eventually be mixed-in with a class extending Vehicle. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 32 of 45

Higher-order Mixin inheritance Using traits Inheriting traits A trait can be inherited when defining a class class JamesBondsCar extends Car with Flying with Diving or when creating an instance. val jbscar = new Car with Flying with Diving When a class only extends mixins, it will automatically also extend AnyRef. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 33 of 45

Higher-order Mixin inheritance Using traits Requiring a behaviour When multiple traits are inherited they can refer to members of their common super class, but not to members of other mixed-in traits. A trait can require another class or trait; it can only be mixed-in when the requirement is available. trait Reading extends Person requires Seeing Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 34 of 45

Higher-order Mixin inheritance Using traits You will now test some of the described concepts yourself. Define traits for mixin. Mixin these traits into instances to inherit behaviour. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 35 of 45

Higher-order Defining higher-order function Higher-order functions on lists Higher-order functions A higher-order function is a function (or a method) that takes another function as parameter. def order( data: List[Thing], lessthan: (Thing, Thing) => Boolean ) =... This method orders a list of things. But since order on things is not well-defined, order is parameterized as a function. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 36 of 45

Higher-order Defining higher-order function Higher-order functions on lists There is nothing more to it. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 37 of 45

Higher-order Defining higher-order function Higher-order functions on lists Higher-order functions on lists But the real deal with higher-order functions is their use in lists (and other container structures). Lists are the most common container structures. For every element of the list do... is a natural task. This requires the definition of the operation to apply on each element. Which means: higher-order function. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 38 of 45

Higher-order Defining higher-order function Higher-order functions on lists Map, Flat map and Filter The scala list class defines a number of usefull higher-order functions. For a list of type A def map[b](f: (A) => B): List[B] will apply f to all elements of the list, and return the resulting new list. def flatmap[b](f: (A) => List[B]): List[B] will apply f to all elements of the list, and concatenate all resulting lists into one flat list. def filter(p: (A) => Boolean): List[A] will return a new list containing only those elements that are true for predicate p. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 39 of 45

Higher-order Defining higher-order function Higher-order functions on lists For-comprehensions Java s for loops are replaced in Scala with comprehensions. A comprehension will loop on all elements of a list. for (val e <- List(1,2,3)) print(e) prints 123 A comprehension can return a new list. for (val e <- List(1,2,3)) yield e * 2 returns List(2,4,6). A comprehension can filter its elements. for (val e <- List(1,2,3); e.iseven) yield e returns List(2). A comprehension can have multiple loops. for (val e1 <- List(1,2); val e2 <- List(2,3)) yield e1 * e2 returns List(2,3,4,6). Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 40 of 45

Higher-order Defining higher-order function Higher-order functions on lists For-comprehensions are entirely made out of higher-order functions on lists. A for-comprehension for { val i <- 1 to n val j <- 1 to i isprime(i+j) } yield Pair(i, j) and the code it gets turned into. (1 to n).flatmap { case i => (1 to i).filter { j => isprime(i+j) }.map { case j => Pair(i, j) } } Which means they can be used on any class that supports map, flatmap and filter. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 41 of 45

Higher-order Defining higher-order function Higher-order functions on lists You will now test some of the described concepts yourself. Using for-loops or other higher-order functions on lists. As this is the last hands-on section, we will also reuse everything else we discussed in a grand finale. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 42 of 45

To conclude: the Scala tutorial at JMLC 2006 We hope you did enjoy this tutorial. We would like you to be able to take back the following. A good idea as to how pattern matching, mixins and first-class functions can improve the modularity of an OO language. A feel for the kind of programming that Scala permits, and how useful it is. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 43 of 45

To conclude: Scala and modular programs Scala is a fairly complex language with many features. But this complexity can be put to good use because it allows modularising more in a safer way and in a more reusable way. Scala s seamless and complete integration of functional and OOP features is the key to its success. Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 44 of 45

Get Scala for yourself Scala is open source and available free of charge. For downloads, example code, libraries, discussions, etc., visit the Scala website at http://scala.epfl.ch Tutorial on Writing Modular Programs in Scala Martin Odersky and Gilles Dubochet 45 of 45