Skip to content

Instantly share code, notes, and snippets.

@dcbriccetti
Created September 29, 2012 17:07
Show Gist options
  • Save dcbriccetti/3804602 to your computer and use it in GitHub Desktop.
Save dcbriccetti/3804602 to your computer and use it in GitHub Desktop.
How would you avoid the Option.get?
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) {
// ⋮
}
Copy link

ghost commented Sep 29, 2012

dogsWithTails.flatMap(_.tailLength)

@debasishg
Copy link

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))

Copy link

ghost commented Sep 29, 2012

For-comprehension:

for {
dog <- dogsWithTails
length <- dog.tailLength
} println(length)

@harryh
Copy link

harryh commented Sep 29, 2012

Even though we "know" all of the dogsWithTails have tails I still might:

dogsWithTails.map(dog => {
dog.tailLength.map(tailLength => {
someWorkWithSideEffects(tailLength, dog.weight)
}
})

@debasishg
Copy link

// 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

@harryh
Copy link

harryh commented Sep 29, 2012

even though we know that tailLength exists for dogsWithTails I might do this:

dogsWithTails.map(dog => {
dog.tailLength.map(tailLength => {
someWorkWithSideEffects(tailLength, dog.weight)
})
})

@harryh
Copy link

harryh commented Sep 29, 2012

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)
  })
})

@harryh
Copy link

harryh commented Sep 29, 2012

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)
  })
})

@harryh
Copy link

harryh commented Sep 29, 2012

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)
  })
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment