An introduction to scala

Mohsen Zainalpour
Scala for Java Developers

                 Mohsen Zainalpour
             zainalpour@yahoo.com
Goals
• Have an understanding of what Scala is

• Have an interest in learning more

• Go install Scala!
What is Scala?
•   A general purpose programming language
•   A language that runs on the Java VM
•   Statically typed
•   An object-oriented language
•   No primitives or operators, but with singletons
•   A functional language
•   A scalable language
•   Java compatible
Scala History … 1995 to 2013
Brief history of Scala
•Developed by Martin Odersky
   o 1995 he learned of Java and wrote functional language that compiled to Java
     bytecode - Pizza
   o Pizza evolved into what we now recognize as Java generics
•Sun approached Odersky in 1997 to write Java 1.1 compiler
•Odersky led javac development from Java 1.1 through 1.4

•In 1999, Odersky joined EPFL to conduct research into
improving functional and OO languages
•Design of Scala began in 2001 and first release was in 2003

•Early releases of compiler written in Java
•Version 2.0.0 introduced a completely rewritten compiler in Scala

•Current version 2.10.1 released in March 2013
Is Scala a fit?
• Core Strengths
  o   Functional Programming
  o   Less boilerplate code
  o   Concurrency
  o   Java Interoperable
  o   Domain-Specific Languages
  o   XML
  o   Bridging
• Weaknesses
  o Syntax
  o Mutability
Getting up and running
Required
•Java 1.5 or greater
•Scala 2.10.1 distribution


Optional
•SBT – Simple Build Tool

•IDE Plugin
   o ScalaIDE (Eclipse – must use Helios)
   o Scala Plugin for IntelliJ IDEA
   o Scala Plugin for NetBeans
Scala cheat sheet
Definitions
Scala method definitions:     Java method definition:
def fun(x: Int): Int = {      int fun(int x) {
   result                          return result
 }                            }

def fun = result              (no parameterless methods) Java

Scala variable definitions:   variable definitions:
    var x: Int = 10                int x = 10
    val y: String = "Scala"        final String x = "Scala"
Objects and Classes
Scala Class and Object                     Java Class with statics

class Sample(x: Int, val p: Int) {         public class Sample {
  def instMeth(y: Int) = x + y               private final int x;
}                                            public final int p;

object Sample {                                Sample(int x, int p) {
  def staticMeth(x: Int, y: Int) = x * y         this.x = x;
}                                                this.p = p;
                                               }

                                               int instMeth(int y) {
                                                  return x + y;
                                               }

                                               static int staticMeth(int x, int y) {
                                                  return x * y;
                                               }
                                           }
Traits
Scala Trait                          Java Interface

trait T {                            interface T {
  var field = "!"                         int abstractMth(String x)
                                     }
 def abstractMth(x: String): Int
                                     (no concrete methods) (no fields)
  def concreteMth(x: String) = x +
field                                Java extension + implementation:
}
                                     class C extends Super implements T
Scala mixin composition:

class C extends Super with T
Constructors
class Person(val firstName: String, val lastName: String) {
 private var position: String = _
 println("Creating " + toString())

    def this(firstName: String, lastName: String, positionHeld: String) {
      this(firstName, lastName)
      position = positionHeld
    }

    override def toString(): String = {
      firstName + " " + lastName + " holds " + position + " position "
    }
}
Statics in Scala
class Marker private(val color: String) {
  override def toString(): String = "marker co1or " + color
}

object Marker {
 private val markers = Map(
   "red" -> new Marker("red"),
   "b1ue" -> new Marker("blue"),
   "green" -> new Marker("green")
 )

    def primaryColors = "red, green, blue"

    def apply(color: String) = if (markers.contains(color)) markers(color) else null
}

object MarkerTest extends App {
  println("Primary co1ors are · " + Marker.primaryColors)
  println(Marker("blue"))
  println(Marker("red"))
}
Higher Order Functions
These are functions that take other functions as
parameters, or whose result is a function.

class Decorator(left: String, right: String) {
  def layout[A](x: A) = left + x.toString() + right
}

object FunTest extends App {
  def apply(f: Int => String, v: Int) = f(v)
  val decorator = new Decorator("[", "]")
  println(apply(decorator.layout, 7))
}
Currying & Partial
                         Functions
Methods may define multiple parameter lists. When a method is called with a fewer number of parameter
lists, then this will yield a function taking the missing parameter lists as its arguments.

object CurryTest extends App {

    def filter(xs: List[Int], p: Int => Boolean): List[Int] =
     if (xs.isEmpty) xs
     else if (p(xs.head))
       xs.head :: filter(xs.tail, p)
     else filter(xs.tail, p)

    def modN(n: Int)(x: Int) = ((x % n) == 0)

    val nums = List(1, 2, 3, 4, 5, 6, 7, 8)
    println(filter(nums, modN(2)))
    println(filter(nums, modN(3)))
}
Closures
You can create code blocks with variables that are not bound.
You will have to bind them before you can invoke the function; however,
they could bind to, or close over, variables outside of their local scope
and parameter list.
That’s why they’re called closures.

