SlideShare ist ein Scribd-Unternehmen logo
1 von 48
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.
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

Weitere ähnliche Inhalte

Was ist angesagt?

Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesTomer Gabel
 
Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010JUG Lausanne
 
Data structures in scala
Data structures in scalaData structures in scala
Data structures in scalaMeetu Maltiar
 
Demystifying functional programming with Scala
Demystifying functional programming with ScalaDemystifying functional programming with Scala
Demystifying functional programming with ScalaDenis
 
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...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...Sanjeev_Knoldus
 
Introduction to functional programming using Ocaml
Introduction to functional programming using OcamlIntroduction to functional programming using Ocaml
Introduction to functional programming using Ocamlpramode_ce
 
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
 
Data Structures In Scala
Data Structures In ScalaData Structures In Scala
Data Structures In ScalaKnoldus Inc.
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
Functions In Scala
Functions In Scala Functions In Scala
Functions In Scala Knoldus Inc.
 
O caml2014 leroy-slides
O caml2014 leroy-slidesO caml2014 leroy-slides
O caml2014 leroy-slidesOCaml
 
Scala Functional Patterns
Scala Functional PatternsScala Functional Patterns
Scala Functional Patternsleague
 
Scala Collections : Java 8 on Steroids
Scala Collections : Java 8 on SteroidsScala Collections : Java 8 on Steroids
Scala Collections : Java 8 on SteroidsFrançois Garillot
 
Scala at GenevaJUG by Iulian Dragos
Scala at GenevaJUG by Iulian DragosScala at GenevaJUG by Iulian Dragos
Scala at GenevaJUG by Iulian DragosGenevaJUG
 
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
 

Was ist angesagt? (19)

Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type Classes
 
Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010
 
Data structures in scala
Data structures in scalaData structures in scala
Data structures in scala
 
Demystifying functional programming with Scala
Demystifying functional programming with ScalaDemystifying functional programming with Scala
Demystifying functional programming with Scala
 
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...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
 
Scala 2013 review
Scala 2013 reviewScala 2013 review
Scala 2013 review
 
Introduction to functional programming using Ocaml
Introduction to functional programming using OcamlIntroduction to functional programming using Ocaml
Introduction to functional programming using Ocaml
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)
 
Data Structures In Scala
Data Structures In ScalaData Structures In Scala
Data Structures In Scala
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
Functions In Scala
Functions In Scala Functions In Scala
Functions In Scala
 
O caml2014 leroy-slides
O caml2014 leroy-slidesO caml2014 leroy-slides
O caml2014 leroy-slides
 
Joy of scala
Joy of scalaJoy of scala
Joy of scala
 
Scala Functional Patterns
Scala Functional PatternsScala Functional Patterns
Scala Functional Patterns
 
Meet scala
Meet scalaMeet scala
Meet scala
 
Suit case class
Suit case classSuit case class
Suit case class
 
Scala Collections : Java 8 on Steroids
Scala Collections : Java 8 on SteroidsScala Collections : Java 8 on Steroids
Scala Collections : Java 8 on Steroids
 
Scala at GenevaJUG by Iulian Dragos
Scala at GenevaJUG by Iulian DragosScala at GenevaJUG by Iulian Dragos
Scala at GenevaJUG by Iulian Dragos
 
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?
 

Ähnlich wie An introduction to scala

Scala or functional programming from a python developer's perspective
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
 
Principles of functional progrmming in scala
Principles of functional progrmming in scalaPrinciples of functional progrmming in scala
Principles of functional progrmming in scalaehsoon
 
(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
 
Lecture 5: Functional Programming
Lecture 5: Functional ProgrammingLecture 5: Functional Programming
Lecture 5: Functional ProgrammingEelco Visser
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Languageleague
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With ScalaMeetu Maltiar
 
Extractors & Implicit conversions
Extractors & Implicit conversionsExtractors & Implicit conversions
Extractors & Implicit conversionsKnoldus Inc.
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With ScalaKnoldus Inc.
 
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
 
Scala: Functioneel programmeren in een object georiënteerde wereld
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 wereldWerner Hofstra
 
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
 

Ähnlich wie An introduction to scala (20)

Scala or functional programming from a python developer's perspective
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
 
Principles of functional progrmming in scala
Principles of functional progrmming in scalaPrinciples of functional progrmming in scala
Principles of functional progrmming in scala
 
(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?
 
Lecture 5: Functional Programming
Lecture 5: Functional ProgrammingLecture 5: Functional Programming
Lecture 5: Functional Programming
 
Scala
ScalaScala
Scala
 
Scala coated JVM
Scala coated JVMScala coated JVM
Scala coated JVM
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Language
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
 
Extractors & Implicit conversions
Extractors & Implicit conversionsExtractors & Implicit conversions
Extractors & Implicit conversions
 
SDC - Einführung in Scala
SDC - Einführung in ScalaSDC - Einführung in Scala
SDC - Einführung in Scala
 
Coding in Style
Coding in StyleCoding in Style
Coding in Style
 
Spark workshop
Spark workshopSpark workshop
Spark workshop
 
Scala Introduction
Scala IntroductionScala Introduction
Scala Introduction
 
C# programming
C# programming C# programming
C# programming
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With 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
 
Scala: Functioneel programmeren in een object georiënteerde wereld
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
 
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
 

Kürzlich hochgeladen

How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Kaya Weers
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Nikki Chapple
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesBernd Ruecker
 

Kürzlich hochgeladen (20)

How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
 

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.
  • 35.
  • 36.
  • 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