SlideShare una empresa de Scribd logo
1 de 24
Descargar para leer sin conexión
Scala coated JVM
@ Joint meeting of Java User Group Scotland
            and Scala Scotland


            Stuart Roebuck
            stuart.roebuck@proinnovate.com
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 1
Scripting with Scala
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(_) }
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
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
  (end - start) / 1000000.0
}


val timeTaken = time {
  Thread.sleep(100)
}
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;
    }

}
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 {
   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
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 =
Demo 2
e Scala REPL(Read Eval Print Loop)
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)
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))
  …
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
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
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-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/

Más contenido relacionado

La actualidad más candente

Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008Yardena Meymann
 
A Tour Of Scala
A Tour Of ScalaA Tour Of Scala
A Tour Of Scalafanf42
 
Metaprogramming in Scala 2.10, Eugene Burmako,
Metaprogramming  in Scala 2.10, Eugene Burmako, Metaprogramming  in Scala 2.10, Eugene Burmako,
Metaprogramming in Scala 2.10, Eugene Burmako, Vasil Remeniuk
 
The Why and How of Scala at Twitter
The Why and How of Scala at TwitterThe Why and How of Scala at Twitter
The Why and How of Scala at TwitterAlex Payne
 
Introduction to Scala for Java Developers
Introduction to Scala for Java DevelopersIntroduction to Scala for Java Developers
Introduction to Scala for Java DevelopersMichael Galpin
 
Intro to Functional Programming in Scala
Intro to Functional Programming in ScalaIntro to Functional Programming in Scala
Intro to Functional Programming in ScalaShai Yallin
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Jonas Bonér
 
Procedure Typing for Scala
Procedure Typing for ScalaProcedure Typing for Scala
Procedure Typing for Scalaakuklev
 
Scala for scripting
Scala for scriptingScala for scripting
Scala for scriptingmichid
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Martin Odersky
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 

La actualidad más candente (20)

Scala introduction
Scala introductionScala introduction
Scala introduction
 
Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008
 
Spark workshop
Spark workshopSpark workshop
Spark workshop
 
A Tour Of Scala
A Tour Of ScalaA Tour Of Scala
A Tour Of Scala
 
Metaprogramming in Scala 2.10, Eugene Burmako,
Metaprogramming  in Scala 2.10, Eugene Burmako, Metaprogramming  in Scala 2.10, Eugene Burmako,
Metaprogramming in Scala 2.10, Eugene Burmako,
 
The Why and How of Scala at Twitter
The Why and How of Scala at TwitterThe Why and How of Scala at Twitter
The Why and How of Scala at Twitter
 
Introduction to Scala for Java Developers
Introduction to Scala for Java DevelopersIntroduction to Scala for Java Developers
Introduction to Scala for Java Developers
 
All about scala
All about scalaAll about scala
All about scala
 
Scala in a nutshell by venkat
Scala in a nutshell by venkatScala in a nutshell by venkat
Scala in a nutshell by venkat
 
Intro to Functional Programming in Scala
Intro to Functional Programming in ScalaIntro to Functional Programming in Scala
Intro to Functional Programming in Scala
 
Scala fundamentals
Scala fundamentalsScala fundamentals
Scala fundamentals
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)
 
Scala test
Scala testScala test
Scala test
 
Java 7 New Features
Java 7 New FeaturesJava 7 New Features
Java 7 New Features
 
Scale up your thinking
Scale up your thinkingScale up your thinking
Scale up your thinking
 
Procedure Typing for Scala
Procedure Typing for ScalaProcedure Typing for Scala
Procedure Typing for Scala
 
Scala for scripting
Scala for scriptingScala for scripting
Scala for scripting
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
Scala in Practice
Scala in PracticeScala in Practice
Scala in Practice
 

Destacado

Buildstore Pres Lorraine
Buildstore Pres LorraineBuildstore Pres Lorraine
Buildstore Pres Lorrainespikeytrim
 
Onnivori vegetariani vegani il dibattito è aperto 19 aprile 2012
Onnivori vegetariani vegani il dibattito è aperto 19 aprile 2012Onnivori vegetariani vegani il dibattito è aperto 19 aprile 2012
Onnivori vegetariani vegani il dibattito è aperto 19 aprile 2012bomber87
 
Mangiare bene fa male e 21 marzo 2012 light
Mangiare bene fa male e 21 marzo 2012 lightMangiare bene fa male e 21 marzo 2012 light
Mangiare bene fa male e 21 marzo 2012 lightbomber87
 
