Symbol-Munger: A common lisp library to help convert symbols between their representations in various systems

I just posted a small common lisp library to my github account named Symbol-Munger. Symbol-Munger provides functionality to ease conversion between the same symbol in different environments.

For example, when generating common lisp classes from a database schema, I want to change column names into lisp slot / accessor names, and then later when I am displaying that common lisp slot on the screen I want to display its slot-name as an english column header. (IE: my_db_col > my-db-col > My Db Col)

I have had this code laying about for years and use it everywhere frequently. Earlier today while cleaning other code, I ran across this TODO in vana-inflector, and realized I already had code that performed this function. This library contains one function that does the bulk of the work (normalize-capitalization-and-spacing) and many functions that set default arguments to that one (lisp->english, english->camel-case, english->keyword. etc). Its only dependency is iterate.

4 thoughts on “Symbol-Munger: A common lisp library to help convert symbols between their representations in various systems

  1. Is it roundtrip safe for lisp->camelcase->lisp and camelcase->lisp->camelcase?

  2. It is certainly the intent that it be round trip safe. I just added a test case that seems to prove this true (for that test case), and I don’t know of any places it would fail. I don’t guarantee it will work on every possible lisp symbol though (ie: I have not tested with ‘|some stuff in here| style symbols at all). If you do find a failure case, please open a github issue or submit a patch.

  3. I would put a big caveat emptor on this library. CL symbols *are* case-sensitive, and can (and do) contain underscores; in general the translations won’t be injective and certainly are not going to be bijective.

    I’ve started to use CamelCased symbols in my code when talking to JavaScript and am encouraging other people to do the same. IMO it’s easier to set readtable-case to :invert (or use named-readtables) and use consistent naming everywhere, than having to remember how something like XMLHttpRequest is supposed to map to Lisp conventions.

  4. While I appreciate what you are saying about common lisp symbols, I use this library to prevent having non-standard lisp symbol names (anything in pipes, or containing an underscore). All these functions are about converting between naming conventions, not what is possible in each environment. The convention for names in common lisp is name-parts-separated-with-hyphens, just like in javascript it is camelCasedNames, even though you could just as well use underscores_to_separate. As such I feel symbol-munger is not misrepresenting itself and is not likely to lead people astray. I have tried to include examples, and there is a test library containing at least one test for everything that is expected to work.

    Furthermore, if you had the lisp-symbol ‘|aNameInCamelCase| and you wanted to convert it you could call the correct converter (eg: camel-case->english), and everything would still work smoothly. You could even call camel-case->lisp-symbol to create a new symbol with standard lisp naming / casing.

    Also if you are using other language’s symbol conventions in common-lisp, then you probably wouldn’t need this library. Presumably you used those conventions to ease translating between the two systems, which is pretty much the only aim of this library.

    I have only used this library with readtable-case set to :UPCASE and there would probably need to be changes to support other values. That said, I don’t use other values for readtable-case and so have written no code to support other values (patches welcome).

Leave a Reply

Your email address will not be published. Required fields are marked *