Holding onto your head & step functions -- confusion

8 views
Skip to first unread message

Garth Sheldon-Coulson

unread,
Nov 22, 2009, 6:06:31 PM11/22/09
to clo...@googlegroups.com
Hi All,

I'll confused about the hold-onto-your-head business when building lazy seqs using lazy-seq.

The "lazier" documentation on the web site doesn't really clear things up for me, though I've read it a few times.

Under what circumstances must one use a "step" function, and under what circumstances it it acceptable not to?

If this is covered in depth somewhere other than the "lazier" page please feel free just to send a link.

As a point of discussion, does the following function have the potential to cause memory problems?

Some notes: *kernel* and *options* are dynamic vars I want the lazy-seq to close over. parse-simple-atom is a function for parsing each element ("atom," **unrelated to Clojure atoms**) of the coll. type is an arg that is used in the parsing of each element of the coll.

(defn parse-expr-coll-to-lazy-seq [coll type]
    (let [kernel  *kernel*
                options *options*]
        (lazy-seq
            (binding [*kernel*  kernel
                                *options* options]
                (when-let [s (seq coll)]
                    (cons (parse-simple-atom (first s) type) (parse-expr-coll-to-lazy-seq (rest s) type)))))))

Thanks,
Garth

Garth Sheldon-Coulson

unread,
Nov 23, 2009, 6:46:58 AM11/23/09
to clo...@googlegroups.com
Scratch this question.

I think I've figured out that the "step" function in some people's code is there for aesthetic reasons only. As long as the step function contains the same code as would otherwise go directly inside the (lazy-seq ... ) there's no difference.

That is,

(let [step (fn [a b] <stuff>)]
  (lazy-seq (step a b)))

is the same as

(lazy-seq <stuff>)

The lazy documentation on the web site had made me think there was something special about the former, i.e. the compiler was optimizing it in some special way.

Someone please correct me if I'm still confused.
Reply all
Reply to author
Forward
0 new messages