Scala coated JVM
@ Joint meeting of Java User Group Scotland
            and Scala Scotland


            Stuart Roebuck
 ...
The basics…

• Created by Martin Odersky (EPFL)
• JVM
• Object oriented and functional
• ‘scalable language’
• Scala 1.0—l...
“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...
Try this at home!

• Scala home: http://www.scala-lang.org/
• Downloadable for Mac, Linux & Windows
• Shell interpreter: s...
Demo 1
Scripting with Scala
Email extraction shell script
#! /bin/sh
exec scala "$0" "$@"
!#

import scala.io.Source

val email = """[a-zA-Z0-9._%+-]+...
How does Scala differ
             from Java?

•   Everything is an object   •   Pattern matching and
                     ...
Everything is an object
“Answer = ” + 6 * 4

“Answer = ”.+((6).*(4))
First class functions
def time(f: => Unit): Double = {
  val start = System.nanoTime
  f
  val end = System.nanoTime
  (en...
Singleton Objects (Java)
public class Singleton {

    private Singleton() {
    }

    private static class SingletonHold...
Singleton Objects (Scala)
object Singleton {
  val name = “This is a Singleton Object”
}
Traits / Mix-in Composition
class Executor(f: () => Unit) {
   def exec() { f() }
}
trait Logging extends Executor {
   ov...
Pattern Matching
def intToString(value: Any) = value match {
  case x:Int => x.toString
  case (x:Int) :: y => x.toString
...
Demo 2
e Scala REPL(Read Eval Print Loop)
BigInteger / BigInt Factorial
import java.math.BigInteger

def factorial(x: BigInteger): BigInteger =
  if (x == BigIntege...
BigInt Definition
class BigInt(val bigInteger: BigInteger) extends java.lang.Number {

  override def hashCode(): Int = thi...
Implicit conversion
scala> factorial(10)
res0: BigInt = 3628800



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



...
Pattern matching
scala> val Email = """([a-zA-Z0-9._%+-]+)@([a-zA-Z0-9.-]+.[a-zA-Z]
{2,4})""".r
Email: scala.util.matching...
Demo 3
Building a Scala project with sbt
Questions?
Build tools +
• Maven Plugin (no endorsement implied)—http://
  scala-tools.org/mvnsites/maven-scala-plugin/
• simple-buil...
Scala coated JVM
Scala coated JVM
Upcoming SlideShare
Loading in …5
×

Scala coated JVM

2,512
-1

Published on

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

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,512
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
30
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Scala coated JVM

  1. 1. Scala coated JVM @ Joint meeting of Java User Group Scotland and Scala Scotland Stuart Roebuck stuart.roebuck@proinnovate.com
  2. 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. 3. “If I were to pick a language to use today other than Java, it would be Scala” James Gosling
  4. 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. 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. 6. Demo 1 Scripting with Scala
  7. 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. 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. 9. Everything is an object “Answer = ” + 6 * 4 “Answer = ”.+((6).*(4))
  10. 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. 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. 12. Singleton Objects (Scala) object Singleton { val name = “This is a Singleton Object” }
  13. 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. 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. 15. Demo 2 e Scala REPL(Read Eval Print Loop)
  16. 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. 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. 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. 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. 20. Demo 3 Building a Scala project with sbt
  21. 21. Questions?
  22. 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/
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×