Hi,

On 5 Mai, 14:54, Robert Luo <robort...@gmail.com> wrote:

> (defn repeated-seq
>   [f start]
>   (let [coll (f start)]
>     (concat coll (lazy-seq (repeated-seq f (last coll))))))
>
> it is OK when I call it with:
> (repeated-seq #(range % 5) 0)

repeated-seq cannot do anything about the problem. You have to ensure
that your f is free of "overlap" (so to say). One way todo that in
your example is (repeated-seq #(-> % inc (range 5)) -1).

Also depending on the size of your intermediate seqs you might want to
roll your own concat or use knowledge of the underlying collection
type to avoid the slow last.

(defn roll-your-own-repeated-seq
  [f init]
  (let [step (fn step
               [s]
               (lazy-seq
                 (when-let [[fst & rst] s]
                   (if rst
                     (cons fst (step rst))
                     (cons fst (roll-your-own-repeated-seq f
fst))))))]
    (lazy-seq
      (step (seq (f init))))))

(defn vector-repeated-seq
  [f init]
  (lazy-seq
    (let [v (f init)]
      (when-let [s (seq v)]
        (concat s (lazy-seq (vector-repeated-seq f (peek v))))))))

Hope this helps.

Sincerely
Meikel

-- 
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
Note that posts from new members are moderated - please be patient with your 
first post.
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