Scala coated JVM

24
Scala coated JVM @ Joint meeting of Java User Group Scotland and Scala Scotland Stuart Roebuck [email protected]

description

An introductory talk on Scala at the joint meeting of Java User Group Scotland and Scala Scotland in Edinburgh on 28 September 2010

Transcript of Scala coated JVM

Page 1: Scala coated JVM

Scala coated JVM@ Joint meeting of Java User Group Scotland

and Scala Scotland

Stuart [email protected]

Page 2: Scala coated JVM

The basics…

• Created by Martin Odersky (EPFL)• JVM• Object oriented and functional• ‘scalable language’• Scala 1.0—late 2003• Scala 2.8.0—July 2010

Page 3: Scala coated JVM

“If I were to pick a language to use today other than Java, it would be Scala”

James Gosling

Page 4: Scala coated JVM

Commercial users of Scala• Twitter—Scala back end Ruby front end• LinkedIn• Foursquare—Scala and Lift• Siemens—Scala and Lift• SAP• EDF• Sony Pictures (ImageWorks)• Nature Magazine• TomTom• …and Google

Page 5: Scala coated JVM

Try this at home!

• Scala home: http://www.scala-lang.org/• Downloadable for Mac, Linux & Windows• Shell interpreter: scala• Compilers: scalac and fsc• Documentation generator scaladoc• Plugins for Eclipse, Netbeans, IntelliJ• Popular build tool “sbt” (simple-build-tool)

Page 6: Scala coated JVM

Demo 1Scripting with Scala

Page 7: Scala coated JVM

#! /bin/shexec scala "$0" "$@"!#

import scala.io.Source

val email = """[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}""".rval filtered = Source.stdin.getLines.

flatMap( email.findAllIn(_)).map(_.toLowerCase).toSet.toList.sortWith(_<_)

filtered.foreach{ println(_) }

Email extraction shell script

Page 8: Scala coated JVM

How does Scala differfrom Java?

• Everything is an object• First-class functions

(‘closures’)• Singleton objects• Mixin composition

with Traits

• Pattern matching and Extractors

• XML literals• Case classes• Lazy evaluation• Tuples

Page 9: Scala coated JVM

“Answer = ” + 6 * 4

“Answer = ”.+((6).*(4))

Everything is an object

Page 10: Scala coated JVM

def time(f: => Unit): Double = {val start = System.nanoTimefval end = System.nanoTime(end - start) / 1000000.0

}

First class functions

val timeTaken = time {Thread.sleep(100)

}

Page 11: Scala coated JVM

public class Singleton { private Singleton() { } private static class SingletonHolder { public static final Singleton INSTANCE = new Singleton(); } public static Singleton getInstance() { return SingletonHolder.INSTANCE; } }

Singleton Objects (Java)

Page 12: Scala coated JVM

object Singleton {val name = “This is a Singleton Object”

}

Singleton Objects (Scala)

Page 13: Scala coated JVM

class Executor(f: () => Unit) {def exec() { f() }

}trait Logging extends Executor {

override def exec() {println("Executing...")super.exec()

}}trait Timing extends Executor {

override def exec() {val start = System.currentTimeMillissuper.exec()val end = System.currentTimeMillisprintf("==> Time taken: %d ms%n", end-start)

}}

val e = new Executor(() => println("Hello")) with Timing with Logginge.execExecuting...Hello==> Time taken: 0 ms

Traits / Mix-in Composition

Page 14: Scala coated JVM

def intToString(value: Any) = value match {case x:Int => x.toStringcase (x:Int) :: y => x.toStringcase Some(x:Int) => x.toStringcase _ => ""

}

Pattern Matching

scala> intToString(23)res1: java.lang.String = 23

scala> intToString(List(23,45))res2: java.lang.String = 23

scala> intToString(Some(11)) res3: java.lang.String = 11

scala> intToString(Some("String"))

res4: java.lang.String =

Page 15: Scala coated JVM

Demo 2e Scala REPL(Read Eval Print Loop)

Page 16: Scala coated JVM

import java.math.BigInteger

def factorial(x: BigInteger): BigInteger =if (x == BigInteger.ZERO)

BigInteger.ONEelse

x.multiply(factorial(x.subtract(BigInteger.ONE)))

BigInteger / BigInt Factorial

def factorial(x: BigInt): BigInt =if (x == 0) 1 else x * factorial(x - 1)

Page 17: Scala coated JVM

class BigInt(val bigInteger: BigInteger) extends java.lang.Number {

override def hashCode(): Int = this.bigInteger.hashCode()

override def equals (that: Any): Boolean = that match { case that: BigInt => this equals that case that: java.lang.Double => this.bigInteger.doubleValue == that.doubleValue case that: java.lang.Float => this.bigInteger.floatValue == that.floatValue case that: java.lang.Number => this equals BigInt(that.longValue) case that: java.lang.Character => this equals BigInt(that.charValue.asInstanceOf[Int]) case _ => false}

def equals (that: BigInt): Boolean = this.bigInteger.compareTo(that.bigInteger) == 0def compare (that: BigInt): Int = this.bigInteger.compareTo(that.bigInteger)def <= (that: BigInt): Boolean = this.bigInteger.compareTo(that.bigInteger) <= 0def >= (that: BigInt): Boolean = this.bigInteger.compareTo(that.bigInteger) >= 0def < (that: BigInt): Boolean = this.bigInteger.compareTo(that.bigInteger) < 0def > (that: BigInt): Boolean = this.bigInteger.compareTo(that.bigInteger) > 0def + (that: BigInt): BigInt = new BigInt(this.bigInteger.add(that.bigInteger))…

BigInt Definition

Page 18: Scala coated JVM

implicit def int2bigInt(i: Int): BigInt = BigInt(i)

Implicit conversionscala> factorial(10)res0: BigInt = 3628800

def factorial(x: BigInt): BigInt = …

scala> factorial(int2bigInt(10))res0: BigInt = 3628800

Page 19: Scala coated JVM

scala> val Email = """([a-zA-Z0-9._%+-]+)@([a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})""".rEmail: scala.util.matching.Regex = ([a-zA-Z0-9._%+-]+)@([a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})

scala> val Email(name,address) = "[email protected]"name: String = stuart.roebuckaddress: String = proinnovate.com

Pattern matching

Page 20: Scala coated JVM

Demo 3Building a Scala project with sbt

Page 21: Scala coated JVM
Page 22: Scala coated JVM

Questions?

Page 23: Scala coated JVM
Page 24: Scala coated JVM

Build tools +• Maven Plugin (no endorsement implied)—http://

scala-tools.org/mvnsites/maven-scala-plugin/• simple-build-tool—http://code.google.com/p/

simple-build-tool/• Apache Ant tasks for Scala—http://www.scala-

lang.org/node/98• Apache Buildr—http://buildr.apache.org/• JavaRebel—http://www.zeroturnaround.com/

jrebel/