•A closure allows a function to access variables outside its immediate lexical
scope.

    Val outer:String=“scala”
    Val f:( String => Unit) = { (x:String) => println(x + “ “ + outer)
Traits
They are fundamental unit for code reuse in Scala
A Trait encapsulates method and field definitions, which can be
reused by mixing them in classes
Unlike class inheritance , in which class must inherit from just one
superclass, a class may mix in any number of Traits

Unlike Interfaces they can have concrete methods
import scala.collection.mutable.ArrayBuffer   //Mixing traits in type definition
                                              class DoublePlusOneQueue extends IntQueueImpl with
//Type Definition                             Incrementing with Doubling
abstract class IntQueue {
 def get(): Int                               object QueueWithTraits {
                                               def main(args: Array[String]) {
    def put(x: Int)                             val queue1 = new DoublePlusOneQueue
                                                queue1 put 1
    def size(): Int                             queue1 put 2
}                                               println(queue1 get)
                                                println(queue1 get)
//ArrayBuffer implementation
class IntQueueImpl extends IntQueue {                 //Mixing traits in object instantiation
 private val buf = new ArrayBuffer[Int]               val queue2 = new IntQueueImpl with Filtering
                                                      queue2 put -1
    def get = buf remove 0                            queue2 put 1
                                                      println(queue2 size)
    def put(x: Int) {                             }
      buf += x                                }
    }

    def size = buf length
}

trait Doubling extends IntQueue {
  abstract override def put(x: Int) {
    super.put(2 * x)
  }
}

trait Incrementing extends IntQueue {
  abstract override def put(x: Int) {
    super.put(x + 1)
  }
}

trait Filtering extends IntQueue {
  abstract override def put(x: Int) {
    if (x > 0) super.put(x)
  }
}
Pattern Matching
Scala has a built-in general pattern matching mechanism. It
allows to match on any sort of data with a first-match policy. 

object MatchTest2 extends App {
 def matchTest(x: Any): Any = x match {
   case 1 => "one"
   case "two" => 2
   case y: Int => "scala.Int"
 }

    println(matchTest("two"))
    println(matchTest(100))
}
Collections
Basic Data Structures
•Lists
       val numbers = List(1, 2, 3, 4)
•Sets
       Set(1, 1, 2)
•Tuple
       val hostPort = ("localhost", 80)
•Maps
       Map(1 -> 2)

Functional Combinators
•Map
     scala> numbers.map((i: Int) => i * 2)
     res0: List[Int] = List(2, 4, 6, 8)
•Foreach
     numbers.foreach((i: Int) => i * 2)
Collections
•   Filter
      scala> numbers.filter((i: Int) => i % 2 == 0)
      res0: List[Int] = List(2, 4)
•   Zip
      scala> List(1, 2, 3).zip(List("a", "b", "c"))
      res0: List[(Int, String)] = List((1,a), (2,b), (3,c))
•   Partition
      scala> val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

      scala> numbers.partition(_ %2 == 0)
      res0: (List[Int], List[Int]) = (List(2, 4, 6, 8, 10),List(1, 3, 5, 7, 9))
•   Find
      scala> numbers.find((i: Int) => i > 5)
      res0: Option[Int] = Some(6)
•   drop and dropWhile
      scala> numbers.drop(5)
      res0: List[Int] = List(6, 7, 8, 9, 10)
Collections
•   foldRight and foldLeft
     scala> numbers.foldLeft(0)((m: Int, n: Int) => m + n)
     res0: Int = 55

    scala> numbers.foldLeft(0) { (m: Int, n: Int) => println("m: " + m + " n: " + n); m + n }
    m: 0 n: 1
    m: 1 n: 2
    m: 3 n: 3
    m: 6 n: 4
    m: 10 n: 5
    m: 15 n: 6
    m: 21 n: 7
    m: 28 n: 8
    m: 36 n: 9
    m: 45 n: 10

    res0: Int = 55
Collections
•   Flatten
     scala> List(List(1, 2), List(3, 4)).flatten
     res0: List[Int] = List(1, 2, 3, 4)
•   flatMap
     scala> val nestedNumbers = List(List(1, 2), List(3, 4))
     nestedNumbers: List[List[Int]] = List(List(1, 2), List(3, 4))

    scala> nestedNumbers.flatMap(x => x.map(_ * 2))
    res0: List[Int] = List(2, 4, 6, 8)
•   Generalized functional combinators
every functional combinator shown above can be written on top of fold.


     def ourMap(numbers: List[Int], fn: Int => Int): List[Int] = { numbers.foldRight(List[Int]()) { (x:
     Int, xs: List[Int]) => fn(x) :: xs } }

     scala> ourMap(numbers, timesTwo(_)) res0: List[Int] = List(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)
Scala Features
Functional programming
• The functional paradigm expresses programs as
  functions in the mathematical sense, mapping from
  one value to another (f (x) = y)
• without side effects, such as :
   o   maintaining the status of objects
   o   input/output of data
• Features such as :
   o  immutable values
   o  collections
   o  higher-order functions
   o  pattern matching 
   encourages Scala developers to use the functional style.
a program that prints the thousandth element of the Fibonacci sequence can be written as
follows in Java:

import java.math.BigInteger;
 
public class FiboJava {
  private static BigInteger fibo(int x) {
    BigInteger a = BigInteger.ZERO;
    BigInteger b = BigInteger.ONE;
    BigInteger c = BigInteger.ZERO;
    for (int i = 0; i < x; i++) {
      c = a.add(b);
      a = b;
      b = c;
    }
    return a;
  }
 
  public static void main(String args[]) {
    System.out.println(fibo(1000));
  }
}
a more compact and functional version, using infinite
sequences and tuples can be very different:

import scala.math.BigInt

object Main extends App {

    val fibs: Stream[BigInt] =
    BigInt(0) #:: BigInt(1) #:: fibs.zip(fibs.tail).map {
        n => n._1 + n._2
    }

    fibs take 5 foreach println
}
java 8 Lambda project
Lambda Expressions as Functions

•A lambda expression is a function literal. It defines a function with input parameters and function body.
      (String s1, String s2) -> s1.length() - s2.length();


Lambda Expression as Closures
•A closure allows a function to access variables outside its immediate lexical scope.
      String outer = "Java 8" (String s1) -> s1.length() - outer.length()


