Long journey of Ruby standard library at RubyConf AU 2024
Introduction To Scala
1.
2. 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
4. 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
5. What could you use it for?
Scripting
Web Applications
Messaging
Testing
Graphical User Interfaces
6. 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.
7. 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!
11. 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)
12. 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
13. 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"
15. 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)
}
16. 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
}
}
17. 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
18. 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!"
}
}
19. Much more
Implicit conversions (duck typing)
Actors
Tuples
Pattern Matching
Built-in parser constructs
Monads
XML support built into the language
Regexp support
...