Introduction to Scala

Similar documents
Programming in Scala Second Edition

G Programming Languages - Fall 2012

Scala : an LLVM-targeted Scala compiler

Programming Kotlin. Familiarize yourself with all of Kotlin s features with this in-depth guide. Stephen Samuel Stefan Bocutiu BIRMINGHAM - MUMBAI

Scala. Fernando Medeiros Tomás Paim

Imperative Programming 2: Traits

Scala, Your Next Programming Language

Lecture 09: Introduction to Scala

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

The SCAlable LAnguage

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

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

First Programming Language in CS Education The Arguments for Scala

A Whirlwind Tour of Scala. Kevin Kilroy October 2014

Programming in Scala Mixin inheritance Summer 2008

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

Object-oriented programming

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

Java Object Oriented Design. CSC207 Fall 2014

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Optimizing Higher-Order Functions in Scala

n n Official Scala website n Scala API n

CS 2340 Objects and Design - Scala

...something useful to do with the JVM.

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

A- Core Java Audience Prerequisites Approach Objectives 1. Introduction

Advanced Object-Oriented Languages Scala

CSCI-GA Final Exam

University of Tartu Faculty of Mathematics and Computer Science Institute of Computer Science

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Introducing Scala-like function types into Java-TX

CS Programming Languages: Scala

Topic VIII. The state of the art Scala < > Scala By Example by M. Odersky. Programming Methods Laboratory, EPFL, 2008.

Copyright 2018 Tendril, Inc. All rights reserved.

Course Description. Learn To: : Intro to JAVA SE7 and Programming using JAVA SE7. Course Outline ::

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

Index COPYRIGHTED MATERIAL

An introduction introduction to functional functional programming programming using usin Haskell

WA1278 Introduction to Java Using Eclipse

CS Programming Languages: Scala

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Swift. Introducing swift. Thomas Woodfin

Java 8 Programming for OO Experienced Developers

Background Type Classes (1B) Young Won Lim 6/28/18

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

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

Advanced Systems Programming

Object Oriented Issues in VDM++

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Programs as Data. The Scala language

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

Java Classes and Objects

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Practically Functional. Daniel Spiewak

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

CS 11 Haskell track: lecture 1

D7020E. The dynamic evolution of a system Robust and Energy-Efficient Real-Time Systems. blocked Lecture 2: Components & the Timber language.

D7020E. Robust and Energy-Efficient Real-Time Systems D7020E. Lecture 2: Components & the Timber language

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

Assumptions. History

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

Exercise 8 Parametric polymorphism November 18, 2016

Who am I? Harlan Iverson. Programming enthusiast. Seeker of truth. Imperfect. I'll be wrong about some things. Please correct me if you can.

G Programming Languages - Fall 2012

Using Scala for building DSL s

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

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Compiler construction

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

CMSC 330: Organization of Programming Languages. Rust Basics

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

Object-Oriented Software Engineering. Chapter 2: Review of Object Orientation

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

CS205: Scalable Software Systems

CSCI-GA Scripting Languages

Company Predecessor Domain Concepts

Applied Unified Ownership. Capabilities for Sharing Across Threads

Object-oriented programming in...

Introduction to Java

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Advances in Programming Languages: Type accuracy

Software Engineering: Design & Construction

Functions and Objects. Week 7: Symbolic Computation

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Reminder About Functions

learning objectives learn about variables, their types and their values learn about different number representations

301AA - Advanced Programming [AP-2017]

Test 1 Summer 2014 Multiple Choice. Write your answer to the LEFT of each problem. 5 points each 1. Preprocessor macros are associated with: A. C B.

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

Homework 8. What To Turn In. Reading. Self Check. Problems. Handout 19 CSCI 334: Spring, 2017

Object-Oriented Software Engineering Practical Software Development using UML and Java. Chapter 2: Review of Object Orientation

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

PIC 20A The Basics of Java

Using Scala in CS241

A Scala Tutorial for Java programmers

Java: introduction to object-oriented features

An introduction to functional programming. July 23, 2010

Transcription:

The new Java? July 4, 2009

Presentation Outline Outline Introduction Concise Syntax Object Orientation Functional Programming Various Stuff Conclusion

Introducing Scala What is Scala? 100% Object-oriented Functional programming Typing: static, strong, inferred/implicits Java bytecode JVM.NET (CLR) Also available Extensibility Java-like syntax Scala syntax Current version 2.7.5 2.8

