Introduction To Scala
Upcoming SlideShare
Loading in...5
×
 

Introduction To Scala

on

  • 3,182 views

Short introduction to the Scala programming language which I presented on the Ebay/Marktplaats Tech Tuesday

Short introduction to the Scala programming language which I presented on the Ebay/Marktplaats Tech Tuesday

Statistics

Views

Total Views
3,182
Views on SlideShare
2,047
Embed Views
1,135

Actions

Likes
0
Downloads
28
Comments
0

8 Embeds 1,135

http://log4p.com 986
http://code.technically.us 140
http://www.linkedin.com 3
http://translate.googleusercontent.com 2
http://safe.tumblr.com 1
http://static.slidesharecdn.com 1
http://www.slideshare.net 1
http://translate.yandex.net 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Introduction To Scala Introduction To Scala Presentation Transcript

  • whoami Peter Maas: Joined marktplaats in the beginning of 2010 after working in outplacement for too many years. Currently part of the Migration team. Lot's of experience on the Java platform (VPRO, Kennisnet, Rabobank, European commission) Interested in programming languages in general
  • What & Why
  • Scala is: A general purpose language Runs on the JVM statically typed! Object Oriented A Functional language A Scalable language Deep: Closures, Currying, Tuples, Implicit conversions, Pattern matching, Monads and MUCH more
  • What could you use it for? Scripting Web Applications Messaging Testing Graphical User Interfaces
  • Is used by: Twitter Siemens LinkedIn Sony Pictures Imageworks During a meeting in the Community Corner The migration team (java.net booth) with James Gosling, a participant asked an interesting question: "Which Programming Language would you use *now* on top of JVM, except Java?". The answer was surprisingly fast and very clear: - Scala.
  • Why would you use it? Concise / tolerant syntax ==> good for DSLs Integrates seamlessly with existing Java libraries/apps Makes concurrent programming easier Scalable from a language point of view Statically typed It's from Switzerland!
  • Syntax
  • object MyGreeter { def main(args:Array[String]) = println("Hello World") }
  • object MyGreeter{ def main(args:Array[String]) = { val toGreet = "Marktplaats developers" println("Hello %s".format(toGreet)) } }
  • methods & 'operators' val toGreet = " Marktplaats developers" "Hello".+(toGreet) "Hello" + toGreet "Hello".concat(toGreet) "Hello" concat toGreet // methods are objects... val greetMethod = "hello %s".format(_:String) greetMethod(toGreet)
  • methods def makeGreet(toGreet:String):String = { return "Hello " + toGreet } def makeGreet(toGreet:String) = { // return type is inferred from returned type "Hello " + toGreet // the last result is also returned (like javascript, ruby) } // for short methods you can drop the line noise def makeGreet(toGreet:String) = "Hello " + toGreet
  • collections List(1,2,3,4).foreach{ num => println(num) } // 1,2,3,4 List(1,2,3,4).foreach(println(_)) // 1,2,3,4 List(1,2,3,4).map( _ * 2) foreach(println(_)) // 2,4,6,8 List(1,2,3,4).foldLeft(0)(_+_) // ((((0+1)+2)+3)+4) -> 10 1 :: 2 :: 3 :: 4 :: Nil // List(1,2,3,4) "this is a line of words" .split(" ") .map(_.reverse) // call reverse on each item .mkString(" ") // create a string results in: "siht si a enil fo sdrow"
  • REPL Read Eval(uate) Print Loop
  • Classes & Constructors class Person(name:String, age:Int) // immutable is default val p = new Person("Peter", 31) p.name = "Obama" // compiler error! class Person(var name:String, age:Int) val p = new Person("Peter", 31) p.name = "Obama" class Person(name:String, age:Int) { this(name:String) = this(name, 31) this(age:Int) = this("anonymous", age) }
  • Companion Objects class Car() { def drive = ... } /** * similar to a helper with static members in Java but without * strange inheritance quircks. */ object Car { def assemble(e:Engine, c:Chassis, w:List[Wheel]):Car = { ... // assemble the car and return it } }
  • Case Classes Classes with a lot of useful 'defaults' like hashcode, equals, toString, simplyfied constructor, copy and support for pattern matching. case class Person(firstName:String, lastName:String) val p1 = Person("Peter", "Maas") val p2 = Person("Peter", "Maas") p1 == p2 --> true val p3 = p1.copy(firstName = "Sjoerd") p3 == p1 --> false
  • Traits & Stacking trait Steer { def changeDirection() } trait Engine { def start() } trait Safety extends Engine { var locked = true abstract override def start() = if(!locked) super.start() else println("locked!") } class Car extends Engine with Steer { override def changeDirection() = { println("steering") } override def start() = { println("starting!") } } object TraitTests { def main(args: Array[String]) { val normal = new Car val safe = new Car with Safety normal.start // prints "starting!" safe.start // prints "locked!" } }
  • Much more Implicit conversions (duck typing) Actors Tuples Pattern Matching Built-in parser constructs Monads XML support built into the language Regexp support ...
  • DEMO!
  • Thanks! Questions?