2 ms·
If you have to walk over some space that is not easily mapped over (perhaps consisting of disjoint spaces), and collect/produce items according to various crite
by kazinator 7y ago
If you have to walk over some space that is not easily mapped over (perhaps consisting of disjoint spaces), and collect/produce items according to various criteria, it doesn't necessarily match, the "prepare a bag, and collect into it" is definitely a better approach.
He's just working in wretched language without macros.
TXR Lisp:
(build
(each ((i (range 1 5)))
(let ((id `car-@i`))
(when (car-empty carid)
(add id)))))
build starts an environment for implicit procedural list construction. The local function add is visible for adding an item to the bag. The macro yields the constructed list when it terminates.
The build environment provides deque semantics. Items can be added on both ends, and also deleted. A depth first search can be succinctly expressed using the closely related buildn:
;; breadth-first traversal of nested list;
(defun bf-map (tree visit-fn)
(buildn
(add tree)
(whilet ((item (del)))
(if (atom item)
[visit-fn item]
(each ((el item))
(add el))))))
This example is given in the documentation: https://www.nongnu.org/txr/txr-manpage.html#N-01346AAA https://www.nongnu.org/txr/txr-manpage.html#N-01346AAA