introduction to scala

21

Upload: peter-maas

Post on 10-May-2015

2.717 views

Category:

Technology


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Introduction To Scala
Page 2: Introduction To Scala

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

Page 3: Introduction To Scala

What & Why

Page 4: Introduction To Scala

Scala is:

A general purpose languageRuns on the JVM

statically typed!Object OrientedA Functional languageA Scalable languageDeep: Closures, Currying, Tuples, Implicit conversions, Pattern matching, Monads and MUCH more

Page 5: Introduction To Scala

What could you use it for?

Scripting Web ApplicationsMessagingTesting Graphical User Interfaces

Page 6: Introduction To Scala

Is used by:

TwitterSiemensLinkedInSony Pictures ImageworksThe migration team During a meeting in the Community Corner

(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.

Page 7: Introduction To Scala

Why would you use it?

Concise / tolerant syntax ==> good for DSLsIntegrates seamlessly with existing Java libraries/appsMakes concurrent programming easierScalable from a language point of viewStatically typedIt's from Switzerland!

Page 8: Introduction To Scala

Syntax

Page 9: Introduction To Scala

object MyGreeter { def main(args:Array[String]) = println("Hello World")}

Page 10: Introduction To Scala

object MyGreeter{ def main(args:Array[String]) = { val toGreet = "Marktplaats developers" println("Hello %s".format(toGreet)) }}

Page 11: Introduction To Scala

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 & 'operators'

Page 12: Introduction To Scala

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 noisedef makeGreet(toGreet:String) = "Hello " + toGreet

Page 13: Introduction To Scala

collections

List(1,2,3,4).foreach{ num => println(num) } // 1,2,3,4 List(1,2,3,4).foreach(println(_)) // 1,2,3,4List(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"

Page 14: Introduction To Scala

REPL

Read Eval(uate) Print Loop

Page 15: Introduction To Scala

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)}

Page 16: Introduction To Scala

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 }}

Page 17: Introduction To Scala

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 --> trueval p3 = p1.copy(firstName = "Sjoerd") p3 == p1 --> false

Page 18: Introduction To Scala

Traits & Stackingtrait 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!" }}

Page 19: Introduction To Scala

Much more

Implicit conversions (duck typing)ActorsTuplesPattern MatchingBuilt-in parser constructsMonadsXML support built into the languageRegexp support...

Page 20: Introduction To Scala

DEMO!

Page 21: Introduction To Scala

Thanks! Questions?