8

I recently noticed that there was a very clear implementation of insertion sort here :

Insertion sort in clojure throws StackOverFlow error

  • which suffers from a memory overflow, due to the fact that concat lazily joins lists. I was wondering :

What strategies can we apply to "de-lazying" a list when we want better performance on large collections ?

Community
  • 1
  • 1
jayunit100
  • 17,388
  • 22
  • 92
  • 167
  • Hmm... After some more googling, its clear that the simple answer to this question is by using the "doall" function to wrap the collection. However, I assume there might be some gotchas here which might be useful to know about. – jayunit100 Mar 26 '12 at 00:18

1 Answers1

6

doall is certainly fine for forcing lazy evaluation.

Another useful thing to remember is that reduce is non-lazy. This can therefore be very useful in large computations for ensuring that intermediate results get evaluated and reduced to a single output value before the computation proceeds.

mikera
  • 105,238
  • 25
  • 256
  • 415
  • I don't quite understand the downvote. `doall` will do the job and a `reduce` accumulating into a vector also provides a solution, which shouldn't be dismissed immediatelly. The result has fast random access which might be interesting for certain applications. (That said: a `vec` around the resulting sequence would also give the same result.) – kotarak Mar 26 '12 at 06:27
  • I agree do all would work... In fact, it did work I tried it just after posting this question, regarding the other referenced post. Please comment. – jayunit100 Mar 26 '12 at 09:17