-
-
Save dcbriccetti/3804602 to your computer and use it in GitHub Desktop.
case class Dog(name: String, tailLength: Option[Int]) | |
val dogs = Seq(Dog("Sparky", Some(85)), Dog("Rotty", None)) | |
val (dogsWithTails, dogsWithoutTails) = dogs.partition(_.tailLength.isDefined) | |
if (! dogsWithoutTails.isEmpty) | |
println("These dogs are missing tails: " + | |
dogsWithoutTails.map(_.name).mkString(", ")) | |
dogsWithTails.map(dog => { | |
val tailLength = dog.tailLength.get // We know it exists because of the partition above | |
// ⋮ | |
}) | |
// More realistic version: | |
case class Dog(name: String, tailLength: Option[Int], weight: Int) | |
val dogs = Seq(Dog("Sparky", Some(85), 10), Dog("Rotty", None, 15)) | |
val (dogsWithTails, dogsWithoutTails) = dogs.partition(_.tailLength.isDefined) | |
if (! dogsWithoutTails.isEmpty) | |
println("These dogs are missing tails: " + | |
dogsWithoutTails.map(_.name).mkString(", ")) | |
dogsWithTails.map(dog => { | |
val tailLength = dog.tailLength.get // We know it exists because of the partition above | |
someWorkWithSideEffects(tailLength, dog.weight) | |
}) | |
def someWorkWithSideEffects(tailLength: Int, weight: Int) { | |
// ⋮ | |
} |
scala> dogs
res16: List[Dog] = List(Dog(Sparky,Some(85)), Dog(Rotty,None))
scala> dogs.map(dog => dog.tailLength.cata(t => t, "missing tail:" + dog.name))
res17: List[Any] = List(85, missing tail:Rotty)
scala> res17.partition(s => s match {
| case i: Int => true
| case _ => false
| })
res21: (List[Any], List[Any]) = (List(85),List(missing tail:Rotty))
For-comprehension:
for {
dog <- dogsWithTails
length <- dog.tailLength
} println(length)
Even though we "know" all of the dogsWithTails have tails I still might:
dogsWithTails.map(dog => {
dog.tailLength.map(tailLength => {
someWorkWithSideEffects(tailLength, dog.weight)
}
})
// another approach where we use partition
scala> val dogs = List(Dog("Sparky", Some(85)), Dog("Rotty", None), Dog("Fuzzy", Some(20)), Dog("Murky", None))
dogs: List[Dog] = List(Dog(Sparky,Some(85)), Dog(Rotty,None), Dog(Fuzzy,Some(20)), Dog(Murky,None))
scala> val (wt, nt) = dogs.partition(_.tailLength.isDefined)
wt: List[Dog] = List(Dog(Sparky,Some(85)), Dog(Fuzzy,Some(20)))
nt: List[Dog] = List(Dog(Rotty,None), Dog(Murky,None))
scala> wt.map(_.tailLength).sequence
res25: Option[List[Int]] = Some(List(85, 20))
scala> nt.map(_.name)
res26: List[String] = List(Rotty, Murky)
scala> nt.map(_.name).mkString(", ")
res27: String = Rotty, Murky
even though we know that tailLength exists for dogsWithTails I might do this:
dogsWithTails.map(dog => {
dog.tailLength.map(tailLength => {
someWorkWithSideEffects(tailLength, dog.weight)
})
})
Even though we know that all dogsWithTails have a tailLength I still might do:
dogsWithTails.map(dog => {
dog.tailLength.map(tailLength => {
someWorkWithSideEffects(tailLength, dog.weight)
})
})
Even though we know that all dogsWithTails have a tailLength I still might do:
dogsWithTails.map(dog => {
dog.tailLength.map(tailLength => {
someWorkWithSideEffects(tailLength, dog.weight)
})
})
Even though we know that all dogsWithTails have a tailLength I still might do:
dogsWithTails.map(dog => {
dog.tailLength.map(tailLength => {
someWorkWithSideEffects(tailLength, dog.weight)
})
})
dogsWithTails.flatMap(_.tailLength)