Hmmm. Interesting idea, but I think it might be a little bit nuanced for the '+ symbol. I suppose if you make it work exactly the same way that it does for alists, then it would make sense, but otherwise you might want more general database functions, so that you can do different types of compositions.
What if you wanted to add the values of the fields? I.e.:
arc> (+ (obj a 3 b 2) (obj a 4 b 1))
#hash((a . 7) (b . 3))
What you describe seems to be a mapping -- you're mapping the polymorphic '+ on each pair of values in the table.
Extending the idea of the polymorphic '+ on lists to include tables suggests what CatDancer proposed in the original post.
So I wrote up the following sketchy hacks of '+ for tables:
; arc3/tab-tests.arc
(def table+ tbs
(listtab (apply +
(map tablist
(rev tbs)))))
;; Conceptually transparent, but try to avoid
;; thinking about the wastefulness...
(def table+-better tbs
(w/table tb-new
(each tb (rev tbs)
(ontable k v tb
(= (tb-new k) v)))))
;; Much better! Has a nice profile too :)
;; The rest is a "throw-away" test for the code
;; (require 'human-at-monitor-to-compare-tests)
(with (ta (obj a 'ta
b 'ta)
tb (obj b 'tb
c 'tb)
tc (obj c 'tc
a 'tc))
(map prn `("Consecutive lines should be identical:"
,(table+ ta tb tc)
,(table+-better ta tb tc)
,(table+ tb tc ta)
,(table+-better tb tc ta)
,(table+ tc ta tb)
,(table+-better tc ta tb))))
; Arc REPL
Use (quit) to quit, (tl) to return here after an interrupt.
arc> (load "tab-tests.arc")
Consecutive lines should be identical:
#hash((a . ta) (b . ta) (c . tb))
#hash((a . ta) (b . ta) (c . tb))
#hash((a . tc) (b . tb) (c . tb))
#hash((a . tc) (b . tb) (c . tb))
#hash((a . tc) (b . ta) (c . tc))
#hash((a . tc) (b . ta) (c . tc))
nil
So, the functions work. However, to get this as the behavior of '+ for tables would require adding a clause in ac.scm, to the following definition (from line 710):
My definition uses a bunch of higher-level functions defined quite a ways into arc.arc, but I need to use them in a low-level function in ac.scm. Should I implement the functions by hand into ac.scm, or is there a better solution?
EDIT: Someday I'll manage to format my code correctly the first time around...
I guess putting the definition of + on hash tables into arc.arc makes sense. Either one of the algorithms is a more high-level procedure than the other behaviors of '+.
I like this 'redef macro. How come it's not in arc.arc already? I can't think of any reason to not have this be there, along with the definitions of other key macros like 'do, 'def, and 'mac.
Now, granted, this function actually contributes nothing to <insert large project here, including news.yc>. However, I think that this function strikes at the heart of the "hackable language" mentality, by making it much easier to quickly modify behavior on the fly. It also sends a message to somebody reading the source code: "One of the key principles in this language is that you can redefine behavior across the board with one macro call."
I think that for quick exploratory patches, it's a better solution than editing the original definition. If I want to modify this behavior, I just have one self-explanatory (redef + ...) form to edit, rather than having to sift through the original '+ definition. It is a clean solution, which adds hackability to the language.
'extend is great! However, I think it's more of a great library/patch, than something that should sit with the core functions. 'redef, on the other hand, is elegant, transparent ('extend is transparentially challenged...), and works well for quick hacks. 'extend seems to me to be more suited for setting in stone some behavior sketched out with 'redef (for example, the method-like behavior that you described in the original thread about 'extend http://arclanguage.org/item?id=8895)
The example you have of + on tables uses the algorithm that conses up a storm...
One thing I like about 'extend is that I can run it several times while developing an extension without my older, buggy versions remaining on the call chain. Try actually hacking with this version of 'redef, it turns out to be really quite a pain!
oops, this 'redef is now calling the previous 'redef, I'm stuck!
On the other hand I don't think the explicit separation in 'extend between the test of whether to apply the extension from the implementation of the extension has turned out to be all that useful, so maybe some combination of the two would be better.
I tried storing a list as each value in the table, and popping or pushing to/from the list in redef and undef, so that you could step backwards more than just one definition at a time. However, that made the entire thing much more unstable, because it turns out that 'push and 'pop rely on a bunch of functionality which breaks quite easily if you're messing with the internals.
So, this stripped down version is my little 'redef/'undef hack-pack.