SlideShare ist ein Scribd-Unternehmen logo
1 von 53
Downloaden Sie, um offline zu lesen
- en bedre og mere effektiv Java?

                     Jesper Kamstrup Linnet
                     jesper@linnet-data.dk

                     7N IT-konference 2010
                     5. maj 2010
Om mig

•Freelancekonsulent
• Java- og .NET-udvikler/arkitekt
• Sprogbegejstret
Agenda

•Hvad er Scala?
• En bedre Java?
• Ready for prime time?
Kort om Scalas historie


• Startet i 2001
• Skabt af Martin Odersky
Hvad er Scala?
En bedre Java?
Java         Scala




                  + ?
          J av a+
Er Java dødt som sprog?




                     Kilde: InfoQ.com
Kendetegn


• Statisk typet
• Skalabilitet i højsædet
Hybridsprog

         =
 Objektorienteret
         +
Funktionsorienteret
Objektorienteret



1.to(5)        Range(1, 2, 3, 4, 5)
Funktionsorienteret


          val a = 10



   val f = (x: Int) => x + 5
Immutability

•   Centralt for
    funktionsprogrammering
•   Vigtigt for parallelisering
• Letter kodelæsning
Hvad gør Scala mere
     effektivt?
Hello, world



println("Hello, world")
Syntaktisk sukker




               Image: Suat Eman / FreeDigitalPhotos.net
Kompakt syntaks (1)
            Java                           Scala
public class Person {
    private final String name;
                                    class Person(
    private final String address;   	 val name: String,
     public Person(String name,
                                    	 val address: String);
	   	 String address) {
         this.name = name;
         this.address = address;
     }

    public String getName() {
        return name;
    }

