Skip to content

Instantly share code, notes, and snippets.

@spoertsch
spoertsch / Collections.scala
Last active December 25, 2015 01:59
IPSWAYS Scala Meetup - Collections
// List (immutable)
val numbers: List[Int] = List(1, 2, 6, 34, 17, 42, 54, 100, 117, 1337)
// Map (immutable)
val map = Map("Test" -> 4, "ABC" -> 4)
// List API
// Filter
numbers.filter(x => x < 4)
numbers.filter(_ < 4)
@spoertsch
spoertsch / OO.scala
Last active December 25, 2015 01:59
IPSWAYS Scala Meetup - OO
// object orientation
trait Player
case class EmptyPlayer() extends Player
case class NamedPlayer(name: String) extends Player
case class Score(p1: Int, p2: Int)
// Vorteil case class: toString, hashcode, copy, == vs. eq
val scoreA = Score(1, 2)
scoreA.toString()
scoreA.hashCode()
@spoertsch
spoertsch / OO_preconditions.scala
Created October 9, 2013 10:17
IPSWAYS Scala Meetup - OO preconditions
// Class preconditions
case class ScoreCond(p1: Int, p2: Int) {
require(p1 != p2)
require(p1 > 0 && p2 > 0)
}
ScoreCond(1, 2)
// Unerlaubte Werte führen zu einer Illegal argument exception
//ScoreCond(5, 5)