Rather than 'down-right, I now prefer 'rightmost (first you go down then
you go to the rightmost child):
(defn rightmost
"Returns the loc of the rightmost sibling of the node at this loc, or
self"
[loc]
(let [[node {l :l r :r :as path}] loc]
(when (and path r)
(with-meta [(last r) (assoc path :l (apply conj l node (butlast
r)) :r nil)] ^loc))))
'leftmost is easier: #(-> % up down) but it may be good to have both
'leftmost and 'rightmost.
Plus 'rightmost makes 'prev easier to write and I need 'prev to fix 'remove.
Christophe
Christophe Grand a écrit :
> I keep needing to go to the last (rightmost) child of a loc (generally
> after appending it).
> (Or I failed to spot the proper idiom.)
>
> So I wrote last-child:
> (defn last-child [loc]
> (last (take-while identity (iterate zip/right (zip/down loc)))))
>
> but it feels a bit overkill because of all this repeated calls to
> zip/right. I could write:
>
> (defn down-right
> "Returns the loc of the rightmost child of the node at this loc, or
> nil if no children"
> [loc]
> (let [[node path] loc
> cs (vec (zip/children loc))]
> (when (seq cs)
> (with-meta [(peek cs) {:l (pop cs)
> :pnodes (if path (conj (:pnodes path) node)
> [node])
> :ppath path
> :r nil}] ^loc))))
>
> but it would mess with zippers internals: I don't like to have that in
> user code.
>
> Would it be a useful addition to clojure.zip?
>
> Christophe
>
>
> >
>
>
--
Professional: http://cgrand.net/ (fr)
On Clojure: http://clj-me.blogspot.com/ (en)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---