Skip to content

Instantly share code, notes, and snippets.

@spoertsch
spoertsch / currying.scala
Created October 9, 2013 18:15
IPSWAYS Scala Meetup - Partial function vs. Currying
def lessThan(max: Int, value: Int) = value < max
//> lessThan: (max: Int, value: Int)Boolean
lessThan(5, 10)
//> res0: Boolean = false
// Partial applied functions
val lessThan5 = lessThan(max = 5, _: Int)
//> lessThan5 : Int => Boolean = <function1>
lessThan5(10)
//> res1: Boolean = false
@spoertsch
spoertsch / playerPairs.scala
Created October 9, 2013 18:07
IPSWAYS Scala Meetup - Creating unique Pairs of players using for expression
val players = List("Timo", "Jan", "Lothar", "Sven", "Danny")
//> players : List[String] = List(Timo, Jan, Lothar, Sven, Danny)
val playerPairs = for {
p1 <- players
p2 <- players
if p1 != p2
} yield (p1, p2)
//> playerPairs : List[(String, String)] = List((Timo,Jan), (Timo,Lothar), (Tim
//| o,Sven), (Timo,Danny), (Jan,Timo), (Jan,Lothar), (Jan,Sven), (Jan,Danny), (L
@spoertsch
spoertsch / links.txt
Last active December 25, 2015 01:59
IPSWAYS Scala Meetup - Links
@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)
@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 / 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)