Hello Lambda Expressions, Goodbye Anonymous Inner Classes
      class UIBuilder {   
                   public UIBuilder() {
             button.addActionListener(e -> //process ActionEvent e)   
      }
      }


Higher-Order Functions as Reusable Building Blocks
When we pass a function literal to a method, we basically have a method that accepts a method. Such methods are
called higher-order functions.

def measure[T](func: => T):T = {       val start = System.nanoTime()       val result = func       val elapsed =
System.nanoTime() - start       println("The execution of this call took: %s ns".format(elapsed))       result }

def myCallback = {       Thread.sleep(1000)       "I just took a powernap" } val result = measure(myCallback);
Less boilerplate code
Some Scala features, such as :
• type inference
• unchecked exceptions
• optional objects
• implicit conversions
 can greatly reduce the amount of statements and checks in a program, without
changing its meaning.
In Java:
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.Enumeration;

public class ListMACsJava {
  public static void main(String[] args) throws SocketException {
    Enumeration<NetworkInterface> nics =
NetworkInterface.getNetworkInterfaces();
    for (NetworkInterface nic : Collections.list(nics)) {
       byte[] mac = nic.getHardwareAddress();
       for (int i = 0; mac != null && i < mac.length; i++) {
         System.out.format("%2x", mac[i]);
         System.out.print(i == mac.length - 1 ? 'n' : ':');
       }
    }
  }
}
In Scala:
import java.net.NetworkInterface
import scala.collection.JavaConversions._

object ListMACsScala {
  def main(args: Array[String]) {
    NetworkInterface
     .getNetworkInterfaces
     .flatMap(nic => Option(nic.getHardwareAddress))
     .map(_ map ("%02x" format _) mkString ":")
     .foreach(println(_))
  }
}
Or, another implementation in Scala using sequence
comprehension :

import java.net.NetworkInterface
import scala.collection.JavaConversions._

object ListMACsScala {
  def main(args: Array[String]) {
    val nicaddresses = for {
      nic <- NetworkInterface.getNetworkInterfaces
      addrbytes <- Option(nic.getHardwareAddress)
    } yield {
      addrbytes map {
        "%02x" format _
      } mkString ":"
    }
    nicaddresses foreach println
  }
}
Does Scala offer better concurrency?
“If it hurts, stop doing it” is a doctor’s good advice. In
concurrent program-ming, shared mutability is “it.”

Shared mutability—the root of concurrency roblems—
is where multiple threads can modify a variable.

Solution :

•Immutability
•Actors
Immutability
• Synchronizing access to shared mutable objects
  can result in much complexity in the use of
  concurrency primitive (locks, semaphores, etc.).
• Scala tries to mitigate this problem by using
  immutable objects and pure functions
• If an object is immutable, it can be shared or
  copied without worrying about who is using it, so it is
  naturally "thread-safe."
Actors
• Using Low-level parallelism controls, such as locks
  and synchronized blocks may not be easy.

• to write this type of program more productively and
  prevent defects, a high level concurrency control is
  very desirable.
   Such abstraction can be like
  o Fork /Join
  o Software Transactional Memory
  o or, as featured in Scala, the Actor Model.
   In Actor Model, the parallelism is expressed as actors reacting to messages,
  rather than locking and releasing of threads.
An introduction to scala
An introduction to scala
The following example demonstrates actors estimating the value of Pi using the Monte Carlo method.

import scala.util.Random
import Math._
import scala.actors.Actor

case object Calculate

case object ShutDown

class Calculator extends Actor {
 val rand = new Random
 var in, cnt = 1.0

    def act {
      while (true) {
        receive {
          case Calculate =>
           sender ! estimativeOfPi
          case ShutDown => exit
        }
      }
    }

    def estimativeOfPi: Double = {
      val x = rand.nextDouble - 0.5
      val y = rand.nextDouble - 0.5
      cnt += 1.0
      if (sqrt(x * x + y * y) < 0.5) in += 1
      in / cnt * 4
    }
}
The "coordinator" starts a list of calculators and tell them to calculate until any of them produces an accurate enough estimation

import actors.Actor

class Coordinator(numOfCalculators: Int) extends Actor {
  def act {
    val startedAt = System.currentTimeMillis()
    var calculators = List.fill(numOfCalculators)(new Calculator)
    calculators.foreach(c => {
      c.start
      c ! Calculate
    })
    while (true) {
      receive {
        case estimative: Double =>
         val error = Math.abs(Math.PI - estimative)
         if (error > 0.0000001)
           sender ! Calculate
         else {
           val tempo = System.currentTimeMillis() - startedAt
           calculators.foreach(_ ! ShutDown)
           println("Pi found by " + sender + " = " + estimative)
           println("Execution time: " + tempo)
           exit
         }
      }
    }
  }
}


object PiActors extends App {
  new Coordinator(2) start
}
Parallel Collections
Scala 2.9 introduced parallel collections, which makes it easy to parallelize the
execution of common collections operations, such as map(), filter () and
foreach(). The par() method can be used to obtain a parallel version of the
collection



object ParCol extends App {
  (1 to 5) foreach println
  (1 to 5).par foreach println
}
XML
An interesting combination in Scala is the XML syntax in combination
with pattern matching.

val movies = <movies>
  <movie>The Incredibles</movie>
  <movie>WALL E</movie>
  <short>Jack Jack Attack</short>
  <short>Geri's Game</short>
</movies>
(movies  "_").foreach {
  movie =>
   movie match {
     case <movie>{movieName}</movie> => println(movieName)
     case <short>{shortName}</short> => println(shortName + " (short)")
   }
}
Is Scala extensible?
Scala allows developers to customize the look and
feel of the language, creating new languages and
altering the compilation process.
Domain Specific Languages
Using generic classes, abstract types, functions as objects, methods named as
operators and implicit conversions, Scala code can become a domain specific
language (DSL).

Domain-specific languages can also be created when a more abstract and
declarative language is needed by developers. For example, Apache Camel offers a
Scala DSL to make the configuration of service routes more concise and correct.

 
"direct:a" ==> {
  to ("mock:polyglot")
  choice {
     when (_.in == "<hello/>") to ("mock:english")
     when (_.in == "<hallo/>") {
       to ("mock:dutch")
       to ("mock:german")
     }
     otherwise to ("mock:french")
   }
}
Changing the compilation
• allowing the creation of entirely new grammars with
  parser combinators.
• create compiler plugins to change the build
  process.
  These plugins could be written, for :
  operform static code analysis .
  oevaluating metrics, like PMD or FindBugs.
Are Scala and Java
           interoperable?
• it is possible to invoke Java methods in Scala code and
  vice versa.
• When calling Scala methods from Java, the developer
  needs to understand how the features that do not exist
  in Java. For example:
   o   methods with non-alphabetic names
   o   receiving functions
   o   tuples as parameters
• When invoking Java from Scala, the problems is the
  features of Java that were abandoned or implemented
  differently in Scala, such as :
   o interfaces
   o annotations
   o collections
Using Java Libraries and
        Frameworks in Scala
any library or framework available for Java can be used in Scala. This includes all Java
EE (EJB, JSF, JAX-RS, etc.) and popular libraries, such as Hibernate and Junit.

import javax.servlet.http._
import javax.servlet.annotation.WebServlet

import scala.collection.JavaConversions._

@WebServlet(Array("/printParams"))
class PrintParametersServlet extends HttpServlet {
  override def

    doGet(req:HttpServletRequest, resp:HttpServletResponse) {
      val out = resp.getWriter req.getParameterMap.map {
         case (key,value)=>key + " = " + value.mkString(",")
      }.foreach(out println _)
    }
}
Final Thoughts
Scala is appropriate for an application that has
significant scalability requirements that requires
concurrency
Q&A
Thank You
zainalpour@yahoo.com
1 von 48

Recomendados

Scala Bootcamp 1 von
Scala Bootcamp 1Scala Bootcamp 1
Scala Bootcamp 1Knoldus Inc.
1.7K views41 Folien
Introducing scala von
Introducing scalaIntroducing scala
Introducing scalaMeetu Maltiar
1.8K views41 Folien
Scala categorytheory von
Scala categorytheoryScala categorytheory
Scala categorytheoryKnoldus Inc.
1.4K views22 Folien
Scala collections von
Scala collectionsScala collections
Scala collectionsInphina Technologies
2.9K views25 Folien
Getting Started With Scala von
Getting Started With ScalaGetting Started With Scala
Getting Started With ScalaXebia IT Architects
2K views35 Folien
Getting Started With Scala von
Getting Started With ScalaGetting Started With Scala
Getting Started With ScalaMeetu Maltiar
6.4K views35 Folien

Más contenido relacionado

Was ist angesagt?

Scala Back to Basics: Type Classes von
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesTomer Gabel
3.7K views35 Folien
Introduction à Scala - Michel Schinz - January 2010 von
Introduction à Scala - Michel Schinz - January 2010Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010JUG Lausanne
1.4K views62 Folien
Data structures in scala von
Data structures in scalaData structures in scala
Data structures in scalaMeetu Maltiar
3.3K views20 Folien
Demystifying functional programming with Scala von
Demystifying functional programming with ScalaDemystifying functional programming with Scala
Demystifying functional programming with ScalaDenis
536 views60 Folien
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ... von
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...Sanjeev_Knoldus
4.9K views97 Folien
Scala 2013 review von
Scala 2013 reviewScala 2013 review
Scala 2013 reviewSagie Davidovich
12.2K views76 Folien

Was ist angesagt?(19)

Scala Back to Basics: Type Classes von Tomer Gabel
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type Classes
Tomer Gabel3.7K views
Introduction à Scala - Michel Schinz - January 2010 von JUG Lausanne
Introduction à Scala - Michel Schinz - January 2010Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010
JUG Lausanne1.4K views
Data structures in scala von Meetu Maltiar
Data structures in scalaData structures in scala
Data structures in scala
Meetu Maltiar3.3K views
Demystifying functional programming with Scala von Denis
Demystifying functional programming with ScalaDemystifying functional programming with Scala
Demystifying functional programming with Scala
Denis 536 views
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ... von Sanjeev_Knoldus
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Sanjeev_Knoldus4.9K views
Introduction to functional programming using Ocaml von pramode_ce
Introduction to functional programming using OcamlIntroduction to functional programming using Ocaml
Introduction to functional programming using Ocaml
pramode_ce6K views
Pragmatic Real-World Scala (short version) von Jonas Bonér
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)
Jonas Bonér72.8K views
Data Structures In Scala von Knoldus Inc.
Data Structures In ScalaData Structures In Scala
Data Structures In Scala
Knoldus Inc.11K views
scalaliftoff2009.pdf von Hiroshi Ono
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
Hiroshi Ono1.4K views
Functions In Scala von Knoldus Inc.
Functions In Scala Functions In Scala
Functions In Scala
Knoldus Inc.2.2K views
O caml2014 leroy-slides von OCaml
O caml2014 leroy-slidesO caml2014 leroy-slides
O caml2014 leroy-slides
OCaml418.1K views
Joy of scala von Maxim Novak
Joy of scalaJoy of scala
Joy of scala
Maxim Novak13.3K views
Scala Functional Patterns von league
Scala Functional PatternsScala Functional Patterns
Scala Functional Patterns
league1.4K views
Scala at GenevaJUG by Iulian Dragos von GenevaJUG
Scala at GenevaJUG by Iulian DragosScala at GenevaJUG by Iulian Dragos
Scala at GenevaJUG by Iulian Dragos
GenevaJUG1.3K views

