This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| http://http://www.scala-lang.org/ - Scala Homepage | |
| http://typesafe.com/platform/getstarted - Typesafe Activator | |
| http://twitter.github.io/scala_schoo/l - Twitter Scala School | |
| https://www.coursera.org/course/progfun - Coursera Scala Kurs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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) |