A Scala Firehose - - PowerPoint PPT Presentation

a scala firehose
SMART_READER_LITE
LIVE PREVIEW

A Scala Firehose - - PowerPoint PPT Presentation

A Scala Firehose Enough about me: Run the Artima Developer website Existing investment


slide-1
SLIDE 1

A Scala Firehose

slide-2
SLIDE 2

Enough about me:

  • Run the Artima Developer website
  • Existing investment in Java knowledge and

code

  • Wanted a more productive language for JVM
  • Didn’t want to give up static typing
  • Scala fit my needs
  • Scala book, ScalaTest
slide-3
SLIDE 3

A bit about Scala:

  • Designed by Martin Odersky

Scala is:

  • A Scalable language
  • Object-oriented
  • Functional
  • Statically typed
slide-4
SLIDE 4

A Scalable language

  • From scripts to systems
  • Grow new types
  • Grow new control constructs

Design libraries that enable clear, concise client code that feels like native language support.

slide-5
SLIDE 5

Scalable language means:

  • 1. From scripts to systems
slide-6
SLIDE 6

Ruby:

class Hello { public static void main(String[] args) { System.out.println("Hello, world!"); } }

Java:

puts "Hello, world!"

slide-7
SLIDE 7

Scala

println("Hello, world!")

slide-8
SLIDE 8

Scalable language means:

  • 2. Extensible by growing new types
slide-9
SLIDE 9

Java's if statement:

if (a > b) System.out.println(a); else System.out.println(b); int m = (a > b) ? a : b; System.out.println(m);

Java’s ternary operator:

slide-10
SLIDE 10

Scala’s if expression:

val m = if (a > b) a else b println(m)

slide-11
SLIDE 11

if (x == BigInteger.ZERO) BigInteger.ONE else x.multiply(factorial(x.subtract(BigInteger.ONE)))

java.math.BigInteger: scala.BigInt:

if (x == 0) 1 else x * factorial(x - 1)

slide-12
SLIDE 12

Scalable language means:

  • 3. Extensible by growing new control

constructs

slide-13
SLIDE 13

String s = "hi”; try { s.charAt(-1); fail(); } catch (IndexOutOfBoundsException e) { // Expected, so continue }

JUnit 3: JUnit 4:

@Test(expected=IndexOutOfBoundsException.class) public void testPassingANegativeToCharAt() { s.charAt(-1); }

slide-14
SLIDE 14

intercept(classOf[IndexOutOfBoundsException]) { s.charAt(-1) }

ScalaTest:

slide-15
SLIDE 15

How Scala scales:

  • 1. Scala is object-oriented
slide-16
SLIDE 16

1 + 2 (1).+(2)

“Pure” OO language: Every value is an object; Every operation is a method call.

slide-17
SLIDE 17

Domain-specific languages

if (x == 0) 1 else x * factorial(x - 1)

x - 1 x.-(1)

slide-18
SLIDE 18

But what about…

1 - x

slide-19
SLIDE 19

Implicit conversions

int2BigInt (1).-(x) 1 - x

implicit def int2BigInt(i: Int): BigInt = new BigInt(i)

slide-20
SLIDE 20

Why i: Int, not int i?

val i = 0 not i = 0

val m: HashMap[String, (String, List[Char])] = …

not

final HashMap<String, Pair<String, List<Char>>> m = …

  • 1. Type inference:
  • 2. Large type expressions:
slide-21
SLIDE 21

Scala Traits

trait T { def abstractMeth(s: String): Int def concreteMeth(s: String) = s + field var field = "!" } interface Ex { int abstractMeth(String x); } (no concrete methods) (no fields)

Java Interfaces Scala Mixin Composition

class C extends Super with T

Java Interface Implementation

Class C extends Super implements Ex

slide-22
SLIDE 22

Scala:

class Sample(x: Int, val p: Int) { def instMeth(y: Int) = x + y }

  • bject Sample {

def mult(x: Int, y: Int) = x * y } class Sample { private final int x; public final int p; Sample(int x, int p) { this.x = x; this.p = p; } int instMeth(int y) { return x + y; } static int staticMeth(int x, int y) { return x * y; } }

Java: Classes and Objects

Sample.mult(1, 2)

Invoked:

slide-23
SLIDE 23

How Scala scales:

  • 2. Scala is functional
slide-24
SLIDE 24

int incr(int x) { return x + 1; }

