0
whoami

Peter Maas:

   Joined marktplaats in the beginning of 2010 after working in
   outplacement for too many years. C...
What & Why
Scala is:

  A general purpose language
  Runs on the JVM
     statically typed!
  Object Oriented
  A Functional language...
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 th...
Why would you use it?

 Concise / tolerant syntax ==> good for DSLs
 Integrates seamlessly with existing Java libraries/ap...
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".for...
methods & 'operators'




val toGreet = " Marktplaats developers"
"Hello".+(toGreet)
"Hello" + toGreet

"Hello".concat(toG...
methods

def makeGreet(toGreet:String):String = {
  return "Hello " + toGreet
}

def makeGreet(toGreet:String) = { // retu...
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,...
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 ...
Companion Objects

class Car() {
    def drive = ...
}

/**
  * similar to a helper with static members in Java but withou...
Case Classes

Classes with a lot of useful 'defaults' like hashcode, equals,
toString, simplyfied constructor, copy and su...
Traits & Stacking
trait Steer {
  def changeDirection()
}

trait Engine {
  def start()
}

trait Safety extends Engine {
 ...
Much more

 Implicit conversions (duck typing)
 Actors
 Tuples
 Pattern Matching
 Built-in parser constructs
 Monads
 XML ...
DEMO!
Thanks!
Questions?
Introduction To Scala
Upcoming SlideShare
Loading in...5
×

Introduction To Scala

2,732

Published on

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

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,732
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
29
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Introduction To Scala"

  1. 1. 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
  2. 2. What & Why
  3. 3. 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
  4. 4. What could you use it for? Scripting Web Applications Messaging Testing Graphical User Interfaces
  5. 5. 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.
  6. 6. 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!
  7. 7. Syntax
  8. 8. object MyGreeter { def main(args:Array[String]) = println("Hello World") }
  9. 9. object MyGreeter{ def main(args:Array[String]) = { val toGreet = "Marktplaats developers" println("Hello %s".format(toGreet)) } }
  10. 10. 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)
  11. 11. 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
  12. 12. 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"
  13. 13. REPL Read Eval(uate) Print Loop
  14. 14. 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) }
  15. 15. 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 } }
  16. 16. 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
  17. 17. 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!" } }
  18. 18. Much more Implicit conversions (duck typing) Actors Tuples Pattern Matching Built-in parser constructs Monads XML support built into the language Regexp support ...
  19. 19. DEMO!
  20. 20. Thanks! Questions?
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×