Version 10 of atoms

Updated 2007-05-22 16:03:03 by escargo

Richard Suchenwirth 2007-05-22 - Some languages (like Lisp) have the concept of "atoms" which are values (often strings) stored in only one place, and all uses of such values share the same representation. This reduces memory needs for often-repeated strings, and allows fast comparison - two atoms are only equal if they have the same address (on the C side). Here's a quick sketch how to do that in Tcl:

 proc atom x {
    if {[catch set ::Atom($x)]} {
        set ::Atom($x) $x
    } else {set ::Atom($x)}
 }

#-- Testing

 set res {}
 foreach test {foo bar grill bar foo} {lappend res [atom $test]}
 parray Atom

Disadvantage: If used uncritically, the Atom array may grow considerably in long-running apps. Values that are unlikely to occur repeatedly should better not be "atomized". For instance, in a library database application, author or publisher names or years are well worth sharing with atoms, but books' titles probably not.

escargo - What would be the difference between [catch set ::Atom($x)] and [catch {set ::Atom($x)}]?


Category Concept