I coded up a quickie iterative version for you so you can get an idea
what that looks like.  My timing tests indicate that this runs many
times faster than even the hardcoded for loop nested to 9 levels.

; Convert to a vector, and pass off to step, which builds a lazy
sequence by applying increment over and over again.
(defn expand [seqs]
  (let [v-seqs (vec seqs)]
    (step v-seqs v-seqs)))

; The next step in the sequence is the first of everything in v-seqs,
then increment.

(defn step [v-seqs v-original-seqs]
  (if (nil? v-seqs) nil
      (lazy-cons (map first v-seqs)
                 (step (increment v-seqs v-original-seqs) v-original-seqs))))

; Do the rest of the last seq in v-seqs, if that yields nil, restore
this to the original seq, and do the rest of the previous seq in
v-seqs, and so on.

(defn increment [v-seqs v-original-seqs]                
  (loop [i (dec (count v-seqs)), v-seqs v-seqs]
    (cond (= i -1) nil
          (rest (v-seqs i)) (assoc v-seqs i (rest (v-seqs i)))
          :else (recur (dec i) (assoc v-seqs i (v-original-seqs i))))))

One thing that really bothers me about this code is that step and
increment should really be locally defined within expand (this would
also eliminate the need to pass around v-original-seqs everywhere),
but without letrec, I don't see any easy way to do this, because step
needs to refer to itself.  This seems like a good example of why
Clojure would benefit from letrec.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to