Is there a scala identity function?

前端 未结 3 441
生来不讨喜
生来不讨喜 2021-02-04 23:13

If I have something like a List[Option[A]] and I want to convert this into a List[A], the standard way is to use flatMap:

         


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-04 23:37

    You could just give the type inferencer a little help:

    scala> val l = List(Some("Hello"), None, Some("World"))
    l: List[Option[java.lang.String]] = List(Some(Hello), None, Some(World))
    
    scala> l.flatten[String]
    res0: List[String] = List(Hello, World)
    

提交回复
热议问题