I have this code:
val res = Stream // launch the real computation, which alternates E and M steps, updating the computation state
.iterate(initCompState)(Base.emIteration)
.take(nIteration)
.last
The idea is to provide an initial state initCompState, a function that generates a new state from the previous one, run this for nIterations and get the final result.
I am not interrested in the intermediary states, and would like them to be garbage collected as soon as they are not needed. From what I read online, Stream retains values when they are recursively defined, which is not the case here.
Is my implementation correct and are the intermediate states between the initCompState and res garbage collected as soon as the next state in the stream has been computed ?