Similar a An introduction to scala

Scala or functional programming from a python developer's perspective von
Scala or functional programming from a python developer's perspectiveScala or functional programming from a python developer's perspective
Scala or functional programming from a python developer's perspectivegabalese
950 views25 Folien
Principles of functional progrmming in scala von
Principles of functional progrmming in scalaPrinciples of functional progrmming in scala
Principles of functional progrmming in scalaehsoon
428 views49 Folien
(How) can we benefit from adopting scala? von
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?Tomasz Wrobel
1.1K views69 Folien
Lecture 5: Functional Programming von
Lecture 5: Functional ProgrammingLecture 5: Functional Programming
Lecture 5: Functional ProgrammingEelco Visser
1.1K views90 Folien
Scala von
ScalaScala
Scalasuraj_atreya
778 views21 Folien
Scala coated JVM von
Scala coated JVMScala coated JVM
Scala coated JVMStuart Roebuck
999 views24 Folien

Similar a An introduction to scala(20)

Scala or functional programming from a python developer's perspective von gabalese
Scala or functional programming from a python developer's perspectiveScala or functional programming from a python developer's perspective
Scala or functional programming from a python developer's perspective
gabalese950 views
Principles of functional progrmming in scala von ehsoon
Principles of functional progrmming in scalaPrinciples of functional progrmming in scala
Principles of functional progrmming in scala
ehsoon428 views
(How) can we benefit from adopting scala? von Tomasz Wrobel
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
Tomasz Wrobel1.1K views
Lecture 5: Functional Programming von Eelco Visser
Lecture 5: Functional ProgrammingLecture 5: Functional Programming
Lecture 5: Functional Programming
Eelco Visser1.1K views
The Scala Programming Language von league
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Language
league1.4K views
Getting Started With Scala von Meetu Maltiar
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
Meetu Maltiar1.3K views
Extractors & Implicit conversions von Knoldus Inc.
Extractors & Implicit conversionsExtractors & Implicit conversions
Extractors & Implicit conversions
Knoldus Inc.2.1K views
Coding in Style von scalaconfjp
Coding in StyleCoding in Style
Coding in Style
scalaconfjp1.4K views
Functional Programming With Scala von Knoldus Inc.
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
Knoldus Inc.802 views
From Java to Scala - advantages and possible risks von SeniorDevOnly
From Java to Scala - advantages and possible risksFrom Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risks
SeniorDevOnly593 views
Scala: Functioneel programmeren in een object georiënteerde wereld von Werner Hofstra
Scala: Functioneel programmeren in een object georiënteerde wereldScala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereld
Werner Hofstra378 views
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf von Hiroshi Ono
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono370 views
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf von Hiroshi Ono
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono406 views
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf von Hiroshi Ono
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono524 views

