I'm trying to move from F# to Scala. In F#, we can easily create a seq with computation expression or monad. For example:
let myseq = seq {
let mutableList = List()
for i = 0 to 100 do
mutableList.append(i)
yield sum(mutableList)
}
myseq |> Seq.iter println
I read about scala Stream
, but I'm not for sure how to use it properly, like the above example, which contains some state keep updating during the seq generation.
Another example would be to do some initialization and cleanup job inside the seq:
let myseq = seq {
let file = open(path)
while (x = read(file)) do
yield x
file.close() }
Can we do this in scala?