How to implement an iterator in Scala making use of sequences as does this F# code? -


i wondering equivalent scala code short f# snippet:

seq {   in 1..10     printfn "%d"     yield } 

is possible have in scala?


what i'm attempting implement, though, shown in following bit of code:

module bst =     type t =         | node of int * t * t         | empty      let rec toseq tree = seq {         match tree         | empty -> ()         | node(value, left, right) ->             yield! toseq left             yield value             yield! toseq right     } 

i know how define discriminated-unions (as case calsses) in scala, i'm not sure how go implementing iterator based on sequences..?

thanks

if i'm not mistaken f#'s seq syntactic sugar constructing ienumerable instances lazy. closest scala version think of use of streams:

sealed trait bst case class node(value: int, left: bst, right: bst) extends bst case object empty extends bst  def toseq(tree: bst): stream[int] = tree match {   case empty => stream.empty   case node(value, left, right) =>     toseq(left) #::: value #:: toseq(right) } 

Comments