awwx.ws

alistcall

Use x!foo for association list lookups

diff -u a/ac.scm b/ac.scm
--- a/ac.scm	2009-11-21 19:15:40.000000000 -0500
+++ b/ac.scm	2009-11-21 19:23:39.000000000 -0500
@@ -646,11 +646,14 @@
 ; default vals with them.  To make compatible with existing written tables, 
 ; just use an atom or 3-elt list to keep the default.
 
+(defarc alref arc-alref)
+
 (define (ar-apply fn args)
   (cond ((procedure? fn) 
          (apply fn args))
         ((pair? fn) 
-         (list-ref fn (car args)))
+         ((if (number? (car args)) list-ref arc-alref)
+          fn (car args)))
         ((string? fn) 
          (string-ref fn (car args)))
         ((hash-table? fn) 

In arc3, lists can be called with an integer argument and will return the item in the list at that position:

arc> ('(a b c d e) 2)
c

Since calling a list with a non-integer argument results in an error, this means that we could make calling lists with other types do something useful, if we think of something.

I was inspired by thaddeus’ post about adding a syntax to Arc for lookups in association lists to think that perhaps we could have calling a list with a non-integer do an association list lookup.

I often use tables instead of association lists merely because with tables I can use the x!foo notation.

With this patch the x!foo notation will also work for association lists, as long as “foo” is a symbol or something else that isn’t a number.

It doesn’t bother me that x!2 still returns the item at that position in the list, since when I'm using a table (or now an association list) as an “object”, a container of named values, my keys are symbols anyway.

Taking thaddeus’ example:

arc> (= x '((first "First Name")(last "Last Name")))
((first "First Name") (last "Last Name"))
arc> x!first
"First Name"
arc> x!last
"Last Name"

Note that this patch doesn’t implement setting values in an association list. I.e.,

(= alist!foo 3)

doesn’t work.

Get this hack

Download alistcall0.patch and its prerequisite: defarc.

Or, using the hackinator:

hack ycombinator.com/arc/arc3.1.tar \
  awwx.ws/defarc0.patch \
  awwx.ws/alistcall0.patch

unLicense

This patch is in the public domain. The combination of this patch and Arc may be redistributed or modified under the same terms as Arc itself.

Contact me

Twitter: awwx
Email: andrew.wilcox [at] gmail.com