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
koan("Tuples can be created easily") { | |
val tuple = ("apple", "dog") | |
tuple should be(Tuple2("apple", "dog")) | |
} |
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
koan("Tuples can be created easily") { | |
val tuple = ("apple", "dog") | |
tuple should be("apple", "dog") | |
} |
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 b = a.filterNot(v => v == 5) // remove where value is 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
// map a function to double the numbers over the list | |
a.map {v => v * 2} should equal(List(__, __, __, __, __)) |
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
koan("Lists can be 'reduced' with a mathematical operation") { | |
val a = List(1, 3, 5, 7) | |
// note the two _s below indicate the first and second args respectively | |
a.reduceLeft(_ + _) should equal(16) // 1 + 3 + 5 + 7 | |
a.reduceLeft(_ * _) should equal(105) // 1 * 3 * 5 * 7 | |
} |
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
koan("Foldleft is like reduce, but with an explicit starting value") { | |
val a = List(1, 3, 5, 7) | |
// NOTE: foldLeft uses a form called currying that we will explore later | |
a.foldLeft(0)(_ + _) should equal(16) | |
a.foldLeft(10)(_ + _) should equal(26) | |
a.foldLeft(1)(_ * _) should equal(105) | |
a.foldLeft(0)(_ * _) should equal(0) | |
} |
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 a = (1 to 5).toList |
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
koan("Lists reuse their tails") { | |
val d = Nil | |
val c = 3 :: d | |
val b = 2 :: c | |
val a = 1 :: b | |
a should be(List(1, 2, 3)) | |
a.tail should be(b) | |
b.tail should be(c) | |
c.tail should be(d) |
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
// NOTE that the following will not compile, as iterators do not implement "contains" | |
// mapValues.contains("Illinois") should be (true) |
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
scala> val myMap = Map("MI" -> "Michigan", "OH" -> "Ohio", "WI" -> "Wisconsin", "MI" -> "Michigan") | |
myMap: scala.collection.immutable.Map[java.lang.String,java.lang.String] = Map(MI -> Michigan, OH -> Ohio, WI -> Wisconsin) | |
scala> val mapValues = myMap.values | |
mapValues: Iterable[java.lang.String] = MapLike(Michigan, Ohio, Wisconsin) |