scala for java programmers

51
Scala for Java Programmers

Upload: akira-koyasu

Post on 18-May-2015

4.510 views

Category:

Technology


4 download

DESCRIPTION

Introducing Scala programming language for Java programmers. Several functional concepts, traits, pattern matching and actors are mentioned.

TRANSCRIPT

Page 1: Scala for Java programmers

Scalaf o r J a v a P r o g r a m m e r s

Page 2: Scala for Java programmers

Hello, Scala!

Page 3: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Hello, World!

3

Page 4: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Hello, World!

object HelloWorld { def main(args: Array[String]) = println("Hello, World!")}

3

Page 5: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Agenda

About Scala

Features

4

Page 6: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Motivation

Scalable language. To be scalable in the sense that the same concepts can describe small as well as large parts.

A unified and generalized object-oriented and functional programming language provides scalable support.

From Inventor

5

Page 7: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Motivation

More productive as “better Java” with existing Java resources.

Several concepts not in Java bring more posibilities to you.

For Java programmers

6

Page 8: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

the Programming Language

Language Specification

API

Runtime

7

Page 9: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

API

8

Scaladoc

Page 10: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Runtime

scalac

.scala

.class

the compiler for .NET is out of date

JVMscala

Running on JVM

Compiler generates class files

9

Page 11: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Influencers

Java C#

Smalltalk

Haskell

Algol Simula

Eiffel

SML F#

Erlang

IswimLisp

Python

Ruby

10

Page 12: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Inventor

Martin Odersky

Professor of programming methods at EPFL in Switzerland

Designer of Java generics

photo#1

11

Page 13: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Object, Operator

val apple = new Appleval orange = new Orangeprintln(apple + orange)

?

12

Page 14: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Object, Operator

val apple = new Appleval orange = new Orangeprintln(apple + orange)

?

(A) "Apple@xxxxOrange@xxxx"(B) 2(C) Compile error(D) Runtime exception(E) Others

12

Page 15: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Features

Static typing & Suitable type inference

Object-oriented & Functional

Trait

Collaborating with Java

13

Page 16: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Vorbose JavaType declaration

public String suffix(String str) { int pos = str.indexOf("-"); return str.substring(pos);}

14

Page 17: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Vorbose JavaType declaration

def suffix(str: String) = { val pos = str.indexOf("-") str.substring(pos)}

14

Page 18: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Vorbose JavaJavaBeans

public class JavaBeans { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; }}

15

Page 19: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Vorbose JavaJavaBeans

case class ScalaCase(name: String)

15

Page 20: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Functional Programming

variables

method parameters

method returns

Functions are the first-class values

16

Page 21: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Functional Programming

Consider a method signature sending mails to appropriate addresses from listing

def send() { for(c: Contact <- listing()) { mail(c) }}

17

Page 22: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Functional Programming

18

Page 23: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Functional Programming

age==20

18

Page 24: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Functional Programming

age==20 def send(age: Int)

18

Page 25: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Functional Programming

age==20

35<=age, age<=60

def send(age: Int)

18

Page 26: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Functional Programming

age==20

35<=age, age<=60

def send(age: Int)

def send(minAge: Int, maxAge: Int)

18

Page 27: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Functional Programming

age==20

35<=age, age<=60

age<30, gender:Male

def send(age: Int)

def send(minAge: Int, maxAge: Int)

18

Page 28: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Functional Programming

age==20

35<=age, age<=60

age<30, gender:Male

def send(age: Int)

def send(minAge: Int, maxAge: Int)

def send(minAge: Int, maxAge: Int, g: Gender)

18

Page 29: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Functional Programming

age==20

35<=age, age<=60

age<30, gender:Male

gender:Female, in Tokyo

def send(age: Int)

def send(minAge: Int, maxAge: Int)

def send(minAge: Int, maxAge: Int, g: Gender)

18

Page 30: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Functional Programming

age==20

35<=age, age<=60

age<30, gender:Male

gender:Female, in Tokyo

def send(age: Int)

def send(minAge: Int, maxAge: Int)

def send(minAge: Int, maxAge: Int, g: Gender)

def send(minAge: Int, maxAge: Int, g: Gender, pref: String)

18

Page 31: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Functional Programming

def send(p: Contact => Boolean) { for(c: Contact <- listing()) { if (p(c)) mail(c) }}

Apply condition function to method

19

Page 32: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Functional Programming

y = f(x)Avoiding side effects

Conscious of immutability

20

Page 33: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Partially applied Function

21

def sum(a: Int, b: Int, c: Int) = a + b + cval f1 = sum _val f2 = sum(1, _: Int, 3)