Introducing Scala A first example class IntMath(val x: Int, val y: Int) { def sum(): Int = { x + y; def mul = x * y val test = new IntMath(3, 4) println(test.x) // output: 3 println(test.sum) // output: 7 println(test.mul()) // output: 12

Introducing Scala The Scala Interpreter An interactive shell for writing Scala expressions Automatically creates temporary result variables Simply type scala at the command prompt Using Maven2: Type mvn scala:console Starts the Scala Interpreter with project classpath Demo

Syntax Examples Imports Like Java, but more features Can be anywhere Import from packages, classes or objects import java.util._ // All import java.util.{arraylist,hashset // Selected import java.util.{arraylist => _, _ // All except import java.util.arraylist._ // All from AL val list = new ArrayList import list._ // All from object list import java.sql.{date => SDate // Renaming import java.util.date

Syntax Examples Method syntax. and () can be omitted for binary/unary methods Methods can have any name Operators are simply methods full operator overloading 2.max(5) // max of 2 and 5 2 max 5 // same as above class MyNumber(val num: Int) { def +(other: MyNumber) = new MyNumber(other.num + num) val x = new MyNumber(5) val y = new MyNumber(6) val z = x + y

Syntax Examples Type inferrence/implicits If type is obvious no need to write it Implicits can be defined Implicits heavily used in std lib (RichInt etc.) // Type inferrence val a = 42 // Type = Int val b = "hello world!" // Type = String def add(x: Int, y: Int) = x + y // Return-type = Int val sum = add(3, 5) // Type of sum = Int // Implicits class MyInt(val i: Int) { def doubleit = i * 2 implicit def fromint(i: Int) = new MyInt(i) 5.doubleIt // = 10

OO Introduction Object Orientation Pure OO everything is an object Classes blueprints for objects (like Java) Singleton objects Traits AOP like possibilities Semicolon inference

Classes and Objects Scala Class Hierarchy

Classes and Objects Classes Much like Java Contains fields and methods can override eachother override keyword mandatory Can take parameters directly constructor class A(val num: Int) class B(num: Int, val str: String) extends A(num) { def calc() = num * num // calc is a method class C(num: Int, str: String) extends B(num, str) { override val calc = 65 // override method with val val a = new A(35) // Type A val b = new B(32, "Eivind") // Type B - calc method val b2: B = new C(32, "Eivind") // Also type B - calc val

Classes and Objects Singleton Objects No static members in Scala Singleton Objects Keyword object instead of class Companion class/object same name Factory methods Other unique/static behaviour // Array class definition final class Array[A](_length: Int) extends Array0[A] // Array object definition with method object Array { def apply(xs: Int*): Array[Int] = {... // Create array with four integers val arr = Array(1, 2, 3, 4)

Classes and Objects The magic apply()-method Scala provides special syntax for calling the apply() method In classes used to look up elements (for instance in Collections) In objects used to create class instances Functions in Scala are actually just apply() methods Make your classes/objects appear as built-in syntax // Array object has apply method for creating arrays def apply(xs: Int*): Array[Int] = {... // Array class has apply method to access the array def apply(i: Int): A // Scala special syntax val arr = Array(4, 3, 2, 1) // Array.apply(4, 3, 2, 1) val three = arr(1) // arr.apply(1)

Traits Traits Encapsulates method and field definitions, much like classes A class can mix in any number of traits multiple inheritance Widen thin interfaces to rich ones Define stackable modifications trait Hello { def hello { println("hello!") trait Goodbye { def bye { println("bye bye!") // Object with both hello() and bye() methods.. object A extends Hello with Goodbye

Traits Traits Widen thin interface to rich Define one or a few abstract methods Define concrete methods implemented in terms of the abstract trait Ordered[A] { abstract def compare(that: A): Int def <(that: A): Boolean = this.compare(that) < 0 def <=(that: A): Boolean = this.compare(that) <= 0... class Name(val name: String) extends Ordered[Name] { def compare(that: Name) = this.name.compare(that.name) if(name1 <= name2) {... // val name1 = new Name("Ola")

Traits Traits Define stackable modifications Modify methods of a class Stack several modifications with each other abstract class IntQueue { def get: Int def put(x: Int) // + concrete impl IntQueueImpl trait PutPrint extends IntQueue { abstract override def put(x: Int) { println("put: " + x) super.put(x) val printqueue = new IntQueueImpl with PutPrint

Generics and Abstract Types Generics Classes and traits can be generified Generics/type parameterization impl with erasure (like Java) Type parameters are required (not like Java) Variance nonvariant, covariant and contravariant Upper and lower bounds trait Set[T] { // Nonvariant def contains(elem: T): Boolean... trait Set[+T] // Covariant trait Set[-T] // Contravariant trait OrderedSet[T <: Ordered[T]] // Upper bound trait Array[+T] { def indexof[s >: T](elem: S): S // Lower bound

Generics and Abstract Types Abstract types Types as abstract members Much like type parameterization, different usage Generic types reusable containers, collections ++ Abstract types premade subclasses, hides implementation abstract class OrderedSet { type DType <: Ordered[DType]... class IntSet extends OrderedSet { type DType = RichInt...

Introduction to Functional Programming Functional programming Scala goal: Mix OO and FP Some FP characteristics: Higher-order functions Function closure support Recursion as flow control Pure functions no side-effects Pattern matching Type inferrence/implicits

Introduction to Functional Programming Mutable/immutable Immutable data structures important in FP Pure function same result with same arguments Scala uses keywords var and val Immutable definitions (val) encouraged val num = 45 // Immutable - allways 45 num = 60 // Error: reassignment to val var num2 = 45 // Mutable - can be changed num2 = 60 // Ok

Functions Scala functions Higher-order functions args and results Objects that implement scala.functionn traits Special syntax support with => operator // Three equivalent definitions to find even numbers val f1 = new Function[Int, Boolean] { // Full version def apply(i: Int) = i % 2 == 0 val f2 = (i: Int) => i % 2 == 0 // Special operator def f3(i: Int) = i % 2 == 0 // Regular function definition // Usage (f1, f2 and f3 equivalent) f1(64) // Converts to f1.apply(64) val arr = Array(1, 2, 3, 4) arr.filter(f1) // Returns Array(2, 4)

Functions Closures/anonymous functions Scala Anonymous functions Like anonymous classes in Java (and Scala) Nothing special with closures just passing function object The (underscore) can be used to anonymize arguments val arr = Array(1, 2, 3, 4) arr.filter((i: Int) => i % 2 == 0) // Returns Array(2, 4) arr.filter(_ % 2 == 0) // Shorter version using _ arr.map(_ % 2) // Returns Array(1, 0, 1, 0) arr.foreach(print _) // Prints "1234"

Functions Partially applied functions and currying Partially applied functions leave args out Currying multiple argument lists // Partially applied function def sum(i: Int, j: Int) = i + j val fiveplus = sum(5, _: Int) // New func with 1 arg fiveplus(6) // Result 11 val myprint = print _ // Example from previous slide myprint("hello world!") // Currying example def curriedsum(i: Int)(j: Int) = i + j curriedsum(2)(3) // Result 5 val fiveplus = curriedsum(5)_ // New func with 1 arg fiveplus(6) // Result 11

Pattern Matching Pattern matching Like switch statements on steroids Kinds of patterns: Wildcard patterns the char again Constant patterns numbers, strings ++ Variable patterns names Constructor patterns case classes Sequence patterns all sequence classes (List, Array ++) Tuple patterns all tuples Typed patterns like function arguments Variable binding using the @ sign Pattern guards adding an if clause Pattern overlaps case order is important!

Pattern Matching Pattern matching Basic example def desc(x: Any) = x match { case 5 => "five" // Constant pattern case i: Int => "int: " + i.tostring // Typed patterns case s: String => "str: " + s case (a, b) => "tuple: " + a + b // Tuple pattern case _ => "unknown" // Wildcard/default pattern desc(8) // "int: 8" desc("scala") // "str: Scala" desc(5) // "five" desc(("eivind", 32)) // "tuple: Eivind32" desc(4.0) // "unknown"

Pattern Matching Pattern matching Case classes example Factory method + all params are val Methods tostring, hashcode and equals added case class Person(name: String, age: Int) def greet(x: Any) = x match { case Person("Eivind", 32) => "Hello creator!" case Person(n, a) if a < 32 => "Hello young " + n case Person(n, _) => "Hello " + n case _ => "Hello whatever" greet(person("ola", 80)) // Hello Ola greet(person("ola", 5)) // Hello young Ola greet("eivind") // Hello whatever greet(person("eivind", 32)) // Hello creator!

Pattern Matching Pattern matching List example def desc(x: Any) = x match { case List(_: String, _: String) => "List of two strings" case List(_, _) => "List of two elems" case head :: _ => "List starting with " + head case _ => "Whatever" desc(list(1, 2)) // List of two elems desc(list(1, 2, 3)) // List starting with 1 desc(list("hello", "world")) // List of two strings // Note! Two equivalent defs - "Error: unreachable code" case head :: _ => "List starts with " + head case List(head, _*) => "List starts with " + head

Other Functional Concepts For expressions Generators, definitions and filters Can yield value Range class A rewrite of methods map, flatmap and filter for (l <- "letters") println(l) // Split with line for (num <- 1 until 10) print(num) // Prints "123456789" for { p <- persons // Generator n = p.name // Definition if(n startswith "E") // Filter yield n // Two generators - with ( and ; to compact for (n <- 1 to 3; l <- "xyz") yield n.tostring + l

Interesting Tidbits Actors api Simplify concurrent programming Hides threads message based Immutable objects and functional style recommended! import scala.actors.actor._ val helloactor = actor { while (true) { receive { case msg => println("hello message: " + msg) helloactor! "Hello World!!"

Interesting Tidbits Continuations Coming in Scala 2.8 Typical tasks: Asynchronous I/O with Java NIO Executors and thread pools Cross-request control flow in web applications New library functions, not keywords shift and reset Complicated different mindset reset { shift { k: (Int => Int) => k(k(k(7))) + 1 * 2 // result: 20

Interesting Tidbits Java integration Java is called from Scala directly @scala.reflect.beanproperty annotation import java.util.date val d = new Date Scala compiles to standard Java bytecode Singleton objects combo of static and instance methods Traits interface + ancillary class Operators methods with special names def +(other: RichInt) =... // Scala public RichInt \$plus(richint other) {... // Java

Rounding It Up Conclusion Object orientation and functional programming combined Static and compiled benefits, not obstacles Rich syntax Java-code / 3? Seamless integration with Java run in existing environment Big momentum the new Java? More Info: http://www.scala-lang.org/