I’ve made a Stream for Gray Codes using recursion as follows:
val gray: Stream[List[String]] = {
List("") #:: List("0", "1") #:: gray.tail.map {gnext}
}
where
val gnext = (i:List[String]) => i.map {"0" + _} ::: i.reverse.map {"1" + _}
so that, for example
scala> gray(2)
res17: List[String] = List(00, 01, 11, 10)
I don’t really need the List(“0”, “1”) in the definition, because it can be produced from element 0:
scala> gnext(List(""))
res18: List[java.lang.String] = List(0, 1)
So is there a way / pattern that can be used to produce a Stream from just the first element?
Or, alternatively,