Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Consider a case using split-with where you know that the prefix is going to be very small, but the suffix will be very large. If you retain a reference to the prefix, all of the values in the sequence will be retained even if your processing of the suffix is lazy. If the sequence in question is very large, then this head retention will ensure that an out of memory error will occur:
- (let [[t d] (split-with #(< % 12) (range 1e8))] [(count d) (count t)])
- ;= #<OutOfMemoryError java.lang.OutOfMemoryError: Java heap space>
- Reversing the order of evaluation solves the problem:
- (let [[t d] (split-with #(< % 12) (range 1e8))] [(count t) (count d)])
- ;= [12 99999988]
- Since the last reference of t occurs before the processing of d, no reference to the head
- of the range sequence is kept, and no memory issues arise.
Advertisement
Add Comment
Please, Sign In to add comment