Hacker News new | ask | show | jobs
by liugiul 4105 days ago
To me, this seems pretty similar to Clojures lazy-seq. A seq is (afaik, I'm no expert Clojure guy although I enjoy it a lot) something with a 'next' value. A lazy seq basically can wait with returning the next object, and therefore be evaluated when it decides it's ready (like new data comes in, like a stream). Please let me know if I misunderstood this!!!

As a someone who does JS for my day job I'm really enjoying playing with Clojure in my evenings and weekends (like now, it's Saturday night 00:30 here in Berlin). A small part of me dreams about doing it full time ;).

1 comments

Close enough. A lazy sequence is a sequence who's elements are only generated when they're asked for.

In Clojure, a sequence (seq) is a list datastructure. It can be traditional Lisp list, an array, or some other datastructure who's items are arrayed in an ordered sequence. You can generally treat a sequence like you can treat and array or a list in other languages accessing any element by its index, (except that it's immutable, of course.) Clojure doesn't traverse the sequence via 'next' unless the particular sequence type (ie a Lisp style list) is implemented to do it that way.

A lazy sequence is a special kind of sequence which is implemented with a sort of 'cons' structure where the 'cdr' is a function to be called rather than a pointer. To clarify, in a traditional Lisp, a list is implement with a series of pairs of pointers called 'cons' cells. The first pointer is called the 'car' and it pointed to the content at that point in the list and the second pointer is called 'cdr' and it points to the next pair. A lazy sequence is similar except the that the pointer to the next pair is rather a function which returns the next pair. This means you can have a sequence who's actual data is not yet fulfilled. When you access an element in a lazy sequence, that element and every element before will be generated (and cached). Unless you access an element in that sequence, that element will never be generated.

This lets you have things like infinite datastructures or do something like wrap a stream in a sequence and loop over it like it was an array.

BTW, if your day job is with JS, maybe you can squeeze in a a ClojureScript app on a small project? I've done this.