6

Is there a simple way in Scala to flatten or "unlist" a nested sequence of sequences of sequences (etc.) of something into just a simple sequence of those things, without any nesting structure?

amit
  • 3,332
  • 6
  • 24
  • 32
  • 3
    possible duplicate of [Generic, type-safe way to flatten arbitrarily nested collections in Scala?](http://stackoverflow.com/questions/12160596/generic-type-safe-way-to-flatten-arbitrarily-nested-collections-in-scala) – Ben Reich Feb 19 '15 at 14:43
  • 2
    See also the discussion in my somewhat related answer about a deep reverse [here](http://stackoverflow.com/a/12648663/334519). – Travis Brown Feb 19 '15 at 15:01
  • The easiest way that occurs to me is to run the provided flatten function in a loop until its output equals its input. I think most collections have a flatten function but I only checked List, Seq and Array. –  May 07 '15 at 08:53

1 Answers1

4

I don't think there is a flatten` method which converts a deeply nested into a sequence.

Its easy to write a simple recursive function to do this


def flatten(ls: List[Any]): List[Any] = ls flatMap {
    case ms: List[_] => flatten(ms)
    case e => List(e)
  } 
 val a =  List(List(List(1, 2, 3, 4, 5)),List(List(1, 2, 3, 4, 5)))
 flatten(a)
//> res0: List[Any] = List(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
mohit
  • 4,968
  • 1
  • 22
  • 39
  • This works well in a pinch, but the solution provided here: http://stackoverflow.com/questions/12160596/generic-type-safe-way-to-flatten-arbitrarily-nested-collections-in-scala is type safe – Ben Reich Feb 19 '15 at 14:47
  • Sorry for the nube question, but shouldn't you write `+Any`? – Dmitry Ginzburg Feb 19 '15 at 14:47
  • @BenReich - OP wanted a simple way and the solution given in your link is overly complicated for a simple case(i.e if it is a nesting of only Lists etc) – mohit Feb 19 '15 at 14:56
  • @DmitryGinzburg - It will not even compile with +Any :) Sorry, but I don't know what you are trying to do – mohit Feb 19 '15 at 14:57
  • @mohit I agree -- I just meant to say that there is a typesafe option available, so if this is a common problem, that option is preferable! – Ben Reich Feb 19 '15 at 15:01
  • @mohit would that method accept `List[Integer]`, for example? I meant something like `List>` in Java, which is equivalent to `List extends Object>`. – Dmitry Ginzburg Feb 19 '15 at 15:11
  • @DmitryGinzburg - Yes, it will accept. The only issue is that you will not get back List[Integer] but List[Any] with this method. Hence, not a typesafe solution as pointed out by Ben – mohit Feb 19 '15 at 15:32
  • @mohit Of course, I don't expect `List[Integer]` to be returned here, the statement of the problem doesn't allow us to determine output type correctly, isn't it? – Dmitry Ginzburg Feb 19 '15 at 18:39
  • @DmitryGinzburg The link that Ben has pointed out will return List[Integer] . The idea is that if you have List[List[List[Int]]] (arbitrarily nested list of ints), then flatten should maintain the type. After flatten,you should get List[Int] which will be the result if you use multiple built in flatten method like List[List[List[Int]]]().flatten.flatten. – mohit Feb 19 '15 at 18:59