    public String getAddress() {
        return address;
    }
Kompakt syntaks (2)
                        Java                                Scala
                                                     case class Person(
public class Person {
	      private final String name;
	      private final String address;

	
	
      public Person(String name, String address) {
      	      this.name = name;
                                                     	 name: String,
                                                     	 address: String);
	     	      this.address = address;
	     }

	     public String getName() {
	     	      return name;
	     }

	     public String getAddress() {
	     	      return address;
	     }

	     @Override
	     public int hashCode() {
	     	      ...
	     }

	     @Override
	     public boolean equals(Object obj) {
	     	      ...
	     }

	     @Override
	     public String toString() {
	     	      ...
	     }
copy method ala                 2 .8
                                   Sc


val person = Person("Jesper", "Kbh")

val newPerson = person.copy(address = "Aarhus")

                            Person(Jesper,Aarhus)
Kompakt syntaks (3)
class   Car {
	 var   driven = 0.0
	
	 def   drive(distance: Double) = driven += distance
	 	
	 def   milesDriven = driven * 1.6
}

...

val car = new Car
car drive 10
Collections
val list = List(1,2,3)
val map = Map(
	 	 	 	 	 "Jesper" -> 39,
	 	 	 	 	 "Peter" -> 55
				)
val set = Set(1, 2, 5, 2)
val array = Array(1, 2, 3)
Hvad foretrækker du?
            Java                          Scala
List<Integer> numbers = ...        val numbers = ...

List<Integer> result =             val result =
	 new ArrayList<Integer>();        	 numbers filter isEven
for (Integer number : numbers) {
	 if (isEven(number) {
	 	 result.add(number);
	 }
}
Anonyme funktioner
val list = List(1,2,3,4)

list filter isEven                List(2, 4)



list filter { n => n % 2 == 0 }



list filter { _ % 2 == 0 }
Operationer på List (1)
val list = List(1,2,3,4)


list map (x => x * x)      List(1, 4, 9, 16)
list sum                   10
list mkString ","          1,2,3,4
list forall { _ < 5 }      true
list partition isEven      (List(2, 4),
                           List(1, 3))
Closures
var outside = 5
val closure = (i: Int) => i * outside

println(closure(2))                     10


outside = 10
println(closure(2))                     20
Pattern matching (1)

value match {
    case 1 => println("Tal")
    case i: Int => printf("Tallet %d", i)
    case "test" => println("Streng")
    case (x, y) => printf("Et par, x=%s, y=%s", x, y)
    case _ => println("Alt andet")
}
Pattern matching (2)
                                             Lister
value match {
    case List(_, _) => println("To elementer")
    case List(1, rest @ _*) => println("1 og flere")
}
Pattern matching (3)
                             Regulære udtryk
val Danish = "Hej (.*)".r
val English = "Hi, (.*)".r

greeting match {
	 case Danish(name) => printf("Dansk: %s", name)
	 case English(name) => printf("Engelsk: %s", name)
}
Pattern matching (4)
                                 Case classes


value match {
	 case Person(_, "Kbh") => println("Københavner")
	 case _ => println("Uden for København")
}
XML (1)


val personsXml =
	 <persons>
	 	 <person name="Jesper"><age>38</age></person>
	 	 <person name="Ulla"><age>{age}</age></person>
	 </persons>
XML (2)

val persons = personsXml  "person"

val name = person  "@name"




val names = personsXml  "@name"
XML (3)


node match {
	 case <name>{name}</name> => println(name)
	 case _ => println("Andet")
}
Duck typing




“If it walks like a duck, and quacks like a
          duck, then it is a duck”
Duck typing

public class Text extends ... {
	 public void setText (String string) {


public class Button extends ... {
	 public void setText (String string) {
Duck typing m. Scala

def update(control: { def setText(text: String) }) = {
	 	 control.setText("Hello, world")
}




type ControlWithText = { def setText(text: String) }

def update(control: ControlWithText) = {
	 control.setText("Hello, world")
}
Traits (1)
                          Som interface
trait Editable {
	 def isEditable(): Boolean
}

class EditablePerson extends Editable {
	 def isEditable() = true
}
Traits (2)
                  Definition af mixin

trait Persistable {
	 val entityManager: EntityManager = ...

	 def save = {
	 	 entityManager.persist(this)
	 }
}
Traits (3)
                    Statisk brug af mixin

class Car extends Vehicle with Persistable {
	 ...
}

val car = new Car
car.save
Traits (4)
              Dynamisk brug af mixin

class Bicycle extends Vehicle {
	 ...
}

val bicycle = new Bicycle with Persistable
bicycle.save
Traits (5)
                                   Overstyring
trait LoggingCollection extends
               java.util.Collection[String] {

	   abstract override def add(e: String) = {
	   	 printf("Adding: %s", e)
	   	 super.add(e)
	   }
}

val coll = new java.util.ArrayList[String]
                      with LoggingCollection
Traits (6)
                         Eksempel: Observer
trait Subject {
  type Observer = { def receiveUpdate(subject:Any) }

  private var observers = List[Observer]()
  def addObserver(observer:Observer) =
	 	 observers ::= observer
  def notifyObservers =
	 	 observers foreach (_.receiveUpdate(this))
}
Parallelisering

•Højere abstraktionsniveau
• Aktørmodel
• Beskedudveksling
• Share-nothing
Simpel aktør

import scala.actors.Actor._

actor {
	 calculateStuff
}

// stuff in main thread
Beskedudveksling

val parrot = actor {
	 while (true) {
	 	 receive {
	 	 	 case msg =>
	 	 	 	 println("Msg: " + msg)
	 	 }
	 }
}

parrot ! "Hello, Polly"
Ready for prime time?
Scala i den virkelige
       verden
Masser af information

• http://scala-lang.org
• Bøger

• Tutorials og artikler
Hvorfor ikke Scala?

•Ny syntaks
• “Ungt” sprog
• Værktøjsunderstøttelse
Hvorfor Scala?

•Kompatibilitet med Java
• Stærkere syntaks
• I fremgang
Hvorfor Scala?
“You only fully comprehend
the awesomeness of #scala
when after weeks of being
pure scala you have to edit
some Java again...”
           James Strachan (@jstrachan)
Konklusion


En bedre og mere
  effektiv Java?
Kontakt

•   Slides: http://bit.ly/scala-7n

• jesper@linnet-data.dk
• http://twitter.com/jesper_linnet
• http://blog.kamstrup-linnet.dk
Spørgsmål?
- en bedre og mere effektiv Java?

                     Jesper Kamstrup Linnet
                     jesper@linnet-data.dk

                     7N IT-konference 2010
                     5. maj 2010

Weitere ähnliche Inhalte

Was ist angesagt?

Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecLoïc Descotte
 
Demystifying functional programming with Scala
Demystifying functional programming with ScalaDemystifying functional programming with Scala
Demystifying functional programming with ScalaDenis
 
ZIO Prelude - ZIO World 2021
ZIO Prelude - ZIO World 2021ZIO Prelude - ZIO World 2021
ZIO Prelude - ZIO World 2021Jorge Vásquez
 
Be Smart, Constrain Your Types to Free Your Brain!
Be Smart, Constrain Your Types to Free Your Brain!Be Smart, Constrain Your Types to Free Your Brain!
Be Smart, Constrain Your Types to Free Your Brain!Jorge Vásquez
 
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
JDays Lviv 2014:  Java8 vs Scala:  Difference points & innovation streamJDays Lviv 2014:  Java8 vs Scala:  Difference points & innovation stream
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation streamRuslan Shevchenko
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 

Was ist angesagt? (16)

Scala collections
Scala collectionsScala collections
Scala collections
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
 
Demystifying functional programming with Scala
Demystifying functional programming with ScalaDemystifying functional programming with Scala
Demystifying functional programming with Scala
 
ZIO Prelude - ZIO World 2021
ZIO Prelude - ZIO World 2021ZIO Prelude - ZIO World 2021
ZIO Prelude - ZIO World 2021
 
JS OO and Closures
JS OO and ClosuresJS OO and Closures
JS OO and Closures
 
Joy of scala
Joy of scalaJoy of scala
Joy of scala
 
Be Smart, Constrain Your Types to Free Your Brain!
Be Smart, Constrain Your Types to Free Your Brain!Be Smart, Constrain Your Types to Free Your Brain!
Be Smart, Constrain Your Types to Free Your Brain!
 
An introduction to scala
An introduction to scalaAn introduction to scala
An introduction to scala
 
Spsl v unit - final
Spsl v unit - finalSpsl v unit - final
Spsl v unit - final
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
 
Benefits of Kotlin
Benefits of KotlinBenefits of Kotlin
Benefits of Kotlin
 
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
JDays Lviv 2014:  Java8 vs Scala:  Difference points & innovation streamJDays Lviv 2014:  Java8 vs Scala:  Difference points & innovation stream
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
Scala
ScalaScala
Scala
 
1.2 scala basics
1.2 scala basics1.2 scala basics
1.2 scala basics
 
Meetup slides
Meetup slidesMeetup slides
Meetup slides
 

Andere mochten auch

SPS 2014
SPS 2014SPS 2014
SPS 2014alain2a
 
JD Edwards - Automate Benefits Enrollment Leveraging ESS Work Files
JD Edwards - Automate Benefits Enrollment Leveraging ESS Work FilesJD Edwards - Automate Benefits Enrollment Leveraging ESS Work Files
JD Edwards - Automate Benefits Enrollment Leveraging ESS Work FilesSmartbridge
 
It's No Mystery... Mobile BI Will Rule in 2015
It's No Mystery... Mobile BI Will Rule in 2015It's No Mystery... Mobile BI Will Rule in 2015
It's No Mystery... Mobile BI Will Rule in 2015Smartbridge
 
Study: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsStudy: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsLinkedIn
 

Andere mochten auch (6)

Acec2014 RALfieProject
Acec2014 RALfieProjectAcec2014 RALfieProject
Acec2014 RALfieProject
 
SPS 2014
SPS 2014SPS 2014
SPS 2014
 
Tiiu Maarja
Tiiu MaarjaTiiu Maarja
Tiiu Maarja
 
JD Edwards - Automate Benefits Enrollment Leveraging ESS Work Files
JD Edwards - Automate Benefits Enrollment Leveraging ESS Work FilesJD Edwards - Automate Benefits Enrollment Leveraging ESS Work Files
JD Edwards - Automate Benefits Enrollment Leveraging ESS Work Files
 
It's No Mystery... Mobile BI Will Rule in 2015
It's No Mystery... Mobile BI Will Rule in 2015It's No Mystery... Mobile BI Will Rule in 2015
It's No Mystery... Mobile BI Will Rule in 2015
 
Study: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsStudy: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving Cars
 

Ähnlich wie Scala - en bedre og mere effektiv Java?

pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
Scala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghScala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghStuart Roebuck
 
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
Softshake 2013: 10 reasons why java developers are jealous of Scala developersSoftshake 2013: 10 reasons why java developers are jealous of Scala developers
Softshake 2013: 10 reasons why java developers are jealous of Scala developersMatthew Farwell
 
Scala for Java Programmers
Scala for Java ProgrammersScala for Java Programmers
Scala for Java ProgrammersEric Pederson
 
(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
 
BCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java DevelopersBCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java DevelopersMiles Sabin
 
An Introduction to Scala for Java Developers
An Introduction to Scala for Java DevelopersAn Introduction to Scala for Java Developers
An Introduction to Scala for Java DevelopersMiles Sabin
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldBTI360
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Languageleague
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scalashinolajla
 

Ähnlich wie Scala - en bedre og mere effektiv Java? (20)

Scala introduction
Scala introductionScala introduction
Scala introduction
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Scala in Places API
Scala in Places APIScala in Places API
Scala in Places API
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
Scala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghScala @ TechMeetup Edinburgh
Scala @ TechMeetup Edinburgh
 
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
Softshake 2013: 10 reasons why java developers are jealous of Scala developersSoftshake 2013: 10 reasons why java developers are jealous of Scala developers
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
 
A bit about Scala
A bit about ScalaA bit about Scala
A bit about Scala
 
Scala for Java Programmers
Scala for Java ProgrammersScala for Java Programmers
Scala for Java Programmers
 
(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?
 
Workshop Scala
Workshop ScalaWorkshop Scala
Workshop Scala
 
BCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java DevelopersBCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java Developers
 
An Introduction to Scala for Java Developers
An Introduction to Scala for Java DevelopersAn Introduction to Scala for Java Developers
An Introduction to Scala for Java Developers
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 World
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Language
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scala
 

Kürzlich hochgeladen

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
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, Adobeapidays
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
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 TerraformAndrey Devyatkin
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
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?Igalia
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 

Kürzlich hochgeladen (20)

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
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
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
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
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
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?
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 

Scala - en bedre og mere effektiv Java?

  • 1. - en bedre og mere effektiv Java? Jesper Kamstrup Linnet jesper@linnet-data.dk 7N IT-konference 2010 5. maj 2010
  • 2. Om mig •Freelancekonsulent • Java- og .NET-udvikler/arkitekt • Sprogbegejstret
  • 3. Agenda •Hvad er Scala? • En bedre Java? • Ready for prime time?
  • 4. Kort om Scalas historie • Startet i 2001 • Skabt af Martin Odersky
  • 6. En bedre Java? Java Scala + ? J av a+
  • 7. Er Java dødt som sprog? Kilde: InfoQ.com
  • 8. Kendetegn • Statisk typet • Skalabilitet i højsædet
  • 9. Hybridsprog = Objektorienteret + Funktionsorienteret
  • 10. Objektorienteret 1.to(5) Range(1, 2, 3, 4, 5)
  • 11. Funktionsorienteret val a = 10 val f = (x: Int) => x + 5
  • 12. Immutability • Centralt for funktionsprogrammering • Vigtigt for parallelisering • Letter kodelæsning
  • 13. Hvad gør Scala mere effektivt?
  • 15. Syntaktisk sukker Image: Suat Eman / FreeDigitalPhotos.net
  • 16. Kompakt syntaks (1) Java Scala public class Person { private final String name; class Person( private final String address; val name: String, public Person(String name, val address: String); String address) { this.name = name; this.address = address; } public String getName() { return name; } public String getAddress() { return address; }
  • 17. Kompakt syntaks (2) Java Scala case class Person( public class Person { private final String name; private final String address; public Person(String name, String address) { this.name = name; name: String, address: String); this.address = address; } public String getName() { return name; } public String getAddress() { return address; } @Override public int hashCode() { ... } @Override public boolean equals(Object obj) { ... } @Override public String toString() { ... }
  • 18. copy method ala 2 .8 Sc val person = Person("Jesper", "Kbh") val newPerson = person.copy(address = "Aarhus") Person(Jesper,Aarhus)
  • 19. Kompakt syntaks (3) class Car { var driven = 0.0 def drive(distance: Double) = driven += distance def milesDriven = driven * 1.6 } ... val car = new Car car drive 10
  • 20. Collections val list = List(1,2,3) val map = Map( "Jesper" -> 39, "Peter" -> 55 ) val set = Set(1, 2, 5, 2) val array = Array(1, 2, 3)
  • 21. Hvad foretrækker du? Java Scala List<Integer> numbers = ... val numbers = ... List<Integer> result = val result = new ArrayList<Integer>(); numbers filter isEven for (Integer number : numbers) { if (isEven(number) { result.add(number); } }
  • 22. Anonyme funktioner val list = List(1,2,3,4) list filter isEven List(2, 4) list filter { n => n % 2 == 0 } list filter { _ % 2 == 0 }
  • 23. Operationer på List (1) val list = List(1,2,3,4) list map (x => x * x) List(1, 4, 9, 16) list sum 10 list mkString "," 1,2,3,4 list forall { _ < 5 } true list partition isEven (List(2, 4), List(1, 3))
  • 24. Closures var outside = 5 val closure = (i: Int) => i * outside println(closure(2)) 10 outside = 10 println(closure(2)) 20
  • 25. Pattern matching (1) value match { case 1 => println("Tal") case i: Int => printf("Tallet %d", i) case "test" => println("Streng") case (x, y) => printf("Et par, x=%s, y=%s", x, y) case _ => println("Alt andet") }
  • 26. Pattern matching (2) Lister value match { case List(_, _) => println("To elementer") case List(1, rest @ _*) => println("1 og flere") }
  • 27. Pattern matching (3) Regulære udtryk val Danish = "Hej (.*)".r val English = "Hi, (.*)".r greeting match { case Danish(name) => printf("Dansk: %s", name) case English(name) => printf("Engelsk: %s", name) }
  • 28. Pattern matching (4) Case classes value match { case Person(_, "Kbh") => println("Københavner") case _ => println("Uden for København") }
  • 29. XML (1) val personsXml = <persons> <person name="Jesper"><age>38</age></person> <person name="Ulla"><age>{age}</age></person> </persons>
  • 30. XML (2) val persons = personsXml "person" val name = person "@name" val names = personsXml "@name"
  • 31. XML (3) node match { case <name>{name}</name> => println(name) case _ => println("Andet") }
  • 32. Duck typing “If it walks like a duck, and quacks like a duck, then it is a duck”
  • 33. Duck typing public class Text extends ... { public void setText (String string) { public class Button extends ... { public void setText (String string) {
  • 34. Duck typing m. Scala def update(control: { def setText(text: String) }) = { control.setText("Hello, world") } type ControlWithText = { def setText(text: String) } def update(control: ControlWithText) = { control.setText("Hello, world") }
  • 35. Traits (1) Som interface trait Editable { def isEditable(): Boolean } class EditablePerson extends Editable { def isEditable() = true }
  • 36. Traits (2) Definition af mixin trait Persistable { val entityManager: EntityManager = ... def save = { entityManager.persist(this) } }
  • 37. Traits (3) Statisk brug af mixin class Car extends Vehicle with Persistable { ... } val car = new Car car.save
  • 38. Traits (4) Dynamisk brug af mixin class Bicycle extends Vehicle { ... } val bicycle = new Bicycle with Persistable bicycle.save
  • 39. Traits (5) Overstyring trait LoggingCollection extends java.util.Collection[String] { abstract override def add(e: String) = { printf("Adding: %s", e) super.add(e) } } val coll = new java.util.ArrayList[String] with LoggingCollection
  • 40. Traits (6) Eksempel: Observer trait Subject { type Observer = { def receiveUpdate(subject:Any) } private var observers = List[Observer]() def addObserver(observer:Observer) = observers ::= observer def notifyObservers = observers foreach (_.receiveUpdate(this)) }
  • 42. Simpel aktør import scala.actors.Actor._ actor { calculateStuff } // stuff in main thread
  • 43. Beskedudveksling val parrot = actor { while (true) { receive { case msg => println("Msg: " + msg) } } } parrot ! "Hello, Polly"
  • 45. Scala i den virkelige verden
  • 46. Masser af information • http://scala-lang.org • Bøger • Tutorials og artikler
  • 47. Hvorfor ikke Scala? •Ny syntaks • “Ungt” sprog • Værktøjsunderstøttelse
  • 48. Hvorfor Scala? •Kompatibilitet med Java • Stærkere syntaks • I fremgang
  • 49. Hvorfor Scala? “You only fully comprehend the awesomeness of #scala when after weeks of being pure scala you have to edit some Java again...” James Strachan (@jstrachan)
  • 50. Konklusion En bedre og mere effektiv Java?
  • 51. Kontakt • Slides: http://bit.ly/scala-7n • jesper@linnet-data.dk • http://twitter.com/jesper_linnet • http://blog.kamstrup-linnet.dk
  • 53. - en bedre og mere effektiv Java? Jesper Kamstrup Linnet jesper@linnet-data.dk 7N IT-konference 2010 5. maj 2010