2

There is a good question which says that we should Stream/View or Iterate our collections to make them on-demand. It is clear. I just do not understand what should I apply the .view or .iterate to in the following demo

val l1 = List("10", "00", "0")
def gen(depth: Int): Iterable[String] = if (depth == 1) l1 else {
    for (sub <- gen(depth-1); item <- List(depth + sub, sub+sub, sub)) yield item
} 

Should I apply them to gen(depth-1) or to List(depth+..)?

By the way, should I inline l1 in the (depth == 1) l1 else? It is not used anywhere else. I just afraid that it would create a new list for every leaf.

Community
  • 1
  • 1

1 Answers1

0

I would have to try it, but I would say List(depth+..).view, since you wont use the same values more than once.I think it will give you a sort of DFS traversal given the recursion you have there.

And yeah, I would not inline it but live l1 like that. I would even do val l1 = List("10", "00", "0").view.

Daniel Langdon
  • 5,899
  • 4
  • 28
  • 48