How to filter None's out of List[Option]?
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)
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, 2022Comments
-
Ralph almost 2 years
If I have a
List[Option[A]]
in Scala, what is the idiomatic way to filter out theNone
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.
-
dskrvk about 8 years
-
-
Frank about 12 yearsI would add that
flatMap
can be used to process the list elements other thanNone
s, similar tosomeList.flatten.map
, as one often wants to work with these elements and not justflatten
the list out of fun. -
Nicolas about 12 yearsYes, 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 about 12 yearsSometimes I want to retrieve the contents of the
Option
objects, but other times I need them to remain asOption[A]
. I am finding more and more uses forOption[A]
-- cool feature. -
cdmckay about 9 yearsIs there an equivalent flatForeach type method?
-
Nicolas about 9 yearsIf you're collection is immutable, faltmap will do the trick. If you're collection is mutable, no, there is not.
-
matanster about 8 yearsI find that in some cases this requires the use of type ascription in order to work.
-
Will Beason over 6 years@cdmckay Just someList.flatten.foreach
-
Mojo over 3 yearsWhy would cats add this when flatten in the std lib does the job? Is it doing something extra?
-
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 aMap[A, Option[B]
, which does not haveflatten
in the standard library) -
Hunger almost 3 years@Frank I think
flatMap
behaves likemap.flatten
rather thenflatten.map
. As if the map function returnNone
, it will remove from final result.