SlideShare une entreprise Scribd logo
1  sur  37
Télécharger pour lire hors ligne
Splice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS




                                Stepping Into

       :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::



                                                    Angelo Corsaro, Ph.D.
                                                       Chief Technology Officer
                                                               PrismTech
                                                       OMG DDS SIG Co-Chair
                                                    angelo.corsaro@prismtech.com


Splice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS
Stepping Into

:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::




                                                                                           Basics
OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD
                                       What is Scala
:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::




   ‣ Scala (pronounced Skah-lah) stands for “Scalable language”
   ‣ It is a language that carefully and creatively blends Object
       Oriented and Functional constructs into a statically typed
       language with sophisticated type inference

   ‣ Scala targets the JVM and .NET runtime and is 100%
       compatible with Java
OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD
                       Why should you care?
:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::




   ‣ Scala is simple to write, extremely compact and easy to
       understand

   ‣ Scala is strongly typed with a structural type system
   ‣ Scala is an extensible language (many construct are build in
       the standard library)

   ‣ Scala makes it easy to design Domain Specific Language
OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD
                            Complex Numbers
:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::




   ‣ To explore some of the nice features of Scala, let’s see how
       we might design a Complex number class

   ‣ What we expect to be able to do is all mathematical
       operations between complex numbers as well as scalar
       multiplications and division
       ‣ [(1+i2)+2*(3-i5)*(i4)]/(1+i3)
       ‣ ~(1+i2) [conjugate]
       ‣ !(3+i4) [Modulo]
OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD
                                        Constructor
:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::



   ‣ Scala allows to implicitly create constructors and attributes
       starting from a simple argument list associated with the class
       declaration




                    class Complex(val re: Float, val im: Float)
OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD
                                                    In Java
:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::




                                  public class Complex {

                                  !   private final float re;
                                  !   private final float im;

                                  !   public Complex(float re, float im) {
                                  !   ! this.re = re;
                                  !   ! this.im = im;
                                  !   }
                                  !   public Complex(float f) {
                                  !   ! this.re = f;
                                  !   ! this.im = 0;
                                  !   }

                                  !   public float re() { return re;}

                                  !   public float im() { return im;}
OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD
                                               Methods
:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::




   ‣ Everything in Scala is a method even operators
   ‣ Methods name can be symbols such as *, !, +, etc.


           def + (c: Complex) : Complex = Complex(re + c.re, im + c.im)

 or, taking advantage of type inference....
                   def + (c: Complex) = Complex(re + c.re, im + c.im)
OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD
                                                 In Java...
:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::




                   public Complex add(Complex that) {
                   	 	 return new Complex(this.re() + that.re(),
                                          this.im() + that.im());
                   	 }
OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD
                                         As a result...
:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::




                            val result = Complex(1,2) + Complex(2,3)




                            Complex c1 = new Complex(1, 2);
                            Complex c2 = new Complex (3,4);
                            Complex c3 = c1.add(c2);
                    or...
                            Complex c3 = (new Complex(1, 2).add(new Complex (3,4))l
OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD
                            Companion Object
:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::


‣ Scala does not have the concept of static methods/attributes
‣ On the other hand it provides built-in support for Singletons, which are
   specified with the “object” keyword as opposed to the “class” keyword

‣ The companion object,
                                             object Complex {
   is the object                             	 def apply(real: Float, img: Float) = new Complex(real, img)
   associated with a                         	
   class, which shares the                   	 def apply(real: Float) = new Complex(real, 0)
                                             	
   same name and                             	 implicit def floatToReComplex (f: Float) = new ReComplex(f)
   provides typically                        	
                                             	 implicit def intToReComplex(i : Int) = new ReComplex(i)
   helper methods                            }
OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD
                                       “apply” Magic
:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::



   ‣ When an instance of a class is followed by parentheses with a
       list of zero or more parameters, the compiler invokes the
       apply method for that instance

   ‣ This is true for an object with a defined apply method (such
       as a companion object), as well as an instance of a class
       that defines an apply method
                                        val result = Complex(1,2)
                      is the same as....
                                        val result = Complex.apply(1,2)
OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD
 Negation and Scalar Multiplication
:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::




   ‣ In order to design a Complex class that is well integrated in
     our type system we should be able to support the following
     cases:
     ‣ -(a+ib)
     ‣ c*(a+ib)
     ‣ (a+ib)*c
   ‣ How can we supporting something like -(a+ib) and c*(a+ib)?
OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD
                     Scala Unary Operators
:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::




   ‣ Scala allows to define unary operators for the following
       method identifiers +, -, !, ~



                            def unary_-() = Complex(-re, -im)
                            !
                            def unary_!() = Math.sqrt(re*re + im*im)
                            !
                            def unary_~() = Complex(re, -im)

as a result we can write:
                        val result = -Complex(1,2) + ~Complex(2,3)
OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD
                Scala Implicit Conversions
:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::




   ‣ The expression:
                                        val c3 = 3*Complex(5, 7)

   ‣ Is equivalent to:
                                     val c3 = 3.*(Complex(5, 7))

   ‣ Yet, the method to multiply a Integer to a Complex is not
       present in the Scala Int class

   ‣ What can we do to make the trick?
OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD
                Scala Implicit Conversions
:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::




   ‣ Scala does not support Open Classes, thus allowing to add
       new methods to existing classes

   ‣ Yet Scala supports implicit conversions that can be used to
       achieve the same result

   ‣ Lets see how...
OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD
                 Scala Implicit Conversion
:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::




   object Complex {
   	 implicit def floatToReComplex (f: Float) = new ReComplex(f)
   	
   	 implicit def intToReComplex(i : Int) = new ReComplex(i)
   }



   class ReComplex(re: Float) {
   	
   	 def * (that: Complex) = Complex(re*that.re,re*that.im)
   	
   }
OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD
                                     The Result is...
:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::




                                        val c3 = 3*Complex(5, 7)

             is converted automatically into:

                          val c3 = ReComplex(3).*(Complex(5, 7))
OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD
                             Putting it all together
    :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::




class Complex(val re: Float, val im: Float) {
	 	                                                                            // Unary Operators
	 // Binary Operators                                                      	   def unary_-() = Complex(-re, -im)
	 def + (c: Complex) = Complex(re + c.re, im + c.im)                       	   def unary_!() = Math.sqrt(re*re + im*im)
	 def - (c: Complex) = Complex(re - c.re, im - c.im)
                                                                           	   def unary_~() = Complex(re, -im)
                                                                           	
	   def * (f: Float) = Complex(f*re, f*im)	
                                                                           	   // Formatting
	   def * (c: Complex) = Complex((re*c.re) - (im*c.im),
                                                                           	   override def toString() : String = {
                                 ((re*c.im) + (im*c.re)))
                                                                           	   	 if (im > 0) re + "+i" + im
	
                                                                           	   	 else if (im < 0) re + "-i" + (-im)
	   def / (c: Complex) = {
                                                                           	   	 else re.toString
	   	 val d = c.re*c.re + c.im*c.im
	   	 Complex(((re*c.re) + (im + c.im))/d,                                 	   	
              ((im*c.re) - (re*c.im))/d )                                  	   }
	   }                                                                      }
Stepping Into

:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::




                                                                                   Functions,
                                                                                    Closures,
                                                                                   & Currying
OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD
                                              Functions
:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::




   ‣ Scala has first-class functions
   ‣ Functions can be defined and called, but equally functions can
       be defined as unnamed literals and passed as values



        def inc(x: Int) = x + 1                                     val vinc = (x: Int) => x+1
        inc(5)                                                      vinc(5)

                               Notice once again the uniform access principle
OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD
                       Playing with Functions
:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::




                           val list = List(1,2,3,4,5,6,7,8,9)
                           val g5 = list.filter((x: Int) => x > 5)
                           g5: List[Int] = List(6, 7, 8, 9)


  Or with placeholder syntax

                           val list = List(1,2,3,4,5,6,7,8,9)
                           val g5 = list.filter(_ > 5)
                           g5: List[Int] = List(6, 7, 8, 9)
OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD
                                               Closures
:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::


   ‣ Scala allows you to define functions which include free
       variables meaning variables whose value is not bound to the
       parameter list

   ‣ Free variable are resolved at runtime considering the closure
       of visible variable

   ‣ Example:
         def mean(e : Array[Float]) : Float = {                             def mean(e : Array[Float]) : Float = {
         	 	 var sum = 0.0F                                                 	 	 var sum = 0.0F
         	 	 e.foreach((x: Int) => sum += x)                                	 	 e.foreach(sum += _)
         	 	 return sum/e.length                                            	 	 return sum/e.length
         	 }                                                                	 }
OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD
                                               Currying
:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::



   ‣ Scala provides support for curried functions which are applied
       to multiple argument lists, instead of just one

   ‣ Currying is the mechanism Scala provides for introducing new
       control abstraction

                    def curriedSum(x: Int)(y: Int) = x + y

                    curriedSum(1) {
                      3 +5
                    }
Stepping Into

:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::




                                                                                           Traits
OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD
                                                     Traits
:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::




   ‣ Scala supports single inheritance from classes but can mix-in
       multiple traits

   ‣ A trait is the unit of code reuse for Scala. It encapsulate
       methods and field definitions

   ‣ Traits usually expect a class to implement an abstract method,
       which constitutes the “narrow” interface that allows to
       implement a rich behaviour
OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD
           Ordered Complex Numbers
:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::




   ‣ Our complex numbers are not comparable
   ‣ Let’s assume that we wanted to make them comparable, and
       lets supposed that we define the total order as based on the
       module of the complex number

   ‣ How can we implement this behavior?
OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD
                                      Ordered Trait
:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::


   ‣ The Ordered[T] traits encapsulates the set of methods that
       allow to define a total ordering over a type

   ‣ All the behaviour is defined in terms of an abstract method,
       namely “compare”

   ‣ Classes that mix-in this trait have to implement the “compare”
       method
                            class Complex(val re: Float, val im: Float) extends Ordering[Complex] {
                            def compare(x: Complex, y: Complex) = {
                            	 	 if (x == y) 0
                            	 	 else if (!x > !y) 1
                            	 	 else -1
                            	 }
                             }
Stepping Into

:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::




                                                     CASE Classes &
                                                  Pattern Matching
OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD
Case Classes and Pattern Matching
:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::




   ‣ Case Classes and Pattern Matching are twin constructs that
       are pretty useful when dealing with tree-like recursive data
       structures

   ‣ These constructs allow to match patterns in an expression
       and reconstruct the object graph that makes it up

   ‣ Lets see an example...
OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD
Case Classes and Pattern Matching
:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::



       abstract class Expr
       case class Var(name: String) extends Expr
       case class Number(num: Float) extends Expr
       case class UnOp(operator: String, arg: Expr) extends Expr
       case class BinOp(operator: String, left: Expr, right: Expr)



      def simplifyExpr(expr: Expr) : Expr = expr match {
      	 case UnOp("-", UnOp("-", e)) => e
      	 case BinOp("+", e, Number("0")) => e
      	 case BinOp("*", e, Number("1")) => e
      	 case _ => expr
      }
Stepping Into

:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::




                                                               Type
                                                    parametrization
OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD
                         Type Parametrization
:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::




   ‣ Scala provides support for type parametrization and makes it
       available for both classes as well as traits
                                           trait    Queue[T] {
                                           	 def    head: T
                                           	 def    tail: Queue[T]
                                           	 def    append(x: T) : Queue[T]
                                           }


   ‣ Scala allows to annotate the parametrized type to control the
       resulting type variance
OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD
                                       Type Variance
:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::




   ‣ If S <: T is Queue[S] <: Queue[T]?
   ‣ By default Scala makes generic types nonvariant. This
       behaviour can be changed using the following annotations:

   ‣ Queue[+T] indicates that the the sub-typing is covariant in the
       parameter T

   ‣ Queue[-T] indicates that the the sub-typing is contravariant in
       the parameter T
Stepping Into

:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::




                                                                     Scala and DDS
OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD
                              Why Scala & DDS
:: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com ::


   ‣ Scala is strongly typed with a structural type systems, thus
       matching the DDS type system (if we ignore generics)

   ‣ Scala code is easy to read and verity thus a good candidate
       for mission critical system development, and

   ‣ Scala is increasingly being selected as the language to
       develop Internet applications such as Twitter for which DDS
       might be the data distribution back-bone

   ‣ Scala performance match Java performance (exceeds Java in
       some cases, slightly in few exceptional cases
OpenSplice DDS
             Delivering Performance, Openness, and Freedom


                http://www.opensplice.com/
References
               http://www.opensplice.org/                    http://www.slideshare.net/angelo.corsaro
                emailto:opensplicedds@prismtech.com




                http://bit.ly/1Sreg
                                                             http://twitter.com/acorsaro/




                http://www.youtube.com/OpenSpliceTube        http://opensplice.blogspot.com

Contenu connexe

Tendances

AST Transformations at JFokus
AST Transformations at JFokusAST Transformations at JFokus
AST Transformations at JFokus
HamletDRC
 
Invokedynamic / JSR-292
Invokedynamic / JSR-292Invokedynamic / JSR-292
Invokedynamic / JSR-292
ytoshima
 
Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest Updates
Iftekhar Eather
 
Java Bytecode For Discriminating Developers - GeeCON 2011
Java Bytecode For Discriminating Developers - GeeCON 2011Java Bytecode For Discriminating Developers - GeeCON 2011
Java Bytecode For Discriminating Developers - GeeCON 2011
Anton Arhipov
 
Mastering Java Bytecode With ASM - 33rd degree, 2012
Mastering Java Bytecode With ASM - 33rd degree, 2012Mastering Java Bytecode With ASM - 33rd degree, 2012
Mastering Java Bytecode With ASM - 33rd degree, 2012
Anton Arhipov
 

Tendances (12)

What Makes Objective C Dynamic?
What Makes Objective C Dynamic?What Makes Objective C Dynamic?
What Makes Objective C Dynamic?
 
AST Transformations at JFokus
AST Transformations at JFokusAST Transformations at JFokus
AST Transformations at JFokus
 
Objective c runtime
Objective c runtimeObjective c runtime
Objective c runtime
 
2007 09 10 Fzi Training Groovy Grails V Ws
2007 09 10 Fzi Training Groovy Grails V Ws2007 09 10 Fzi Training Groovy Grails V Ws
2007 09 10 Fzi Training Groovy Grails V Ws
 
Invokedynamic / JSR-292
Invokedynamic / JSR-292Invokedynamic / JSR-292
Invokedynamic / JSR-292
 
實戰 Hhvm extension php conf 2014
實戰 Hhvm extension   php conf 2014實戰 Hhvm extension   php conf 2014
實戰 Hhvm extension php conf 2014
 
PyParis 2017 / Camisole : A secure online sandbox to grade student - Antoine ...
PyParis 2017 / Camisole : A secure online sandbox to grade student - Antoine ...PyParis 2017 / Camisole : A secure online sandbox to grade student - Antoine ...
PyParis 2017 / Camisole : A secure online sandbox to grade student - Antoine ...
 
Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest Updates
 
Java Bytecode For Discriminating Developers - GeeCON 2011
Java Bytecode For Discriminating Developers - GeeCON 2011Java Bytecode For Discriminating Developers - GeeCON 2011
Java Bytecode For Discriminating Developers - GeeCON 2011
 
Clojure: Practical functional approach on JVM
Clojure: Practical functional approach on JVMClojure: Practical functional approach on JVM
Clojure: Practical functional approach on JVM
 
OpenGurukul : Language : PHP
OpenGurukul : Language : PHPOpenGurukul : Language : PHP
OpenGurukul : Language : PHP
 
Mastering Java Bytecode With ASM - 33rd degree, 2012
Mastering Java Bytecode With ASM - 33rd degree, 2012Mastering Java Bytecode With ASM - 33rd degree, 2012
Mastering Java Bytecode With ASM - 33rd degree, 2012
 

En vedette

OTS Dive Equipment
OTS Dive EquipmentOTS Dive Equipment
OTS Dive Equipment
DiveStock
 
Renolit SE Introduction PPT
Renolit SE Introduction PPTRenolit SE Introduction PPT
Renolit SE Introduction PPT
Mangesh Dhamele
 
Speedy Catalogue 2011 - Rail Pages
Speedy Catalogue 2011 - Rail PagesSpeedy Catalogue 2011 - Rail Pages
Speedy Catalogue 2011 - Rail Pages
speedyservices
 
Revista Línea Sur 5
Revista Línea Sur 5Revista Línea Sur 5
Revista Línea Sur 5
cancilleriaec
 
2011 Imago Membership Directory
2011 Imago Membership Directory 2011 Imago Membership Directory
2011 Imago Membership Directory
Kevin George
 

En vedette (20)

Tweeting with OpenSplice DDS
Tweeting with OpenSplice DDSTweeting with OpenSplice DDS
Tweeting with OpenSplice DDS
 
Snapshot of UK newspaper activity
Snapshot of UK newspaper activitySnapshot of UK newspaper activity
Snapshot of UK newspaper activity
 
OTS Dive Equipment
OTS Dive EquipmentOTS Dive Equipment
OTS Dive Equipment
 
306 attivita presso terzi
306   attivita presso terzi306   attivita presso terzi
306 attivita presso terzi
 
LOURDES, France
LOURDES, FranceLOURDES, France
LOURDES, France
 
Renolit SE Introduction PPT
Renolit SE Introduction PPTRenolit SE Introduction PPT
Renolit SE Introduction PPT
 
Why launching a venture in France
Why launching a venture in FranceWhy launching a venture in France
Why launching a venture in France
 
Pi fs 2012 2013
Pi fs 2012 2013Pi fs 2012 2013
Pi fs 2012 2013
 
Speedy Catalogue 2011 - Rail Pages
Speedy Catalogue 2011 - Rail PagesSpeedy Catalogue 2011 - Rail Pages
Speedy Catalogue 2011 - Rail Pages
 
Revista Línea Sur 5
Revista Línea Sur 5Revista Línea Sur 5
Revista Línea Sur 5
 
Caso avnet
Caso avnetCaso avnet
Caso avnet
 
Creating the Hickstead Statue
Creating the Hickstead StatueCreating the Hickstead Statue
Creating the Hickstead Statue
 
HESS Acadia in-ground up-lighter
HESS Acadia in-ground up-lighterHESS Acadia in-ground up-lighter
HESS Acadia in-ground up-lighter
 
Mas Alla del Cerebro
Mas Alla del CerebroMas Alla del Cerebro
Mas Alla del Cerebro
 
Web 2 Health 2 Presentation
Web 2 Health 2 PresentationWeb 2 Health 2 Presentation
Web 2 Health 2 Presentation
 
Mi Primer Cuenta De Email
Mi Primer Cuenta De EmailMi Primer Cuenta De Email
Mi Primer Cuenta De Email
 
About CAIE - 2012
About CAIE - 2012About CAIE - 2012
About CAIE - 2012
 
Communiqué de Presse: BILAN DU MIDEST 2014
Communiqué de Presse: BILAN DU MIDEST 2014Communiqué de Presse: BILAN DU MIDEST 2014
Communiqué de Presse: BILAN DU MIDEST 2014
 
Correo
CorreoCorreo
Correo
 
2011 Imago Membership Directory
2011 Imago Membership Directory 2011 Imago Membership Directory
2011 Imago Membership Directory
 

Similaire à Stepping into Scala

Silex: From nothing to an API
Silex: From nothing to an APISilex: From nothing to an API
Silex: From nothing to an API
chrisdkemper
 
Scala4sling
Scala4slingScala4sling
Scala4sling
day
 

Similaire à Stepping into Scala (20)

.NET for hackers
.NET for hackers.NET for hackers
.NET for hackers
 
Sprockets
SprocketsSprockets
Sprockets
 
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
 
Silex: From nothing to an API
Silex: From nothing to an APISilex: From nothing to an API
Silex: From nothing to an API
 
Lambdas & Streams
Lambdas & StreamsLambdas & Streams
Lambdas & Streams
 
Elixir on Containers
Elixir on ContainersElixir on Containers
Elixir on Containers
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy Code
 
New features in jdk8 iti
New features in jdk8 itiNew features in jdk8 iti
New features in jdk8 iti
 
Silex and Twig (PHP Dorset talk)
Silex and Twig (PHP Dorset talk)Silex and Twig (PHP Dorset talk)
Silex and Twig (PHP Dorset talk)
 
Scala presentationjune112011
Scala presentationjune112011Scala presentationjune112011
Scala presentationjune112011
 
Scala4sling
Scala4slingScala4sling
Scala4sling
 
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides:  Let's build macOS CLI Utilities using SwiftMobileConf 2021 Slides:  Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
 
C++ tutorial
C++ tutorialC++ tutorial
C++ tutorial
 
Os Secoske
Os SecoskeOs Secoske
Os Secoske
 
Xopus Application Framework
Xopus Application FrameworkXopus Application Framework
Xopus Application Framework
 
Devoxx 17 - Swift server-side
Devoxx 17 - Swift server-sideDevoxx 17 - Swift server-side
Devoxx 17 - Swift server-side
 
Javascript
JavascriptJavascript
Javascript
 
groovy & grails - lecture 2
groovy & grails - lecture 2groovy & grails - lecture 2
groovy & grails - lecture 2
 
C++primer
C++primerC++primer
C++primer
 
OpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ Programming
 

Plus de Angelo Corsaro

Plus de Angelo Corsaro (20)

Zenoh: The Genesis
Zenoh: The GenesisZenoh: The Genesis
Zenoh: The Genesis
 
zenoh: The Edge Data Fabric
zenoh: The Edge Data Fabriczenoh: The Edge Data Fabric
zenoh: The Edge Data Fabric
 
Zenoh Tutorial
Zenoh TutorialZenoh Tutorial
Zenoh Tutorial
 
Data Decentralisation: Efficiency, Privacy and Fair Monetisation
Data Decentralisation: Efficiency, Privacy and Fair MonetisationData Decentralisation: Efficiency, Privacy and Fair Monetisation
Data Decentralisation: Efficiency, Privacy and Fair Monetisation
 
zenoh: zero overhead pub/sub store/query compute
zenoh: zero overhead pub/sub store/query computezenoh: zero overhead pub/sub store/query compute
zenoh: zero overhead pub/sub store/query compute
 
zenoh -- the ZEro Network OverHead protocol
zenoh -- the ZEro Network OverHead protocolzenoh -- the ZEro Network OverHead protocol
zenoh -- the ZEro Network OverHead protocol
 
zenoh -- the ZEro Network OverHead protocol
zenoh -- the ZEro Network OverHead protocolzenoh -- the ZEro Network OverHead protocol
zenoh -- the ZEro Network OverHead protocol
 
Breaking the Edge -- A Journey Through Cloud, Edge and Fog Computing
Breaking the Edge -- A Journey Through Cloud, Edge and Fog ComputingBreaking the Edge -- A Journey Through Cloud, Edge and Fog Computing
Breaking the Edge -- A Journey Through Cloud, Edge and Fog Computing
 
Eastern Sicily
Eastern SicilyEastern Sicily
Eastern Sicily
 
fog05: The Fog Computing Infrastructure
fog05: The Fog Computing Infrastructurefog05: The Fog Computing Infrastructure
fog05: The Fog Computing Infrastructure
 
Cyclone DDS: Sharing Data in the IoT Age
Cyclone DDS: Sharing Data in the IoT AgeCyclone DDS: Sharing Data in the IoT Age
Cyclone DDS: Sharing Data in the IoT Age
 
fog05: The Fog Computing Platform
fog05: The Fog Computing Platformfog05: The Fog Computing Platform
fog05: The Fog Computing Platform
 
Programming in Scala - Lecture Four
Programming in Scala - Lecture FourProgramming in Scala - Lecture Four
Programming in Scala - Lecture Four
 
Programming in Scala - Lecture Three
Programming in Scala - Lecture ThreeProgramming in Scala - Lecture Three
Programming in Scala - Lecture Three
 
Programming in Scala - Lecture Two
Programming in Scala - Lecture TwoProgramming in Scala - Lecture Two
Programming in Scala - Lecture Two
 
Programming in Scala - Lecture One
Programming in Scala - Lecture OneProgramming in Scala - Lecture One
Programming in Scala - Lecture One
 
Data Sharing in Extremely Resource Constrained Envionrments
Data Sharing in Extremely Resource Constrained EnvionrmentsData Sharing in Extremely Resource Constrained Envionrments
Data Sharing in Extremely Resource Constrained Envionrments
 
The DDS Security Standard
The DDS Security StandardThe DDS Security Standard
The DDS Security Standard
 
The Data Distribution Service
The Data Distribution ServiceThe Data Distribution Service
The Data Distribution Service
 
RUSTing -- Partially Ordered Rust Programming Ruminations
RUSTing -- Partially Ordered Rust Programming RuminationsRUSTing -- Partially Ordered Rust Programming Ruminations
RUSTing -- Partially Ordered Rust Programming Ruminations
 

Dernier

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Dernier (20)

DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 

Stepping into Scala

  • 1. Splice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS Stepping Into :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: Angelo Corsaro, Ph.D. Chief Technology Officer PrismTech OMG DDS SIG Co-Chair angelo.corsaro@prismtech.com Splice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS
  • 2. Stepping Into :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: Basics
  • 3. OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD What is Scala :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: ‣ Scala (pronounced Skah-lah) stands for “Scalable language” ‣ It is a language that carefully and creatively blends Object Oriented and Functional constructs into a statically typed language with sophisticated type inference ‣ Scala targets the JVM and .NET runtime and is 100% compatible with Java
  • 4. OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD Why should you care? :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: ‣ Scala is simple to write, extremely compact and easy to understand ‣ Scala is strongly typed with a structural type system ‣ Scala is an extensible language (many construct are build in the standard library) ‣ Scala makes it easy to design Domain Specific Language
  • 5. OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD Complex Numbers :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: ‣ To explore some of the nice features of Scala, let’s see how we might design a Complex number class ‣ What we expect to be able to do is all mathematical operations between complex numbers as well as scalar multiplications and division ‣ [(1+i2)+2*(3-i5)*(i4)]/(1+i3) ‣ ~(1+i2) [conjugate] ‣ !(3+i4) [Modulo]
  • 6. OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD Constructor :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: ‣ Scala allows to implicitly create constructors and attributes starting from a simple argument list associated with the class declaration class Complex(val re: Float, val im: Float)
  • 7. OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD In Java :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: public class Complex { ! private final float re; ! private final float im; ! public Complex(float re, float im) { ! ! this.re = re; ! ! this.im = im; ! } ! public Complex(float f) { ! ! this.re = f; ! ! this.im = 0; ! } ! public float re() { return re;} ! public float im() { return im;}
  • 8. OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD Methods :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: ‣ Everything in Scala is a method even operators ‣ Methods name can be symbols such as *, !, +, etc. def + (c: Complex) : Complex = Complex(re + c.re, im + c.im) or, taking advantage of type inference.... def + (c: Complex) = Complex(re + c.re, im + c.im)
  • 9. OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD In Java... :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: public Complex add(Complex that) { return new Complex(this.re() + that.re(), this.im() + that.im()); }
  • 10. OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD As a result... :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: val result = Complex(1,2) + Complex(2,3) Complex c1 = new Complex(1, 2); Complex c2 = new Complex (3,4); Complex c3 = c1.add(c2); or... Complex c3 = (new Complex(1, 2).add(new Complex (3,4))l
  • 11. OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD Companion Object :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: ‣ Scala does not have the concept of static methods/attributes ‣ On the other hand it provides built-in support for Singletons, which are specified with the “object” keyword as opposed to the “class” keyword ‣ The companion object, object Complex { is the object def apply(real: Float, img: Float) = new Complex(real, img) associated with a class, which shares the def apply(real: Float) = new Complex(real, 0) same name and implicit def floatToReComplex (f: Float) = new ReComplex(f) provides typically implicit def intToReComplex(i : Int) = new ReComplex(i) helper methods }
  • 12. OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD “apply” Magic :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: ‣ When an instance of a class is followed by parentheses with a list of zero or more parameters, the compiler invokes the apply method for that instance ‣ This is true for an object with a defined apply method (such as a companion object), as well as an instance of a class that defines an apply method val result = Complex(1,2) is the same as.... val result = Complex.apply(1,2)
  • 13. OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD Negation and Scalar Multiplication :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: ‣ In order to design a Complex class that is well integrated in our type system we should be able to support the following cases: ‣ -(a+ib) ‣ c*(a+ib) ‣ (a+ib)*c ‣ How can we supporting something like -(a+ib) and c*(a+ib)?
  • 14. OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD Scala Unary Operators :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: ‣ Scala allows to define unary operators for the following method identifiers +, -, !, ~ def unary_-() = Complex(-re, -im) ! def unary_!() = Math.sqrt(re*re + im*im) ! def unary_~() = Complex(re, -im) as a result we can write: val result = -Complex(1,2) + ~Complex(2,3)
  • 15. OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD Scala Implicit Conversions :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: ‣ The expression: val c3 = 3*Complex(5, 7) ‣ Is equivalent to: val c3 = 3.*(Complex(5, 7)) ‣ Yet, the method to multiply a Integer to a Complex is not present in the Scala Int class ‣ What can we do to make the trick?
  • 16. OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD Scala Implicit Conversions :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: ‣ Scala does not support Open Classes, thus allowing to add new methods to existing classes ‣ Yet Scala supports implicit conversions that can be used to achieve the same result ‣ Lets see how...
  • 17. OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD Scala Implicit Conversion :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: object Complex { implicit def floatToReComplex (f: Float) = new ReComplex(f) implicit def intToReComplex(i : Int) = new ReComplex(i) } class ReComplex(re: Float) { def * (that: Complex) = Complex(re*that.re,re*that.im) }
  • 18. OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD The Result is... :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: val c3 = 3*Complex(5, 7) is converted automatically into: val c3 = ReComplex(3).*(Complex(5, 7))
  • 19. OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD Putting it all together :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: class Complex(val re: Float, val im: Float) { // Unary Operators // Binary Operators def unary_-() = Complex(-re, -im) def + (c: Complex) = Complex(re + c.re, im + c.im) def unary_!() = Math.sqrt(re*re + im*im) def - (c: Complex) = Complex(re - c.re, im - c.im) def unary_~() = Complex(re, -im) def * (f: Float) = Complex(f*re, f*im) // Formatting def * (c: Complex) = Complex((re*c.re) - (im*c.im), override def toString() : String = { ((re*c.im) + (im*c.re))) if (im > 0) re + "+i" + im else if (im < 0) re + "-i" + (-im) def / (c: Complex) = { else re.toString val d = c.re*c.re + c.im*c.im Complex(((re*c.re) + (im + c.im))/d, ((im*c.re) - (re*c.im))/d ) } } }
  • 20. Stepping Into :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: Functions, Closures, & Currying
  • 21. OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD Functions :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: ‣ Scala has first-class functions ‣ Functions can be defined and called, but equally functions can be defined as unnamed literals and passed as values def inc(x: Int) = x + 1 val vinc = (x: Int) => x+1 inc(5) vinc(5) Notice once again the uniform access principle
  • 22. OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD Playing with Functions :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: val list = List(1,2,3,4,5,6,7,8,9) val g5 = list.filter((x: Int) => x > 5) g5: List[Int] = List(6, 7, 8, 9) Or with placeholder syntax val list = List(1,2,3,4,5,6,7,8,9) val g5 = list.filter(_ > 5) g5: List[Int] = List(6, 7, 8, 9)
  • 23. OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD Closures :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: ‣ Scala allows you to define functions which include free variables meaning variables whose value is not bound to the parameter list ‣ Free variable are resolved at runtime considering the closure of visible variable ‣ Example: def mean(e : Array[Float]) : Float = { def mean(e : Array[Float]) : Float = { var sum = 0.0F var sum = 0.0F e.foreach((x: Int) => sum += x) e.foreach(sum += _) return sum/e.length return sum/e.length } }
  • 24. OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD Currying :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: ‣ Scala provides support for curried functions which are applied to multiple argument lists, instead of just one ‣ Currying is the mechanism Scala provides for introducing new control abstraction def curriedSum(x: Int)(y: Int) = x + y curriedSum(1) { 3 +5 }
  • 25. Stepping Into :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: Traits
  • 26. OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD Traits :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: ‣ Scala supports single inheritance from classes but can mix-in multiple traits ‣ A trait is the unit of code reuse for Scala. It encapsulate methods and field definitions ‣ Traits usually expect a class to implement an abstract method, which constitutes the “narrow” interface that allows to implement a rich behaviour
  • 27. OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD Ordered Complex Numbers :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: ‣ Our complex numbers are not comparable ‣ Let’s assume that we wanted to make them comparable, and lets supposed that we define the total order as based on the module of the complex number ‣ How can we implement this behavior?
  • 28. OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD Ordered Trait :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: ‣ The Ordered[T] traits encapsulates the set of methods that allow to define a total ordering over a type ‣ All the behaviour is defined in terms of an abstract method, namely “compare” ‣ Classes that mix-in this trait have to implement the “compare” method class Complex(val re: Float, val im: Float) extends Ordering[Complex] { def compare(x: Complex, y: Complex) = { if (x == y) 0 else if (!x > !y) 1 else -1 } }
  • 29. Stepping Into :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: CASE Classes & Pattern Matching
  • 30. OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD Case Classes and Pattern Matching :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: ‣ Case Classes and Pattern Matching are twin constructs that are pretty useful when dealing with tree-like recursive data structures ‣ These constructs allow to match patterns in an expression and reconstruct the object graph that makes it up ‣ Lets see an example...
  • 31. OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD Case Classes and Pattern Matching :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: abstract class Expr case class Var(name: String) extends Expr case class Number(num: Float) extends Expr case class UnOp(operator: String, arg: Expr) extends Expr case class BinOp(operator: String, left: Expr, right: Expr) def simplifyExpr(expr: Expr) : Expr = expr match { case UnOp("-", UnOp("-", e)) => e case BinOp("+", e, Number("0")) => e case BinOp("*", e, Number("1")) => e case _ => expr }
  • 32. Stepping Into :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: Type parametrization
  • 33. OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD Type Parametrization :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: ‣ Scala provides support for type parametrization and makes it available for both classes as well as traits trait Queue[T] { def head: T def tail: Queue[T] def append(x: T) : Queue[T] } ‣ Scala allows to annotate the parametrized type to control the resulting type variance
  • 34. OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD Type Variance :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: ‣ If S <: T is Queue[S] <: Queue[T]? ‣ By default Scala makes generic types nonvariant. This behaviour can be changed using the following annotations: ‣ Queue[+T] indicates that the the sub-typing is covariant in the parameter T ‣ Queue[-T] indicates that the the sub-typing is contravariant in the parameter T
  • 35. Stepping Into :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: Scala and DDS
  • 36. OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DDS OpenSplice DD Why Scala & DDS :: http://www.opensplice.org :: http://www.opensplice.com :: http://www.prismtech.com :: ‣ Scala is strongly typed with a structural type systems, thus matching the DDS type system (if we ignore generics) ‣ Scala code is easy to read and verity thus a good candidate for mission critical system development, and ‣ Scala is increasingly being selected as the language to develop Internet applications such as Twitter for which DDS might be the data distribution back-bone ‣ Scala performance match Java performance (exceeds Java in some cases, slightly in few exceptional cases
  • 37. OpenSplice DDS Delivering Performance, Openness, and Freedom http://www.opensplice.com/ References http://www.opensplice.org/ http://www.slideshare.net/angelo.corsaro emailto:opensplicedds@prismtech.com http://bit.ly/1Sreg http://twitter.com/acorsaro/ http://www.youtube.com/OpenSpliceTube http://opensplice.blogspot.com