| Hi- I've been using this one macro a lot lately and it seems very useful and "arc-like" to me. It deals with the following common idiom: (let x nil
(...traverse some complicated data...
...
...
(= x ...))
x)
Here, we're searching some complicated data structure, looking for a certain item. When we find it, we assign 'x to it. Finally, we return x.Here's my macro for capturing this idiom: (mac focus (var . body)
(w/uniq (x y)
`(withs (,x nil
,var (fn ,y
(if ,y
(= ,x (car ,y))
,x)))
,@body
,x)))
So basically, this macro is used for "focusing" on a specific data point in a complex data structure and retrieving it. It is a distant cousin of 'accum, but specialized for single items.For instance, here we use it to find an odd number in a list: (focus f
(map [when odd._
f._]
*list-of-numbers*))
(of course, for a simple example like this you could use 'find, instead.)One additional feature is that the function created (which would be 'f in the example above) is both a getter and a setter: If you don't pass the function a parameter it will return the previously assigned value. This makes the 'focus command useful in many tricky situations. For instance, suppose you had a large list of numbers and you wanted to return the largest odd number in the list... With 'focus, this can be written as follows: (focus f
(map [and odd._ (or (no (f)) (< (f) _)) f._] *list-of-numbers*))
This would be cumbersome to do without 'focus, (I think) expecially when dealing with a large list, where you don't want to create an intermediate list (such as by using 'rem first to strip out the even numbers)BTW- I'd love to know if there's a way to address this idiom without my 'focus command. Please post in your comments alternate solutions that show why my macro is obsolete :) |