One thing I've been meaning to do for a while is to try and figure out what the ideal general structure-traversing operator should look like. The bst-trav function in bst.arc set off my missing abstraction detector. I had that "I've written this code about 30 times before" feeling. So here is a shot at a general-purpose traverser: (mac trav (x . fs)
(w/uniq g
`((afn (,g)
(when ,g
,@(map [list _ g] fs)))
,x)))
You can get a lot of different behaviors out of it. E.g. printing the elements of a list is: arc> (trav '(1 2 3) [pr (car _)] [self (cdr _)])
123nil
This is only to illustrate its behavior, of course. You'd only use it to traverse more
complicated structures. E.g. bst-elts becomes (def bst-elts (b)
(accum a (trav b [self _!l] [a _!d] [self _!r])))
Does this seem the Right Thing?(It's not limited to traversal of structures, incidentally.) |