How to filter None's out of List[Option]?

53,034

Solution 1

If you want to get rid of the options at the same time, you can use flatten:

scala> someList.flatten
res0: List[String] = List(Hello, Goodbye)

Solution 2

someList.filter(_.isDefined) if you want to keep the result type as List[Option[A]]

Solution 3

The cats library also has flattenOption, which turns any F[Option[A]] into an F[A] (where F[_] is a FunctorFilter)

import cats.implicits._

List(Some(1), Some(2), None).flattenOption == List(1, 2)
Share:
53,034
Ralph
Author by

Ralph

Physician, retired. Retired software engineer/developer (Master of Science in Computer Science). I currently program mostly in Go. I am particularly interested in functional programming. In past lives, I programmed in Java, Scala, Basic, Fortran, Pascal, Forth, C (extensively, at Bell Labs), C++, Groovy, and various assembly languages. Started programming in assembly language in 1976. I started a martial arts school in 1986 (Shojin Cuong Nhu in New Jersey) and currently teach at the Tallest Tree Dojo in Gainesville, Florida. I like to cook. I am an atheist. Email: user: grk, host: usa.net

Updated on July 09, 2022

Comments

  • Ralph
    Ralph almost 2 years

    If I have a List[Option[A]] in Scala, what is the idiomatic way to filter out the None values?

    One way is to use the following:

    val someList: List[Option[String]] = List(Some("Hello"), None, Some("Goodbye"))
    someList.filter(_ != None)
    

    Is there a more "idiomatic" way? This does seem pretty simple.

  • Frank
    Frank about 12 years
    I would add that flatMap can be used to process the list elements other than Nones, similar to someList.flatten.map, as one often wants to work with these elements and not just flatten the list out of fun.
  • Nicolas
    Nicolas about 12 years
    Yes, I hesitated to ask Raph what he's expecting to do with the filtered list. But there are also a lot of scenarios where you just want to flatten.
  • Ralph
    Ralph about 12 years
    Sometimes I want to retrieve the contents of the Option objects, but other times I need them to remain as Option[A]. I am finding more and more uses for Option[A] -- cool feature.
  • cdmckay
    cdmckay about 9 years
    Is there an equivalent flatForeach type method?
  • Nicolas
    Nicolas about 9 years
    If you're collection is immutable, faltmap will do the trick. If you're collection is mutable, no, there is not.
  • matanster
    matanster about 8 years
    I find that in some cases this requires the use of type ascription in order to work.
  • Will Beason
    Will Beason over 6 years
    @cdmckay Just someList.flatten.foreach
  • Mojo
    Mojo over 3 years
    Why would cats add this when flatten in the std lib does the job? Is it doing something extra?
  • crater2150
    crater2150 almost 3 years
    @Mojo the cats version is more generic, as it provides this for any type, for which a FunctorFilter typeclass can be defined (e.g. cats provides such an instance for maps, so you can use it on a Map[A, Option[B], which does not have flatten in the standard library)
  • Hunger
    Hunger almost 3 years
    @Frank I thinkflatMap behaves like map.flatten rather then flatten.map. As if the map function return None, it will remove from final result.