Arc Forumnew | comments | leaders | submitlogin
A reader macro abstraction over 'setforms (github.com)
4 points by rocketnia 5296 days ago | 1 comment


2 points by rocketnia 5296 days ago | link

This thread continues an off-topic discussion in the thread "A new html engine, with custom tags and layout templates" (http://arclanguage.org/item?id=13233). Particularly, it would have been a reply to http://arclanguage.org/item?id=13293:

me: Maybe :y or %y could be the zapper (or some better-suited abstraction over setforms).

evanrmurphy: Maybe something with `=` in it?

Okay, =y it is. ^_^ I've implemented a working example at https://gist.github.com/772283 (and posted this thread as a link to it).

What place.arc provides is a reader macro such that =(a b c) reads as (place (a b c)) and results in a function that either gets or sets that place depending on whether an argument is passed to it.

Here are example implementations of '= and 'zap using this system:

  (mac = args
    `(do ,@(map (fn ((a b)) `(atomic (=,a ,b))) pair.args)))
  
  (def fn-zap (func place . rest)
    (do.place:apply func (do.place) rest))
  
  ; NOTE: The original evaluates the place before the function instead.
  (mac zap (func place . rest)
    `(atomic:fn-zap ,func =,place ,@rest))
You might as well just skip the 'zap macro and call the function 'zap, since the macro usually only saves one character per use. If you need 'atomic, just say (atomic:zap ...).

The '= macro only provides an occasional benefit too. Most of the time, you can save a character by saying (=foo bar) instead of (= foo bar).

It seems this abstraction over 'setforms not only shortens a lot of arc.arc definitions but also removes the need for some of them altogether.

---

You should know that place.arc is a little hackish in at least two ways:

- First, it still allows the symbol '= to be interpreted in a non-reader-macro way, but it does so using hard-coded special cases, and it doesn't count ssyntax like '=.variable-to-set-to-nil. The hackishness only exists with '=. If another symbol beginning with #\= is in your code, such as '=mc (an example from Lathe), it'll break by design because it'll read as (place mc). Meanwhile, symbols like 'or= and 'expand=list are just fine; I didn't make #\= a symbol-terminating character.

- Second, in order to simplify the design, the code modifies 'setforms so that it has slightly different behavior when given plain variable names. Particularly, (=unbound-var 2) won't cause an error. I believe no part of core Arc depends on the original 'setforms behavior. In fact, the implementation of 'expand= compensates for 'setforms in a similar way.

-----