def main(args: Array[String]) { println(f1(1, 2, 3)) // 6 println(f2(2)) // 6 println(f2(5)) // 9}

Page 34: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Curried Function

22

def sum(a: Int)(b: Int) = a + b

def main(args: Array[String]) { println(sum(1)(2)) // 3}

Page 35: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Curried Function

23

def withResource(r: Closeable)(op: => Unit) { try { op } finally { r.close() }} def main(args: Array[String]) { val writer = new FileWriter("test.txt") withResource(writer) { writer.write("foo bar") }}

Page 36: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Trait

a special form of an abstract classwhich can be used as mixins

24

Page 37: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Trait

25

to enrich interface

class MyInt(val n: Int) extends Ordered[MyInt] { def compare(that: MyInt) = this.n - that.n}

val n1 = new MyInt(1)val n2 = new MyInt(2)println(n1 < n2)println(n1 > n2)println(n1 <= n2)println(n1 >= n2)

Page 38: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Trait

26

stackable modifications

abstract class Sale { def price(): Double}

class BasicSale extends Sale { def price() = 100;}

trait OffTenPercent extends Sale { abstract override def price() = { super.price.toDouble * 0.9 }}

trait OffTwenty extends Sale { abstract override def price() = { super.price - 20 }}

-10%

-¥20

¥100

Page 39: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Trait

27

stackable modifications

class UnbelievableSale extends BasicSale with OffTenPercentclass UnbelievableSale2 extends BasicSale with OffTwentyclass UnbelievableSale3 extends BasicSale with OffTenPercent with OffTwentyclass UnbelievableSale4 extends BasicSale with OffTwenty with OffTenPercent

def main(args: Array[String]) { println((new BasicSale).price) // 100.0 println((new UnbelievableSale).price) // 90.0 println((new UnbelievableSale2).price) // 80.0 println((new UnbelievableSale3).price) // 70.0 println((new UnbelievableSale4).price) // 72.0}

-10%-¥20-10%, -¥20 -¥20, -10%

Page 40: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Pattern Match

28

case class ScalaCase(name: String)

def test(x: Any) = x match { case 1 => println("1") case str: String => println(str) case ScalaCase(name) => println("name: " + name) case _ => println("other")}

def main(args: Array[String]) { test(1) // 1 test("Hello!") // Hello! test(ScalaCase("apple")) // name: apple test(2.5) // other}

Page 41: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.29

Page 42: Scala for Java programmers

Scala in Action

Page 43: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Collection Operation

31

getting the highest score in 2013

case class Student(year: Int, score: Int)

def students(): List[Student] = List(Student(2013, 92), Student(2012, 98), Student(2013, 70))def students2(): List[Student] = Student(2013, 92)::Student(2012, 98)::Student(2013, 70)::Nil

def highestScoreIn2013() = students() .filter(s => s.year == 2013) .map(s => s.score) .foldLeft(0)((a, b) => max(a, b))

Page 44: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Actor

32

actorA

actorB

actorC

actorD

No shared data

Exchanging messages

Concurrency model

Page 45: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Actor (Akka)

33

object Actors { implicit val system = ActorSystem("MySystem") val a, b = actor(new Act { become { case ("ping", actor: ActorRef) => { println("received ping") Thread.sleep(1000) actor ! ("pong", self) } case ("pong", actor: ActorRef) => { println("received pong") Thread.sleep(1000) actor ! ("ping", self) } } }) def main(args: Array[String]) { a ! ("ping", b) }}

received pingreceived pongreceived pingreceived pongreceived pingreceived pong...

Page 46: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Practical Scala

34

Page 47: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Practical Scala

Killer framework Play (+)

Java on the bench (+)

Interactive shell (+)

There are many concepts (-)

the compiler needs more resources (-)

Backward compatibility? (-)

34

Page 48: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Next Step

Implicit conversions

Extractor

Abstract type

Type parameter & variance

35

Page 49: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Reference

An Overview of the Scala Programming Language Second Edition (pdf)

A Scala Tutorial for Java programmers (pdf)

Programming in Scala, Second Edition (Japanese version: Scala スケーラブルプログラミング 第2版)

36

Page 50: Scala for Java programmers

Copyright © 2013 Akira Koyasu. Some rights reserved.

Notes

This work is licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/3.0/.

37

photo#1: http://en.wikipedia.org/wiki/File:Mark_Odersky_photo_by_Linda_Poeng.jpg

Feed backs Welcome!

http://twitter.com/akirakoyasu

http://fb.me/akirakoyasu

Page 51: Scala for Java programmers

Thank you!