(def testify (x) - (if (isa x 'fn) x [is _ x])) + (if (in (type x) 'fn 'table) x [is _ x]))
Treat tables like functions in Arc's list sequence functions
When structs or objects are implemented as tables, code such as “take these objects out of my list of objects” will stop working with this patch. See Duplicates appearing in HN top? for an example.
Arc comes with a number of functions that apply a function to successive elements of a sequence. From the Arc tutorial:
arc> (keep odd '(1 2 3 4 5 6 7)) (1 3 5 7) arc> (rem odd '(1 2 3 4 5 6)) (2 4 6) arc> (all odd '(1 3 5 7)) t arc> (some even '(1 3 5 7)) nil arc> (pos even '(1 2 3 4 5)) 1
If the first argument isn’t a function, it is treated like a function that tests for equality with that (again, from the Arc tutorial):
arc> (rem 'a '(a b a c u s)) (b c u s)
Since a table isn’t a function, if you pass it as the first argument, it gets treated as a value to test for equality with.
This patch changes Arc so that tables are instead treated like functions are:
arc> (rem (obj a t c t) '(a b a c u s)) (b u s)
In arc2 this evaluates to '(a b a c u s)
, since none of the elements of the list is the table. Of course, this means you can no longer check for a table in a list of tables without resorting to writing your own function to check for equality:
arc> (with (t1 (obj a 1) t2 (obj a 2) t3 (obj a 3)) (rem t2 (list t1 t2 t3))) (#hash((a . 1)) #hash((a . 3))) <--- arc2 (#hash((a . 1)) #hash((a . 2)) #hash((a . 3))) <--- with this patch
I use this patch in my code since I often want to do the former and have yet to need to do the latter, so this patch makes my code more concise.
By using git or patch, you can incorporate this patch into your version of Arc.
$ git pull git://github.com/CatDancer/arc.git tag arc2.testify-table0
For example,
$ mkdir arc $ cd arc $ git init $ git pull git://github.com/CatDancer/arc.git tag arc2.testify-table0
To apply this patch to your copy of arc using the patch command, download arc2.testify-table0.patch into your Arc directory, and at the Unix command line inside the Arc directory, type:
patch <arc2.testify-table0.patch
“patch
” is a standard utility that comes with Unix. (If you’re running Windows, iirc patch
comes with cygwin).
For example,
$ wget http://ycombinator.com/arc/arc2.tar [... downloading ...] $ tar xf arc2.tar $ cd arc2 $ patch <arc2.testify-table0.patch patching [...] $
The original Arc source is copyrighted by Paul Graham and Robert Morris and licensed under the Perl Foundations's Artistic License 2.0 as described in the “copyright” file in the Arc distribution.
My changes to Arc (this patch that I wrote) are in the public domain.
The combination of the original Arc and my changes together (what you get after you apply my patch) is also licensed under the Perl Foundations's Artistic License 2.0.