Functional processing of Scala streams without OutOfMemory errors

前端 未结 4 1921
生来不讨喜
生来不讨喜 2020-12-04 20:06

Is it possible to apply functional programming to Scala streams such that the stream is processed sequentially, but the already processed part of the stream can be garbage c

4条回答
  •  无人及你
    2020-12-04 20:52

    As it turns out, this is a bug in the current implementation of reduceLeft. The problem is that reduceLeft calls foldLeft, and thus the stackframe of reduceLeft holds a reference to the head of the stream during the whole call. foldLeft uses tail-recursion to avoid this problem. Compare:

    (1 to 10000000).toStream.foldLeft(0)(_+_)
    (1 to 10000000).toStream.reduceLeft(_+_)
    

    These are semantically equivalent. In Scala version 2.8.0 the call to foldLeft works, but the call to reduceLeft throws an OutOfMemory. If reduceLeft would do its own work, this problem would not occur.

提交回复
热议问题