Scala coated JVM
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Scala coated JVM

on

  • 2,839 views

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

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

Statistics

Views

Total Views
2,839
Views on SlideShare
2,833
Embed Views
6

Actions

Likes
2
Downloads
29
Comments
0

1 Embed 6

http://www.linkedin.com 6

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Scala coated JVM Presentation Transcript

  • 1. Scala coated JVM @ Joint meeting of Java User Group Scotland and Scala Scotland Stuart Roebuck stuart.roebuck@proinnovate.com
  • 2. 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
  • 3. “If I were to pick a language to use today other than Java, it would be Scala” James Gosling
  • 4. 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
  • 5. 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)
  • 6. Demo 1 Scripting with Scala
  • 7. Email extraction shell script #! /bin/sh exec scala "$0" "$@" !# import scala.io.Source val email = """[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}""".r val filtered = Source.stdin.getLines. flatMap( email.findAllIn(_)). map(_.toLowerCase).toSet.toList.sortWith(_<_) filtered.foreach{ println(_) }
  • 8. How does Scala differ from Java? • Everything is an object • Pattern matching and Extractors • First-class functions (‘closures’) • XML literals • Singleton objects • Case classes • Mixin composition • Lazy evaluation with Traits • Tuples
  • 9. Everything is an object “Answer = ” + 6 * 4 “Answer = ”.+((6).*(4))
  • 10. First class functions def time(f: => Unit): Double = { val start = System.nanoTime f val end = System.nanoTime (end - start) / 1000000.0 } val timeTaken = time { Thread.sleep(100) }
  • 11. Singleton Objects (Java) public class Singleton { private Singleton() { } private static class SingletonHolder { public static final Singleton INSTANCE = new Singleton(); } public static Singleton getInstance() { return SingletonHolder.INSTANCE; } }
  • 12. Singleton Objects (Scala) object Singleton { val name = “This is a Singleton Object” }
  • 13. Traits / Mix-in Composition 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.currentTimeMillis super.exec() val end = System.currentTimeMillis printf("==> Time taken: %d ms%n", end-start) } } val e = new Executor(() => println("Hello")) with Timing with Logging e.exec Executing... Hello ==> Time taken: 0 ms
  • 14. Pattern Matching def intToString(value: Any) = value match { case x:Int => x.toString case (x:Int) :: y => x.toString case Some(x:Int) => x.toString case _ => "" } 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 =
  • 15. Demo 2 e Scala REPL(Read Eval Print Loop)
  • 16. BigInteger / BigInt Factorial import java.math.BigInteger def factorial(x: BigInteger): BigInteger = if (x == BigInteger.ZERO) BigInteger.ONE else x.multiply(factorial(x.subtract(BigInteger.ONE))) def factorial(x: BigInt): BigInt = if (x == 0) 1 else x * factorial(x - 1)
  • 17. BigInt Definition 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) == 0 def compare (that: BigInt): Int = this.bigInteger.compareTo(that.bigInteger) def <= (that: BigInt): Boolean = this.bigInteger.compareTo(that.bigInteger) <= 0 def >= (that: BigInt): Boolean = this.bigInteger.compareTo(that.bigInteger) >= 0 def < (that: BigInt): Boolean = this.bigInteger.compareTo(that.bigInteger) < 0 def > (that: BigInt): Boolean = this.bigInteger.compareTo(that.bigInteger) > 0 def + (that: BigInt): BigInt = new BigInt(this.bigInteger.add(that.bigInteger)) …
  • 18. Implicit conversion scala> factorial(10) res0: BigInt = 3628800 implicit def int2bigInt(i: Int): BigInt = BigInt(i) def factorial(x: BigInt): BigInt = … scala> factorial(int2bigInt(10)) res0: BigInt = 3628800
  • 19. Pattern matching scala> val Email = """([a-zA-Z0-9._%+-]+)@([a-zA-Z0-9.-]+.[a-zA-Z] {2,4})""".r Email: scala.util.matching.Regex = ([a-zA-Z0-9._%+-]+)@([a-zA- Z0-9.-]+.[a-zA-Z]{2,4}) scala> val Email(name,address) = "stuart.roebuck@proinnovate.com" name: String = stuart.roebuck address: String = proinnovate.com
  • 20. Demo 3 Building a Scala project with sbt
  • 21. Questions?
  • 22. 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/