scala - Make a recursive Stream from just 1 element? -


i've made stream gray codes using recursion 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, example

scala> gray(2) res17: list[string] = list(00, 01, 11, 10) 

i don't need list("0", "1") in definition, because can produced element 0:

scala> gnext(list("")) res18: list[java.lang.string] = list(0, 1) 

so there way / pattern can used produce stream first element?

val gray: stream[list[string]] = list("") #:: gray.map {gnext} 

or, alternatively,

val gray = stream.iterate(list(""))(gnext) 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -