How to simplify nested map calls?

前端 未结 3 2058
独厮守ぢ
独厮守ぢ 2021-01-11 17:33

Suppose I have a few nested functors, e.g. List[Option[Int]] and need to call the map of the most inner one.

Now I am using nested m

3条回答
  •  不要未来只要你来
    2021-01-11 18:11

    From the question I understood that you are tying to prune the list iterator e.i. remove upper levels of list in that case you can use flatten which convert list of lists into a single list.

    I will be removing few layers of list using flatten

    Code:-

    val lists = List( 
                        List( 
                            List(
                                    List("1"),List("2")
                                ),
                            List(
                                    List("3"),List("4") 
                                ) ,
                            List(
                                    List("a"),List("b")
                                ),
                            List(
                                    List("c"),List("d")
                                ) 
                                )
                      )
    
    val innerVal = lists.flatten.foreach(println)
    

    results :-

    List(List(1), List(2))
    List(List(3), List(4))
    List(List(a), List(b))
    List(List(c), List(d))
    

提交回复
热议问题