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 ?

1
  • 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. Commented Mar 26, 2012 at 0:18

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.)
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.