Último

Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De... von
Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De...Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De...
Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De...Moses Kemibaro
35 views38 Folien
Transcript: Redefining the book supply chain: A glimpse into the future - Tec... von
Transcript: Redefining the book supply chain: A glimpse into the future - Tec...Transcript: Redefining the book supply chain: A glimpse into the future - Tec...
Transcript: Redefining the book supply chain: A glimpse into the future - Tec...BookNet Canada
41 views16 Folien
How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ... von
How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ...How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ...
How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ...ShapeBlue
171 views28 Folien
Enabling DPU Hardware Accelerators in XCP-ng Cloud Platform Environment - And... von
Enabling DPU Hardware Accelerators in XCP-ng Cloud Platform Environment - And...Enabling DPU Hardware Accelerators in XCP-ng Cloud Platform Environment - And...
Enabling DPU Hardware Accelerators in XCP-ng Cloud Platform Environment - And...ShapeBlue
108 views12 Folien
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue von
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlueVNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlueShapeBlue
207 views54 Folien
Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ... von
Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ...Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ...
Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ...ShapeBlue
120 views17 Folien

Último(20)

Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De... von Moses Kemibaro
Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De...Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De...
Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De...
Moses Kemibaro35 views
Transcript: Redefining the book supply chain: A glimpse into the future - Tec... von BookNet Canada
Transcript: Redefining the book supply chain: A glimpse into the future - Tec...Transcript: Redefining the book supply chain: A glimpse into the future - Tec...
Transcript: Redefining the book supply chain: A glimpse into the future - Tec...
BookNet Canada41 views
How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ... von ShapeBlue
How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ...How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ...
How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ...
ShapeBlue171 views
Enabling DPU Hardware Accelerators in XCP-ng Cloud Platform Environment - And... von ShapeBlue
Enabling DPU Hardware Accelerators in XCP-ng Cloud Platform Environment - And...Enabling DPU Hardware Accelerators in XCP-ng Cloud Platform Environment - And...
Enabling DPU Hardware Accelerators in XCP-ng Cloud Platform Environment - And...
ShapeBlue108 views
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue von ShapeBlue
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlueVNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue
ShapeBlue207 views
Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ... von ShapeBlue
Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ...Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ...
Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ...
ShapeBlue120 views
CloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlue von ShapeBlue
CloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlueCloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlue
CloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlue
ShapeBlue137 views
CloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlue von ShapeBlue
CloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlueCloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlue
CloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlue
ShapeBlue139 views
What’s New in CloudStack 4.19 - Abhishek Kumar - ShapeBlue von ShapeBlue
What’s New in CloudStack 4.19 - Abhishek Kumar - ShapeBlueWhat’s New in CloudStack 4.19 - Abhishek Kumar - ShapeBlue
What’s New in CloudStack 4.19 - Abhishek Kumar - ShapeBlue
ShapeBlue265 views
Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda... von ShapeBlue
Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda...Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda...
Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda...
ShapeBlue164 views
Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha... von ShapeBlue
Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha...Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha...
Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha...
ShapeBlue183 views
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f... von TrustArc
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...
TrustArc176 views
Business Analyst Series 2023 - Week 4 Session 8 von DianaGray10
Business Analyst Series 2023 -  Week 4 Session 8Business Analyst Series 2023 -  Week 4 Session 8
Business Analyst Series 2023 - Week 4 Session 8
DianaGray10145 views
"Running students' code in isolation. The hard way", Yurii Holiuk von Fwdays
"Running students' code in isolation. The hard way", Yurii Holiuk "Running students' code in isolation. The hard way", Yurii Holiuk
"Running students' code in isolation. The hard way", Yurii Holiuk
Fwdays36 views
KVM Security Groups Under the Hood - Wido den Hollander - Your.Online von ShapeBlue
KVM Security Groups Under the Hood - Wido den Hollander - Your.OnlineKVM Security Groups Under the Hood - Wido den Hollander - Your.Online
KVM Security Groups Under the Hood - Wido den Hollander - Your.Online
ShapeBlue225 views
Redefining the book supply chain: A glimpse into the future - Tech Forum 2023 von BookNet Canada
Redefining the book supply chain: A glimpse into the future - Tech Forum 2023Redefining the book supply chain: A glimpse into the future - Tech Forum 2023
Redefining the book supply chain: A glimpse into the future - Tech Forum 2023
BookNet Canada44 views