Dove Evolution
Dove EvolutionDove Evolution
Dove EvolutionFawio
 
Blogger2008
Blogger2008Blogger2008
Blogger2008Fawio
 
Subversive talk - Eclipse Summit Europe 2008
Subversive talk - Eclipse Summit Europe 2008Subversive talk - Eclipse Summit Europe 2008
Subversive talk - Eclipse Summit Europe 2008guestee71f
 

Destacado (7)

Buildstore Pres Lorraine
Buildstore Pres LorraineBuildstore Pres Lorraine
Buildstore Pres Lorraine
 
Onnivori vegetariani vegani il dibattito è aperto 19 aprile 2012
Onnivori vegetariani vegani il dibattito è aperto 19 aprile 2012Onnivori vegetariani vegani il dibattito è aperto 19 aprile 2012
Onnivori vegetariani vegani il dibattito è aperto 19 aprile 2012
 
Mangiare bene fa male e 21 marzo 2012 light
Mangiare bene fa male e 21 marzo 2012 lightMangiare bene fa male e 21 marzo 2012 light
Mangiare bene fa male e 21 marzo 2012 light
 
Dove Evolution
Dove EvolutionDove Evolution
Dove Evolution
 
Blogger2008
Blogger2008Blogger2008
Blogger2008
 
Subversive talk - Eclipse Summit Europe 2008
Subversive talk - Eclipse Summit Europe 2008Subversive talk - Eclipse Summit Europe 2008
Subversive talk - Eclipse Summit Europe 2008
 
Metsloomad
MetsloomadMetsloomad
Metsloomad
 

Similar a Scala coated JVM

(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?Tomasz Wrobel
 
From Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risksFrom Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risksSeniorDevOnly
 
Introduction to scala
Introduction to scalaIntroduction to scala
Introduction to scalaMichel Perez
 
Functional Object-Oriented Imperative Scala / 関数型オブジェクト指向命令型 Scala by Sébasti...
Functional Object-Oriented Imperative Scala / 関数型オブジェクト指向命令型 Scala by Sébasti...Functional Object-Oriented Imperative Scala / 関数型オブジェクト指向命令型 Scala by Sébasti...
Functional Object-Oriented Imperative Scala / 関数型オブジェクト指向命令型 Scala by Sébasti...scalaconfjp
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)Pavlo Baron
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scalashinolajla
 
Clojure - A new Lisp
Clojure - A new LispClojure - A new Lisp
Clojure - A new Lispelliando dias
 
Intro to scala
Intro to scalaIntro to scala
Intro to scalaJoe Zulli
 
An introduction to scala
An introduction to scalaAn introduction to scala
An introduction to scalaXing
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Jesper Kamstrup Linnet
 
Scala uma poderosa linguagem para a jvm
Scala   uma poderosa linguagem para a jvmScala   uma poderosa linguagem para a jvm
Scala uma poderosa linguagem para a jvmIsaias Barroso
 

Similar a Scala coated JVM (20)

Scala ntnu
Scala ntnuScala ntnu
Scala ntnu
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
 
From Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risksFrom Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risks
 
Introduction to scala
Introduction to scalaIntroduction to scala
Introduction to scala
 
Coding in Style
Coding in StyleCoding in Style
Coding in Style
 
An introduction to scala
An introduction to scalaAn introduction to scala
An introduction to scala
 
Functional Object-Oriented Imperative Scala / 関数型オブジェクト指向命令型 Scala by Sébasti...
Functional Object-Oriented Imperative Scala / 関数型オブジェクト指向命令型 Scala by Sébasti...Functional Object-Oriented Imperative Scala / 関数型オブジェクト指向命令型 Scala by Sébasti...
Functional Object-Oriented Imperative Scala / 関数型オブジェクト指向命令型 Scala by Sébasti...
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scala
 
Clojure - A new Lisp
Clojure - A new LispClojure - A new Lisp
Clojure - A new Lisp
 
SDC - Einführung in Scala
SDC - Einführung in ScalaSDC - Einführung in Scala
SDC - Einführung in Scala
 
Intro to scala
Intro to scalaIntro to scala
Intro to scala
 
An introduction to scala
An introduction to scalaAn introduction to scala
An introduction to scala
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Scala uma poderosa linguagem para a jvm
Scala   uma poderosa linguagem para a jvmScala   uma poderosa linguagem para a jvm
Scala uma poderosa linguagem para a jvm
 

Último

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 

Último (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 

Scala coated JVM

  • 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)
  • 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.
  • 23.
  • 24. 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/