Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Clojure's reverse function returns a non-lazy sequence?

Why the designers of Clojure's reverse function decided that the returned sequence isn't a lazy one?
Clojure embraces lazy sequences usually.

like image 865
Chiron Avatar asked Nov 20 '11 22:11

Chiron


1 Answers

Surely it's because by definition, in order to reverse a sequence, you have to know what's at the other end in order to return what will become the first item in the reversed collection.

Hence, the sequence must be finite, and you'd have to evaluate it in order to use what lies at it's end.

Addendum:

Reverse doesn't make sense as an infinite sequence, (although it's fair to say that infinite sequences are not always a prerequisite for laziness).

If you are about to reverse a collection, then you already have it loaded in memory; it doesn't need to be calculated.

like image 124
Scott Avatar answered Nov 15 '22 05:11

Scott