introduction to scala

Post on 10-May-2015

2.717 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

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 languageRuns on the JVM

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

What could you use it for?

Scripting Web ApplicationsMessagingTesting Graphical User Interfaces

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.

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!

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

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'

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

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"

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

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

Much more

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

DEMO!

Thanks! Questions?

top related