An introduction to scala

  • 1. Scala for Java Developers Mohsen Zainalpour zainalpour@yahoo.com
  • 2. Goals • Have an understanding of what Scala is • Have an interest in learning more • Go install Scala!
  • 3. What is Scala? • A general purpose programming language • A language that runs on the Java VM • Statically typed • An object-oriented language • No primitives or operators, but with singletons • A functional language • A scalable language • Java compatible
  • 4. Scala History … 1995 to 2013 Brief history of Scala •Developed by Martin Odersky o 1995 he learned of Java and wrote functional language that compiled to Java bytecode - Pizza o Pizza evolved into what we now recognize as Java generics •Sun approached Odersky in 1997 to write Java 1.1 compiler •Odersky led javac development from Java 1.1 through 1.4 •In 1999, Odersky joined EPFL to conduct research into improving functional and OO languages •Design of Scala began in 2001 and first release was in 2003 •Early releases of compiler written in Java •Version 2.0.0 introduced a completely rewritten compiler in Scala •Current version 2.10.1 released in March 2013
  • 5. Is Scala a fit? • Core Strengths o Functional Programming o Less boilerplate code o Concurrency o Java Interoperable o Domain-Specific Languages o XML o Bridging • Weaknesses o Syntax o Mutability
  • 6. Getting up and running Required •Java 1.5 or greater •Scala 2.10.1 distribution Optional •SBT – Simple Build Tool •IDE Plugin o ScalaIDE (Eclipse – must use Helios) o Scala Plugin for IntelliJ IDEA o Scala Plugin for NetBeans
  • 8. Definitions Scala method definitions: Java method definition: def fun(x: Int): Int = { int fun(int x) { result return result } } def fun = result (no parameterless methods) Java Scala variable definitions: variable definitions: var x: Int = 10 int x = 10 val y: String = "Scala" final String x = "Scala"
  • 9. Objects and Classes Scala Class and Object Java Class with statics class Sample(x: Int, val p: Int) { public class Sample { def instMeth(y: Int) = x + y private final int x; } public final int p; object Sample { Sample(int x, int p) { def staticMeth(x: Int, y: Int) = x * y this.x = x; } this.p = p; } int instMeth(int y) { return x + y; } static int staticMeth(int x, int y) { return x * y; } }
  • 10. Traits Scala Trait Java Interface trait T { interface T { var field = "!" int abstractMth(String x) } def abstractMth(x: String): Int (no concrete methods) (no fields) def concreteMth(x: String) = x + field Java extension + implementation: } class C extends Super implements T Scala mixin composition: class C extends Super with T
  • 11. Constructors class Person(val firstName: String, val lastName: String) { private var position: String = _ println("Creating " + toString()) def this(firstName: String, lastName: String, positionHeld: String) { this(firstName, lastName) position = positionHeld } override def toString(): String = { firstName + " " + lastName + " holds " + position + " position " } }
  • 12. Statics in Scala class Marker private(val color: String) { override def toString(): String = "marker co1or " + color } object Marker { private val markers = Map( "red" -> new Marker("red"), "b1ue" -> new Marker("blue"), "green" -> new Marker("green") ) def primaryColors = "red, green, blue" def apply(color: String) = if (markers.contains(color)) markers(color) else null } object MarkerTest extends App { println("Primary co1ors are · " + Marker.primaryColors) println(Marker("blue")) println(Marker("red")) }
  • 13. Higher Order Functions These are functions that take other functions as parameters, or whose result is a function. class Decorator(left: String, right: String) { def layout[A](x: A) = left + x.toString() + right } object FunTest extends App { def apply(f: Int => String, v: Int) = f(v) val decorator = new Decorator("[", "]") println(apply(decorator.layout, 7)) }
  • 14. Currying & Partial Functions Methods may define multiple parameter lists. When a method is called with a fewer number of parameter lists, then this will yield a function taking the missing parameter lists as its arguments. object CurryTest extends App { def filter(xs: List[Int], p: Int => Boolean): List[Int] = if (xs.isEmpty) xs else if (p(xs.head)) xs.head :: filter(xs.tail, p) else filter(xs.tail, p) def modN(n: Int)(x: Int) = ((x % n) == 0) val nums = List(1, 2, 3, 4, 5, 6, 7, 8) println(filter(nums, modN(2))) println(filter(nums, modN(3))) }
  • 15. Closures You can create code blocks with variables that are not bound. You will have to bind them before you can invoke the function; however, they could bind to, or close over, variables outside of their local scope and parameter list. That’s why they’re called closures. •A closure allows a function to access variables outside its immediate lexical scope. Val outer:String=“scala” Val f:( String => Unit) = { (x:String) => println(x + “ “ + outer)
  • 16. Traits They are fundamental unit for code reuse in Scala A Trait encapsulates method and field definitions, which can be reused by mixing them in classes Unlike class inheritance , in which class must inherit from just one superclass, a class may mix in any number of Traits Unlike Interfaces they can have concrete methods
  • 17. import scala.collection.mutable.ArrayBuffer //Mixing traits in type definition class DoublePlusOneQueue extends IntQueueImpl with //Type Definition Incrementing with Doubling abstract class IntQueue { def get(): Int object QueueWithTraits { def main(args: Array[String]) { def put(x: Int) val queue1 = new DoublePlusOneQueue queue1 put 1 def size(): Int queue1 put 2 } println(queue1 get) println(queue1 get) //ArrayBuffer implementation class IntQueueImpl extends IntQueue { //Mixing traits in object instantiation private val buf = new ArrayBuffer[Int] val queue2 = new IntQueueImpl with Filtering queue2 put -1 def get = buf remove 0 queue2 put 1 println(queue2 size) def put(x: Int) { } buf += x } } def size = buf length } trait Doubling extends IntQueue { abstract override def put(x: Int) { super.put(2 * x) } } trait Incrementing extends IntQueue { abstract override def put(x: Int) { super.put(x + 1) } } trait Filtering extends IntQueue { abstract override def put(x: Int) { if (x > 0) super.put(x) } }
  • 18. Pattern Matching Scala has a built-in general pattern matching mechanism. It allows to match on any sort of data with a first-match policy.  object MatchTest2 extends App { def matchTest(x: Any): Any = x match { case 1 => "one" case "two" => 2 case y: Int => "scala.Int" } println(matchTest("two")) println(matchTest(100)) }
  • 19. Collections Basic Data Structures •Lists val numbers = List(1, 2, 3, 4) •Sets Set(1, 1, 2) •Tuple val hostPort = ("localhost", 80) •Maps Map(1 -> 2) Functional Combinators •Map scala> numbers.map((i: Int) => i * 2) res0: List[Int] = List(2, 4, 6, 8) •Foreach numbers.foreach((i: Int) => i * 2)
  • 20. Collections • Filter scala> numbers.filter((i: Int) => i % 2 == 0) res0: List[Int] = List(2, 4) • Zip scala> List(1, 2, 3).zip(List("a", "b", "c")) res0: List[(Int, String)] = List((1,a), (2,b), (3,c)) • Partition scala> val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> numbers.partition(_ %2 == 0) res0: (List[Int], List[Int]) = (List(2, 4, 6, 8, 10),List(1, 3, 5, 7, 9)) • Find scala> numbers.find((i: Int) => i > 5) res0: Option[Int] = Some(6) • drop and dropWhile scala> numbers.drop(5) res0: List[Int] = List(6, 7, 8, 9, 10)
  • 21. Collections • foldRight and foldLeft scala> numbers.foldLeft(0)((m: Int, n: Int) => m + n) res0: Int = 55 scala> numbers.foldLeft(0) { (m: Int, n: Int) => println("m: " + m + " n: " + n); m + n } m: 0 n: 1 m: 1 n: 2 m: 3 n: 3 m: 6 n: 4 m: 10 n: 5 m: 15 n: 6 m: 21 n: 7 m: 28 n: 8 m: 36 n: 9 m: 45 n: 10 res0: Int = 55
  • 22. Collections • Flatten scala> List(List(1, 2), List(3, 4)).flatten res0: List[Int] = List(1, 2, 3, 4) • flatMap scala> val nestedNumbers = List(List(1, 2), List(3, 4)) nestedNumbers: List[List[Int]] = List(List(1, 2), List(3, 4)) scala> nestedNumbers.flatMap(x => x.map(_ * 2)) res0: List[Int] = List(2, 4, 6, 8) • Generalized functional combinators every functional combinator shown above can be written on top of fold. def ourMap(numbers: List[Int], fn: Int => Int): List[Int] = { numbers.foldRight(List[Int]()) { (x: Int, xs: List[Int]) => fn(x) :: xs } } scala> ourMap(numbers, timesTwo(_)) res0: List[Int] = List(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)
  • 24. Functional programming • The functional paradigm expresses programs as functions in the mathematical sense, mapping from one value to another (f (x) = y) • without side effects, such as : o maintaining the status of objects o input/output of data • Features such as : o immutable values o collections o higher-order functions o pattern matching  encourages Scala developers to use the functional style.
  • 25. a program that prints the thousandth element of the Fibonacci sequence can be written as follows in Java: import java.math.BigInteger;   public class FiboJava { private static BigInteger fibo(int x) { BigInteger a = BigInteger.ZERO; BigInteger b = BigInteger.ONE; BigInteger c = BigInteger.ZERO; for (int i = 0; i < x; i++) { c = a.add(b); a = b; b = c; } return a; }   public static void main(String args[]) { System.out.println(fibo(1000)); } }
  • 26. a more compact and functional version, using infinite sequences and tuples can be very different: import scala.math.BigInt object Main extends App { val fibs: Stream[BigInt] = BigInt(0) #:: BigInt(1) #:: fibs.zip(fibs.tail).map { n => n._1 + n._2 } fibs take 5 foreach println }
  • 27. java 8 Lambda project Lambda Expressions as Functions •A lambda expression is a function literal. It defines a function with input parameters and function body. (String s1, String s2) -> s1.length() - s2.length(); Lambda Expression as Closures •A closure allows a function to access variables outside its immediate lexical scope. String outer = "Java 8" (String s1) -> s1.length() - outer.length() Hello Lambda Expressions, Goodbye Anonymous Inner Classes class UIBuilder {    public UIBuilder() {       button.addActionListener(e -> //process ActionEvent e)    } } Higher-Order Functions as Reusable Building Blocks When we pass a function literal to a method, we basically have a method that accepts a method. Such methods are called higher-order functions. def measure[T](func: => T):T = {       val start = System.nanoTime()       val result = func       val elapsed = System.nanoTime() - start       println("The execution of this call took: %s ns".format(elapsed))       result } def myCallback = {       Thread.sleep(1000)       "I just took a powernap" } val result = measure(myCallback);
  • 28. Less boilerplate code Some Scala features, such as : • type inference • unchecked exceptions • optional objects • implicit conversions can greatly reduce the amount of statements and checks in a program, without changing its meaning.
  • 29. In Java: import java.net.NetworkInterface; import java.net.SocketException; import java.util.Collections; import java.util.Enumeration; public class ListMACsJava { public static void main(String[] args) throws SocketException { Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces(); for (NetworkInterface nic : Collections.list(nics)) { byte[] mac = nic.getHardwareAddress(); for (int i = 0; mac != null && i < mac.length; i++) { System.out.format("%2x", mac[i]); System.out.print(i == mac.length - 1 ? 'n' : ':'); } } } }
  • 30. In Scala: import java.net.NetworkInterface import scala.collection.JavaConversions._ object ListMACsScala { def main(args: Array[String]) { NetworkInterface .getNetworkInterfaces .flatMap(nic => Option(nic.getHardwareAddress)) .map(_ map ("%02x" format _) mkString ":") .foreach(println(_)) } }
  • 31. Or, another implementation in Scala using sequence comprehension : import java.net.NetworkInterface import scala.collection.JavaConversions._ object ListMACsScala { def main(args: Array[String]) { val nicaddresses = for { nic <- NetworkInterface.getNetworkInterfaces addrbytes <- Option(nic.getHardwareAddress) } yield { addrbytes map { "%02x" format _ } mkString ":" } nicaddresses foreach println } }
  • 32. Does Scala offer better concurrency? “If it hurts, stop doing it” is a doctor’s good advice. In concurrent program-ming, shared mutability is “it.” Shared mutability—the root of concurrency roblems— is where multiple threads can modify a variable. Solution : •Immutability •Actors
  • 33. Immutability • Synchronizing access to shared mutable objects can result in much complexity in the use of concurrency primitive (locks, semaphores, etc.). • Scala tries to mitigate this problem by using immutable objects and pure functions • If an object is immutable, it can be shared or copied without worrying about who is using it, so it is naturally "thread-safe."
  • 34. Actors • Using Low-level parallelism controls, such as locks and synchronized blocks may not be easy. • to write this type of program more productively and prevent defects, a high level concurrency control is very desirable. Such abstraction can be like o Fork /Join o Software Transactional Memory o or, as featured in Scala, the Actor Model. In Actor Model, the parallelism is expressed as actors reacting to messages, rather than locking and releasing of threads.
  • 37. The following example demonstrates actors estimating the value of Pi using the Monte Carlo method. import scala.util.Random import Math._ import scala.actors.Actor case object Calculate case object ShutDown class Calculator extends Actor { val rand = new Random var in, cnt = 1.0 def act { while (true) { receive { case Calculate => sender ! estimativeOfPi case ShutDown => exit } } } def estimativeOfPi: Double = { val x = rand.nextDouble - 0.5 val y = rand.nextDouble - 0.5 cnt += 1.0 if (sqrt(x * x + y * y) < 0.5) in += 1 in / cnt * 4 } }
  • 38. The "coordinator" starts a list of calculators and tell them to calculate until any of them produces an accurate enough estimation import actors.Actor class Coordinator(numOfCalculators: Int) extends Actor { def act { val startedAt = System.currentTimeMillis() var calculators = List.fill(numOfCalculators)(new Calculator) calculators.foreach(c => { c.start c ! Calculate }) while (true) { receive { case estimative: Double => val error = Math.abs(Math.PI - estimative) if (error > 0.0000001) sender ! Calculate else { val tempo = System.currentTimeMillis() - startedAt calculators.foreach(_ ! ShutDown) println("Pi found by " + sender + " = " + estimative) println("Execution time: " + tempo) exit } } } } } object PiActors extends App { new Coordinator(2) start }
  • 39. Parallel Collections Scala 2.9 introduced parallel collections, which makes it easy to parallelize the execution of common collections operations, such as map(), filter () and foreach(). The par() method can be used to obtain a parallel version of the collection object ParCol extends App { (1 to 5) foreach println (1 to 5).par foreach println }
  • 40. XML An interesting combination in Scala is the XML syntax in combination with pattern matching. val movies = <movies> <movie>The Incredibles</movie> <movie>WALL E</movie> <short>Jack Jack Attack</short> <short>Geri's Game</short> </movies> (movies "_").foreach { movie => movie match { case <movie>{movieName}</movie> => println(movieName) case <short>{shortName}</short> => println(shortName + " (short)") } }
  • 41. Is Scala extensible? Scala allows developers to customize the look and feel of the language, creating new languages and altering the compilation process.
  • 42. Domain Specific Languages Using generic classes, abstract types, functions as objects, methods named as operators and implicit conversions, Scala code can become a domain specific language (DSL). Domain-specific languages can also be created when a more abstract and declarative language is needed by developers. For example, Apache Camel offers a Scala DSL to make the configuration of service routes more concise and correct.   "direct:a" ==> { to ("mock:polyglot") choice { when (_.in == "<hello/>") to ("mock:english") when (_.in == "<hallo/>") { to ("mock:dutch") to ("mock:german") } otherwise to ("mock:french") } }
  • 43. Changing the compilation • allowing the creation of entirely new grammars with parser combinators. • create compiler plugins to change the build process. These plugins could be written, for : operform static code analysis . oevaluating metrics, like PMD or FindBugs.
  • 44. Are Scala and Java interoperable? • it is possible to invoke Java methods in Scala code and vice versa. • When calling Scala methods from Java, the developer needs to understand how the features that do not exist in Java. For example: o methods with non-alphabetic names o receiving functions o tuples as parameters • When invoking Java from Scala, the problems is the features of Java that were abandoned or implemented differently in Scala, such as : o interfaces o annotations o collections
  • 45. Using Java Libraries and Frameworks in Scala any library or framework available for Java can be used in Scala. This includes all Java EE (EJB, JSF, JAX-RS, etc.) and popular libraries, such as Hibernate and Junit. import javax.servlet.http._ import javax.servlet.annotation.WebServlet import scala.collection.JavaConversions._ @WebServlet(Array("/printParams")) class PrintParametersServlet extends HttpServlet { override def doGet(req:HttpServletRequest, resp:HttpServletResponse) { val out = resp.getWriter req.getParameterMap.map { case (key,value)=>key + " = " + value.mkString(",") }.foreach(out println _) } }
  • 46. Final Thoughts Scala is appropriate for an application that has significant scalability requirements that requires concurrency
  • 47. Q&A