Clojure: lazy magic

后端 未结 1 995
無奈伤痛
無奈伤痛 2021-02-20 07:02

Almost 2 identical programs to generate infinite lazy seqs of randoms. The first doesn\'t crash. The second crash with OutOfMemoryError exception. Why?

;Return          


        
相关标签:
1条回答
  • 2021-02-20 07:38

    I believe this is an example of "holding onto the head".

    By making the reference r1 in the second example you open up the possibility of later saying something like (first r1) so you will end up storing the members of your lazy-seq as they are reified.

    In the first case Clojure can determine that nothing will ever be done with earlier members of the infinite sequence so they can be disposed of and not consume memory.

    I am still very much a Clojure beginner myself, any comments or corrections to my understanding or terminology greatly appreciated.

    0 讨论(0)
提交回复
热议问题