SlideShare a Scribd company logo
Scala in Nokia Places API


Lukasz Balamut - Places API Team




         17 April 2013
Purpose of this talk




        Why are we spending time migrating to a new language?
        How are we doing that?
        What have we learned?

    and of course: we are hiring.
Our scala story.

    Our migration in lines of code




    We’ve discovered more and more things on the way.
The Concordion tests problem
    In June 2011 we had problems with Concordion (HTML based acceptance test
    framework),

        hard to integrate
        HTML tests were not readable

    Solution:

        Multi-line Strings in Scala
        Continue using JUnit

    val expectedBody = """
      {
        "id":"250u09wh-83d4c5479b7c4db9836602936dbc8cb8",
        "title" : "Marfil (Le)",
        "alternativeNames" : [
            {
                "name" : "Le Marfil",
                "language" : "fr"
            }
        ]
    }
    """
The test names problem
       JUnit + camel case - not that readable for long names

   @Test
   def preservesSearchResponseItemOrder() { /* ... */ }

       JUnit + underscores - violates naming convention, but better to read

   @Test
   def preserves_search_response_item_order() { /* ... */ }

       ScalaTest

   test("preserves search response’s item order") { /* ... */ }

   It is supported by tools we use and produces nice output when run (as a bonus)

   [info]   DiscoverAroundAcceptanceTest:
   [info]   - calls nsp with 15Km radius
   [info]   - preserves search response’s item order
   [info]   - when requesting zero results, no request to search is made
Lift-Json


         JSON (de)serialisation can be done in really nice way

    e.g. getting place id:

    {
         "place": {
             "a_id": "276u33db-6f084959f97c45bc9db26348bafd4563"
           }
    }

    Also there is a generic way of parsing json, that gives you XPath like access:

    val id = (json  "place"  "a_id").extract[String]

         Lift-json provides also easy and efficient case class JSON (de)serialisation,
         hance case classes
Scala case classes in the model
    We model our upstream and downstream datamodels as case classes.
    case class Place (
      name: String,

        placeId: PlaceID
        //(...)
    )

         Each case class is a proper immutable data type - the new Java Bean ;),
         A lot of boilerplate code is generated by the compiler e.g.:
             accessors,
             equals/hashCode,
             copy methods,
             toString
             apply/unapply (for pattern maching),

    After decompiling the scala code above we will end up with 191 lines of Java:
    package pbapi.model.api;

    import   scala.*;
    import   scala.collection.Iterator;
    import   scala.runtime.BoxesRunTime;
    import   scala.runtime.ScalaRunTime$;
Scala case classes - Configuration

        Easy to update configuration
        Easy to compare environments’ configuration

    case class HttpClientConfig(
          serviceName: String,
          serviceBaseUrl: URL,
          readTimeout: Int = 0,
          connectionTimeout: Int,
          proxy: Option[ProxyConfig] = None,
          oauthKeys: Option[OAuthConfig] = None,
          displayUrl: Option[String] = None
    )

    "httpConfig":{
      "serviceName":"recommendations",
      "serviceBaseUrl":"http://nose.svc.ovi.com/rest/v1/recommendations/near
      "readTimeout":4000,
      "connectionTimeout":500,
      "displayUrl":"http://nose.svc.ovi.com/rest/v1/recommendations/nearby/"
    }
The Scala Type System




       A great way to express assumptions
       Have them checked by the compiler through the whole codebase
       At every point in the code know what to expect or you will be reminded by
       compiler.
The Scala Type System - Options 1




        Avoid null references (see The Billion Dollar Mistake )

    This will not compile!

    case class UpstreamResponse ( unreliableField: Option[String] )

    case class MyResponse ( reliableField: String )

    def transform(upstream: UpstreamResponse) =
        MyResponse(
            reliableField = upstream.unreliableField //<-- incompatible type
        )
The Scala Type System - Options 2




    But this will:

    case class UpstreamResponse ( unreliableField: Option[String] )

    case class MyResponse ( reliableField: String )

    def transform(upstream: UpstreamResponse) =
        MyResponse(
            reliableField = upstream.unreliableField.getOrElse("n/a") // enf
        )
The Scala Type System - Options 3


    how we were learning e.g.: to transform string when it is defined

    def transform(str: String): String

    reliableField =
        if (upstream.unreliableField.isDefined)
            transform(upstream.unreliableField.get)
        else "n/a"

    reliableField =
        upstream.unreliableField match {
            case Some(f) => transform(f)
            case None    => "n/a"
        }

    reliableField =
        upstream.unreliableField.map(transform) | "n/a"
The Scala Type System - Standard Types




       Immutable Map, List, Sets

   val map: Map[String, String] = Map("a" -> "a", "b" -> "b")

   val list: List[String] = "a" :: "b" :: Nil

       Tuples to express Pairs, Triplets etc.

   val pair: Pair[String, Int] = ("a", 1)

   val (a, b) = pair // defines a:String and b:Int
The Scala Type System - error handling
        Types can help with exceptional cases
    val parsed: Validation[NumberFormatException, Int] = someString.parseInt

    val optional: Option[Int] = parsed.toOption

    val withDefault: Int = optional.getOrElse(0) //if there was parsing prob

        Exception to Option
    val sub: Option[String] =
        allCatch.opt(line.substring(line.indexOf("{"")))

        or Either
    val parsed: Either[Throwable, Incident] =
        allCatch.either(new Incident(parse(jsonString))
    and handle it later
    parsed match {
        case Right(j) => Some(j)
        case Left(t) => {
            println("Parse error %s".format(t.getMessage))
            None
The Scala Type System - functions


        we defer translation (Translated) and text rendering (FormattedText) to
        serialisation time using function composition so we don’t need to pass user
        context through the whole stack e.g.:

    case class Place (
      //(...)
      attribution: Option[Translated[FormattedText]],
      //(...)
    )

    case class Translated[A](translator: TransEnv => A) {
        def apply(env: TransEnv): A = translator(env)

        def map[B](f: A => B): Translated[B] =
            new Translated[B](env => f(apply(env)))
        //(...)
    }
XML literals

    XML can be part of the code and compiler is checking syntax of it:

    def toKmlCoordinates(style: String): String         =
        (<Placemark>
            <name>{"bbox " + this}</name>
            <styleUrl>{style}</styleUrl>
            <Polygon>
                <outerBoundaryIs>
                    <LinearRing>
                        <coordinates>
                            {west + "," + north         +   ",0"}
                            {east + "," + north         +   ",0"}
                            {east + "," + south         +   ",0"}
                            {west + "," + south         +   ",0"}
                            {west + "," + north         +   ",0"}
                        </coordinates>
                    </LinearRing>
                </outerBoundaryIs>
            </Polygon>
        </Placemark>).toString
Scalacheck
    Automatic testing of assumptions, using set of generated values.
    e.g. the code below tests if
    BBox.parse(a.toString) == a
    is true for any a
    val generator: Gen[BBox] =
        for {
            s <- choose(-90.0, 90.0)
            n <- choose(-90.0, 90.0)
            if (n > s)
            w <- choose(-180.0, 180.0)
            e <- choose(-180.0, 180.0)
        } yield new BBox(w, s, e, n)

    property("parse . toString is identity") {
        check {
            (a: BBox) =>
                BBox.parse(a.toString) == a
        }
    }
    may yield this failing test message after several evaluations.
Scala (Scalding) in our analytics jobs
    e.g. popular places job
    class PopularPlacesJob(args: Args) extends AccessLogJob(args) {

        implicit val mapMonoid = new MapMonoid[String, Long]()
        val ord = implicitly[Ordering[(Long, String)]].reverse

        readParseFilter()
            .flatMap(’entry -> ’ppid) {
                entry:AccessLogEntry => entry.request.ppid
            }
            .mapTo((’entry, ’ppid) -> (’ppid, ’time, ’app)) {
                arg: (AccessLogEntry, String) => (arg._2, arg._1.dateTime, a
            }
            .groupBy((’ppid, ’app)) {
                _.min(’time -> ’minTime)
                 .max(’time -> ’maxTime)
                 .size(’num)
            }
            .groupBy((’ppid)) {
                _.min(’minTime)
                 .max(’maxTime)
                 .sum(’num)
Not so good in Scala




       Almost whole team needed to learn the new language - luckily we have
       Toralf ;)
       Tools are not as mature as those in Java (but they are getting better very
       fast)
       Longer compilation, compiler has a lot more to do (e.g. type inference)
       Multiple inheritance (traits)
       Operators
       changes in every language release
What we have learned, discovered?




       When done right - it’s possible to painlessly migrate code to new
       language, developing new feautres in the same time
       How to use good typesystem and enjoy it
       Take advantage form whole great immutable world of painless
       programming
       Use functions as first class citizens of language
Plans




        Scala 2.10
        Run Transformations in Futures, Validations etc.
        Akka actors, Futures composition
        Kill last Java files?
        Stop using Spring
Thank you!




       project site:
       places.nlp.nokia.com
       My contact:
       lukasz.balamut@nokia.com
       @lbalamut
       open position in our team:

More Related Content

What's hot (19)

PDF
Scala-对Java的修正和超越
Caoyuan Deng
 
PDF
Workshop Scala
Bert Van Vreckem
 
ODP
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Sanjeev_Knoldus
 
KEY
Scala for scripting
michid
 
PPTX
A Brief Intro to Scala
Tim Underwood
 
PDF
Stepping Up : A Brief Intro to Scala
Derek Chen-Becker
 
PDF
Scala
Sven Efftinge
 
KEY
Scalaz
mpilquist
 
ODP
A Tour Of Scala
fanf42
 
PDF
Scala vs Java 8 in a Java 8 World
BTI360
 
PPT
Scala introduction
Yardena Meymann
 
PDF
Scala at HUJI PL Seminar 2008
Yardena Meymann
 
ODP
Functional Objects & Function and Closures
Sandip Kumar
 
PDF
Pragmatic Real-World Scala (short version)
Jonas Bonér
 
PPTX
Scala on Android
Jakub Kahovec
 
PDF
Scaladroids: Developing Android Apps with Scala
Ostap Andrusiv
 
ODP
1.2 scala basics
futurespective
 
PDF
Scala Intro
Paolo Platter
 
ODP
JavaScript Web Development
vito jeng
 
Scala-对Java的修正和超越
Caoyuan Deng
 
Workshop Scala
Bert Van Vreckem
 
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Sanjeev_Knoldus
 
Scala for scripting
michid
 
A Brief Intro to Scala
Tim Underwood
 
Stepping Up : A Brief Intro to Scala
Derek Chen-Becker
 
Scalaz
mpilquist
 
A Tour Of Scala
fanf42
 
Scala vs Java 8 in a Java 8 World
BTI360
 
Scala introduction
Yardena Meymann
 
Scala at HUJI PL Seminar 2008
Yardena Meymann
 
Functional Objects & Function and Closures
Sandip Kumar
 
Pragmatic Real-World Scala (short version)
Jonas Bonér
 
Scala on Android
Jakub Kahovec
 
Scaladroids: Developing Android Apps with Scala
Ostap Andrusiv
 
1.2 scala basics
futurespective
 
Scala Intro
Paolo Platter
 
JavaScript Web Development
vito jeng
 

Viewers also liked (8)

PPTX
Chakarawet1
Khetpakorn Chakarawet
 
PPTX
Towards a three-step laser excitation of rubidium Rydberg states for use in a...
Ben Catchpole
 
PDF
Bacteria LENRs-and isotopic shifts in Uranium-Larsen-Lattice Energy Dec 7 2010
Lewis Larsen
 
PPTX
IB Chemistry on HNMR Spectroscopy and Spin spin coupling
Lawrence kok
 
PPTX
IB Chemistry on Infrared Spectroscopy
Lawrence kok
 
PPTX
Nuclear Magnetic Resonance Spectroscopy
Assistant Professor in Chemistry
 
PPTX
Hydrogen And Fuel Cell Technology For A Sustainable Future
Gavin Harper
 
PPT
NMR (nuclear Magnetic Resonance)
Rawat DA Greatt
 
Towards a three-step laser excitation of rubidium Rydberg states for use in a...
Ben Catchpole
 
Bacteria LENRs-and isotopic shifts in Uranium-Larsen-Lattice Energy Dec 7 2010
Lewis Larsen
 
IB Chemistry on HNMR Spectroscopy and Spin spin coupling
Lawrence kok
 
IB Chemistry on Infrared Spectroscopy
Lawrence kok
 
Nuclear Magnetic Resonance Spectroscopy
Assistant Professor in Chemistry
 
Hydrogen And Fuel Cell Technology For A Sustainable Future
Gavin Harper
 
NMR (nuclear Magnetic Resonance)
Rawat DA Greatt
 
Ad

Similar to Scala in Places API (20)

PDF
(How) can we benefit from adopting scala?
Tomasz Wrobel
 
PDF
A Brief Introduction to Scala for Java Developers
Miles Sabin
 
PDF
Miles Sabin Introduction To Scala For Java Developers
Skills Matter
 
PDF
BCS SPA 2010 - An Introduction to Scala for Java Developers
Miles Sabin
 
PDF
An Introduction to Scala for Java Developers
Miles Sabin
 
PDF
Scala Paradigms
Tom Flaherty
 
PDF
Scala In The Wild
djspiewak
 
PDF
Scala @ TechMeetup Edinburgh
Stuart Roebuck
 
PPTX
Intro to scala
Joe Zulli
 
PPTX
Why Scala is the better Java
Thomas Kaiser
 
ODP
Scala ntnu
Alf Kristian Støyle
 
PPTX
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
Matthew Farwell
 
PDF
scalaliftoff2009.pdf
Hiroshi Ono
 
PDF
scalaliftoff2009.pdf
Hiroshi Ono
 
PDF
scalaliftoff2009.pdf
Hiroshi Ono
 
PDF
scalaliftoff2009.pdf
Hiroshi Ono
 
PDF
Scala at GenevaJUG by Iulian Dragos
GenevaJUG
 
PDF
An Introduction to Scala (2014)
William Narmontas
 
PPT
An introduction to scala
Mohsen Zainalpour
 
PDF
Scala, Akka, and Play: An Introduction on Heroku
Havoc Pennington
 
(How) can we benefit from adopting scala?
Tomasz Wrobel
 
A Brief Introduction to Scala for Java Developers
Miles Sabin
 
Miles Sabin Introduction To Scala For Java Developers
Skills Matter
 
BCS SPA 2010 - An Introduction to Scala for Java Developers
Miles Sabin
 
An Introduction to Scala for Java Developers
Miles Sabin
 
Scala Paradigms
Tom Flaherty
 
Scala In The Wild
djspiewak
 
Scala @ TechMeetup Edinburgh
Stuart Roebuck
 
Intro to scala
Joe Zulli
 
Why Scala is the better Java
Thomas Kaiser
 
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
Matthew Farwell
 
scalaliftoff2009.pdf
Hiroshi Ono
 
scalaliftoff2009.pdf
Hiroshi Ono
 
scalaliftoff2009.pdf
Hiroshi Ono
 
scalaliftoff2009.pdf
Hiroshi Ono
 
Scala at GenevaJUG by Iulian Dragos
GenevaJUG
 
An Introduction to Scala (2014)
William Narmontas
 
An introduction to scala
Mohsen Zainalpour
 
Scala, Akka, and Play: An Introduction on Heroku
Havoc Pennington
 
Ad

Recently uploaded (20)

PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
PDF
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
Wojciech Ciemski for Top Cyber News MAGAZINE. June 2025
Dr. Ludmila Morozova-Buss
 
PDF
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PDF
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
PDF
Complete Network Protection with Real-Time Security
L4RGINDIA
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Wojciech Ciemski for Top Cyber News MAGAZINE. June 2025
Dr. Ludmila Morozova-Buss
 
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
Complete Network Protection with Real-Time Security
L4RGINDIA
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 

Scala in Places API

  • 1. Scala in Nokia Places API Lukasz Balamut - Places API Team 17 April 2013
  • 2. Purpose of this talk Why are we spending time migrating to a new language? How are we doing that? What have we learned? and of course: we are hiring.
  • 3. Our scala story. Our migration in lines of code We’ve discovered more and more things on the way.
  • 4. The Concordion tests problem In June 2011 we had problems with Concordion (HTML based acceptance test framework), hard to integrate HTML tests were not readable Solution: Multi-line Strings in Scala Continue using JUnit val expectedBody = """ { "id":"250u09wh-83d4c5479b7c4db9836602936dbc8cb8", "title" : "Marfil (Le)", "alternativeNames" : [ { "name" : "Le Marfil", "language" : "fr" } ] } """
  • 5. The test names problem JUnit + camel case - not that readable for long names @Test def preservesSearchResponseItemOrder() { /* ... */ } JUnit + underscores - violates naming convention, but better to read @Test def preserves_search_response_item_order() { /* ... */ } ScalaTest test("preserves search response’s item order") { /* ... */ } It is supported by tools we use and produces nice output when run (as a bonus) [info] DiscoverAroundAcceptanceTest: [info] - calls nsp with 15Km radius [info] - preserves search response’s item order [info] - when requesting zero results, no request to search is made
  • 6. Lift-Json JSON (de)serialisation can be done in really nice way e.g. getting place id: { "place": { "a_id": "276u33db-6f084959f97c45bc9db26348bafd4563" } } Also there is a generic way of parsing json, that gives you XPath like access: val id = (json "place" "a_id").extract[String] Lift-json provides also easy and efficient case class JSON (de)serialisation, hance case classes
  • 7. Scala case classes in the model We model our upstream and downstream datamodels as case classes. case class Place ( name: String, placeId: PlaceID //(...) ) Each case class is a proper immutable data type - the new Java Bean ;), A lot of boilerplate code is generated by the compiler e.g.: accessors, equals/hashCode, copy methods, toString apply/unapply (for pattern maching), After decompiling the scala code above we will end up with 191 lines of Java: package pbapi.model.api; import scala.*; import scala.collection.Iterator; import scala.runtime.BoxesRunTime; import scala.runtime.ScalaRunTime$;
  • 8. Scala case classes - Configuration Easy to update configuration Easy to compare environments’ configuration case class HttpClientConfig( serviceName: String, serviceBaseUrl: URL, readTimeout: Int = 0, connectionTimeout: Int, proxy: Option[ProxyConfig] = None, oauthKeys: Option[OAuthConfig] = None, displayUrl: Option[String] = None ) "httpConfig":{ "serviceName":"recommendations", "serviceBaseUrl":"http://nose.svc.ovi.com/rest/v1/recommendations/near "readTimeout":4000, "connectionTimeout":500, "displayUrl":"http://nose.svc.ovi.com/rest/v1/recommendations/nearby/" }
  • 9. The Scala Type System A great way to express assumptions Have them checked by the compiler through the whole codebase At every point in the code know what to expect or you will be reminded by compiler.
  • 10. The Scala Type System - Options 1 Avoid null references (see The Billion Dollar Mistake ) This will not compile! case class UpstreamResponse ( unreliableField: Option[String] ) case class MyResponse ( reliableField: String ) def transform(upstream: UpstreamResponse) = MyResponse( reliableField = upstream.unreliableField //<-- incompatible type )
  • 11. The Scala Type System - Options 2 But this will: case class UpstreamResponse ( unreliableField: Option[String] ) case class MyResponse ( reliableField: String ) def transform(upstream: UpstreamResponse) = MyResponse( reliableField = upstream.unreliableField.getOrElse("n/a") // enf )
  • 12. The Scala Type System - Options 3 how we were learning e.g.: to transform string when it is defined def transform(str: String): String reliableField = if (upstream.unreliableField.isDefined) transform(upstream.unreliableField.get) else "n/a" reliableField = upstream.unreliableField match { case Some(f) => transform(f) case None => "n/a" } reliableField = upstream.unreliableField.map(transform) | "n/a"
  • 13. The Scala Type System - Standard Types Immutable Map, List, Sets val map: Map[String, String] = Map("a" -> "a", "b" -> "b") val list: List[String] = "a" :: "b" :: Nil Tuples to express Pairs, Triplets etc. val pair: Pair[String, Int] = ("a", 1) val (a, b) = pair // defines a:String and b:Int
  • 14. The Scala Type System - error handling Types can help with exceptional cases val parsed: Validation[NumberFormatException, Int] = someString.parseInt val optional: Option[Int] = parsed.toOption val withDefault: Int = optional.getOrElse(0) //if there was parsing prob Exception to Option val sub: Option[String] = allCatch.opt(line.substring(line.indexOf("{""))) or Either val parsed: Either[Throwable, Incident] = allCatch.either(new Incident(parse(jsonString)) and handle it later parsed match { case Right(j) => Some(j) case Left(t) => { println("Parse error %s".format(t.getMessage)) None
  • 15. The Scala Type System - functions we defer translation (Translated) and text rendering (FormattedText) to serialisation time using function composition so we don’t need to pass user context through the whole stack e.g.: case class Place ( //(...) attribution: Option[Translated[FormattedText]], //(...) ) case class Translated[A](translator: TransEnv => A) { def apply(env: TransEnv): A = translator(env) def map[B](f: A => B): Translated[B] = new Translated[B](env => f(apply(env))) //(...) }
  • 16. XML literals XML can be part of the code and compiler is checking syntax of it: def toKmlCoordinates(style: String): String = (<Placemark> <name>{"bbox " + this}</name> <styleUrl>{style}</styleUrl> <Polygon> <outerBoundaryIs> <LinearRing> <coordinates> {west + "," + north + ",0"} {east + "," + north + ",0"} {east + "," + south + ",0"} {west + "," + south + ",0"} {west + "," + north + ",0"} </coordinates> </LinearRing> </outerBoundaryIs> </Polygon> </Placemark>).toString
  • 17. Scalacheck Automatic testing of assumptions, using set of generated values. e.g. the code below tests if BBox.parse(a.toString) == a is true for any a val generator: Gen[BBox] = for { s <- choose(-90.0, 90.0) n <- choose(-90.0, 90.0) if (n > s) w <- choose(-180.0, 180.0) e <- choose(-180.0, 180.0) } yield new BBox(w, s, e, n) property("parse . toString is identity") { check { (a: BBox) => BBox.parse(a.toString) == a } } may yield this failing test message after several evaluations.
  • 18. Scala (Scalding) in our analytics jobs e.g. popular places job class PopularPlacesJob(args: Args) extends AccessLogJob(args) { implicit val mapMonoid = new MapMonoid[String, Long]() val ord = implicitly[Ordering[(Long, String)]].reverse readParseFilter() .flatMap(’entry -> ’ppid) { entry:AccessLogEntry => entry.request.ppid } .mapTo((’entry, ’ppid) -> (’ppid, ’time, ’app)) { arg: (AccessLogEntry, String) => (arg._2, arg._1.dateTime, a } .groupBy((’ppid, ’app)) { _.min(’time -> ’minTime) .max(’time -> ’maxTime) .size(’num) } .groupBy((’ppid)) { _.min(’minTime) .max(’maxTime) .sum(’num)
  • 19. Not so good in Scala Almost whole team needed to learn the new language - luckily we have Toralf ;) Tools are not as mature as those in Java (but they are getting better very fast) Longer compilation, compiler has a lot more to do (e.g. type inference) Multiple inheritance (traits) Operators changes in every language release
  • 20. What we have learned, discovered? When done right - it’s possible to painlessly migrate code to new language, developing new feautres in the same time How to use good typesystem and enjoy it Take advantage form whole great immutable world of painless programming Use functions as first class citizens of language
  • 21. Plans Scala 2.10 Run Transformations in Futures, Validations etc. Akka actors, Futures composition Kill last Java files? Stop using Spring
  • 22. Thank you! project site: places.nlp.nokia.com My contact: [email protected] @lbalamut open position in our team: