Arc Forumnew | comments | leaders | submitlogin
Use x!foo with association lists (awwx.ws)
6 points by aw 2942 days ago | 5 comments


1 point by thaddeus 2942 days ago | link

Awesome! This is much better than my hack.

Too bad (= x!first "new") didn't work, but I still like yours better as it's cleaner not to have a pre-symbol.

Also note, (and I hope I am not cuttering your post) I also created a template like abstraction called 'arrange'for alists:

  (= arrangements* (table))

  (mac arrange (item . fields)
    (withs (name (carif item) includes (if (acons item) (cdr item)))
      `(= (arrangements* ',name) 
          (+ (mappend arrangements* ',(rev includes))
             (list ,@(map (fn ((k v)) `(list ',k (fn () ,v)))
                          (pair fields)))))))

  (def actuate (item . args)
    (let x nil
      (each (k v) (rev (if (acons item) item (arrangements* item)))
        (unless (no v) (push (list k (v)) x)))
      (each (k v) (pair args)
        (push (list k (v)) x))
      x))

It's just a copy of pgs templates only instead of creating a table it instantiates alists. This allows me to store the order of things (even for tables).

example use:

  arc> (arrange author first "First Name" last "Last Name")
   ((first #<procedure: gs6338>) (last #<procedure: gs6338>))
  
  arc> (= x (actuate 'author))
  ((first "First Name") (last "Last Name"))

  arc> x!first
  "First Name"
I've used this to output large tables in CSV format, where the header and sort order is derived from an 'arrangement'.

and now I can use the intra symbols too! T

-----

2 points by aw 2942 days ago | link

Too bad (= x!first "new") didn't work

hmm, it seems like the association list thing to do for a set would be to push a new pair onto the front of the association list, so that

  (= x!foo 'bar)
would be like saying

  (push '(foo bar) x)
when x was a cons and foo wasn't a number. But I don't know how to do that, or if it's possible.

On the other hand, if you want to modify existing associations in place:

  (extend sref (com val ind) (and (acons com) (isnt (type ind) 'int))
    (aif (assoc ind com) (= (cadr it) val)))

  arc> (= x '((a "A") (b "B") (c "C")))
  ((a "A") (b "B") (c "C"))
  arc> (= x!b "X")
  "X"
  arc> x
  ((a "A") (b "X") (c "C"))
though that won't let you add new keys to the alist.

-----

1 point by thaddeus 2934 days ago | link

lol. I've tried to vote for this post like 4 times. On the first day posted and today. I think andrew must have triggered the 'adding-too-much-value-thresh-hold' in pg's voting algorithm. :) or I've somehow triggered pg's 'newb-alert+ban-thresh-hold'.

-----

2 points by aw 2934 days ago | link

or maybe voting ring detection code I wonder?

-----

1 point by akkartik 2934 days ago | link

Hmm, that may be why several of my upvotes on your stories haven't seemed to register :)

-----