On Nov 18, 12:53 pm, Meikel Brandmeyer <[EMAIL PROTECTED]> wrote:
> As always: don't copy code blindly! Take a step back and look from a
> distance, how you can *translate* the code. For example, in the
> show-list-children function, the recursion is just used for iteration.
> It starts with x, do something to (first x) and then calls itself
> with (rest x). So the first step is to translate this into a loop
> recur pair. The next step is to see, that one can also write this
> as (doseq [child x] (do-something-to x)), or in case its the result
> you are interested in and not the side-effects: (map #(do-something-to
> %) x).
>
> So don't just copy the code, but understand what it does and then
> ask: "how would I do this in Clojure?"
Thanks for the advice. I think this works for show-list-children:
(defn insert-line [x y]
(doc-concat x (doc-concat (doc-line) y)))
(defn show-list-children [x]
(cond (empty? x)
(doc-nil)
(= (count x) 1)
(show (first x))
true
(reduce insert-line (map show x))))
Unfortunately the problem spot is:
(defmethod be :TEXT [w k d]
(let [doc (second (first d))]
(struct Text :Text (:contents doc)
(be w (+ k (.length (:contents doc)))
(rest d)))))
which I think I'll need to make lazy somehow.
-- Steve
--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---