On Jan 15, 1:21 pm, Nicolas Buduroi <nbudu...@gmail.com> wrote:
> Hi, I'm still not familiar with laziness and I'm trying to make a
> function recursively walk arbitrary data structures to perform some
> action on all strings.
> ...
> Is there a way too make a fully lazy version of this function?
>
> - budu

I'm trying to create a lazy version of walk. Here's my first attempt:

(defn lazy-walk
  "Traverses form, an arbitrary data structure. inner and outer are
  functions.  Applies inner to each element of form, building up a
  data structure of the same type, then applies outer to the result.
  Recognizes all Clojure data structures except sorted-map-by."
  [inner outer form]
  (lazy-seq
    (cond
      (list? form) (outer (apply list (map inner form)))
      (seq? form) (outer (map inner form))
      (vector? form) (outer (vec (map inner form)))
      (map? form) (outer (into (if (sorted? form) (sorted-map) {})
                               (map inner form)))
      (set? form) (outer (into (if (sorted? form) (sorted-set) #{})
                               (map inner form)))
      :else (outer form) ) )
)

user=> (def lw (lazy-walk #(if (string? %) (.toUpperCase %) %)
                                         identity (interleave (repeat
10000 88) (repeat 10000 "abc"))))
user=> (type lw)
clojure.lang.LazySeq
user=> (take 20 lw)
(88 "ABC" 88 "ABC" 88 "ABC" 88 "ABC" 88 "ABC" 88 "ABC" 88 "ABC" 88
"ABC" 88 "ABC" 88 "ABC")

I'm still not quite sure how to test lazy functions for correct lazy
behavior, though.
    regards,
    -tom

-- 
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