Scala coated JVM
-
Upload
sroebuck -
Category
Technology
-
view
127 -
download
0
description
Transcript of Scala coated JVM
Scala coated JVM@ Joint meeting of Java User Group Scotland
and Scala Scotland
Stuart [email protected]
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
“If I were to pick a language to use today other than Java, it would be Scala”
James Gosling
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
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)
Demo 1Scripting with Scala
#! /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
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
“Answer = ” + 6 * 4
“Answer = ”.+((6).*(4))
Everything is an object
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)
}
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)
object Singleton {val name = “This is a Singleton Object”
}
Singleton Objects (Scala)
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
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 =
Demo 2e Scala REPL(Read Eval Print Loop)
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)
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
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
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
Demo 3Building a Scala project with sbt
Questions?
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/