A Java method

slide-25
SLIDE 25

def incr(x: Int) = x + 1

A Scala function

slide-26
SLIDE 26

incr(x) = x + 1

A mathematical function

slide-27
SLIDE 27

def incr(x: Int) => x + 1

Anonymizing a function

slide-28
SLIDE 28

(x: Int) => x + 1

A function literal

slide-29
SLIDE 29

scala> val f = (x: Int) => x + 1 f: (Int) => Int = <function> scala> f(1) res0: Int = 2

Functions are first-class values

slide-30
SLIDE 30

f (1) f.apply(1)

Functions are first-class values

slide-31
SLIDE 31

Functional “attitude”

  • Prefer immutable objects
  • Prefer “initialize-only” variables
  • Prefer methods with no side-effects
slide-32
SLIDE 32

Immutable tradeoffs

  • Simpler
  • Can pass them around
  • Inherently thread safe
  • Safest hashtable keys
  • Large graphs expensive to replicate

– So maybe offer a mutable alternative

slide-33
SLIDE 33

Java String

String s = "immutable"; String t = s.replace("im", "also im"); System.out.println(s + ", " + t); immutable, also immutable

slide-34
SLIDE 34

Ruby String

irb(main):007:0> s = 'immutable' => "immutable" irb(main):008:0> s['im'] = 'quite ' => "" irb(main):009:0> puts s quite mutable

slide-35
SLIDE 35

Scala String is Java String

val s = "immutable" val t = s.replace("im", "also im") println(s + ", " + t) immutable, also immutable

slide-36
SLIDE 36

Java List

import java.util.List; List<String> mutable = new ArrayList<String>(); mutable.add("Hi"); List<String> unmodifiable = Collections.unmodifiableList(mutable);

slide-37
SLIDE 37

Scala List

val myList = List("Hi" , "there")

slide-38
SLIDE 38

Scala ListBuffer and Array

import scala.collection.mutable.ListBuffer val muta = new ListBuffer[String] muta += "Hi" val arr = Array("Hi")

slide-39
SLIDE 39

Scala Set

val mySet = Set("Hi", "there")

slide-40
SLIDE 40

Scala mutable.Set

import scala.collection.mutable.Set val muta = Set("Hi")

slide-41
SLIDE 41

Scala's Set Hierarchy

slide-42
SLIDE 42

Java variables String s = "Hi"; final String t = "there";

slide-43
SLIDE 43

Scala variables val s = "Hi"; var t = "there";

slide-44
SLIDE 44

Java idiom String s = "default"; if (args.length > 0) { s = args[0]; }

slide-45
SLIDE 45

Scala with Java accent var s = "default" if (args.length > 0) { s = args(0) }

slide-46
SLIDE 46

Scala idiom val s = if (args.length > 0) args(0) else "default"

slide-47
SLIDE 47

gcdLoop

def gcdLoop(x: Long, y: Long): Long = { var a = x var b = y while (a != 0) { val temp = a a = b % a b = temp } b }

slide-48
SLIDE 48

gcd

def gcd(x: Long, y: Long): Long = if (y == 0) x else gcd(y, x % y)

slide-49
SLIDE 49

How Scala scales:

  • 3. Scala is statically typed
slide-50
SLIDE 50

Type annotations in Java

Map<Integer, String> myMap = new HashMap<Integer, String>();

slide-51
SLIDE 51

Type annotations in Scala

val hm = new HashMap[Int, String]() val m: Map[Int, String] = new HashMap()

  • r
slide-52
SLIDE 52

javax.servlet.ServletRequest

public java.lang.String getParameter(java.lang.String name) Returns the value of a request parameter as a String, or null if the parameter does not exist

slide-53
SLIDE 53

Some(param) or None

slide-54
SLIDE 54

scala.List

def find (p: (A) => Boolean): Option[A] Find and return the first element of the list satisfying a predicate, if any.

slide-55
SLIDE 55

Using Option

val opt = args.find( arg => { arg.substring(0, 2) == "-g" } )

  • pt match {

case Some(dashG) => println("Found: " + dashG) case None => println("No -g found") }

slide-56
SLIDE 56

Getting Started with Scala

  • Download Scala www.scala-lang.org
  • Get the eBook www.artima.com
  • Write scripts
  • Do a side project
  • "Mix in" Scala with Java
slide-57
SLIDE 57