February 17, 2008
STRING+ for the rest of us
About two weeks ago Franz, Inc. announced a new string utility function named STRING+ for their Common Lisp implementation.
It embodies the string concatenation paradigm of CONCATENATE, which is orthogonal to that of FORMAT. Some people don’t like FORMAT at all, but I think both have their uses.
I absolutely hate using the concatenation way for doing things like (here in JavaScript/Java/C++):
"You ate " + A + " apples, " + B + " bananas and " + C + " " + OTHER_THING + "."
The way the punctuation marks are placed here drives me totally nuts, and I’d rather prefer a lovely
"You ate ~A apples, ~A bananas and ~A ~A." A B C OTHER_THING
But the former method is useful for building file system paths and URLs, for example (MERGE-PATHNAMES aside).
So basically STRING+ is a shorthand for CONCATENATE ‘STRING. Franz claims speed improvements for small values, but I don’t need that. Short and sweet, here’s the functional equivalent:
(defun string+ (&rest parts) (with-output-to-string (out) (dolist (part parts) (write part :stream out :escape nil))))
Works as advertised, except for the example with the keyword symbol, where case may vary depending on your implementation’s default case conventions (CLISP prints it uppercase).
Comments(8)
string+ is in my toolbox since I started toying with cl-who.
Ah, where did you get it from?
Well I thought about wating for Santa, but considering he pretty much sucks with documentation, christmas was far away, and I’m known for being a bad hm kid. So I hold a meeting with me, myself and I and after a hell of a debate I decided to write it myself. Which I did.
Yeah, I know, my question was phrased badly. :) What I actually want to know is, how did you get the idea for this? Did you make it up wholly on your own?
Are you mocking me? For McCarthy’s sake I didn’t made a full blown reverese proxy with per OS load balancers. I made 2 liner utility . For everyone who played with cl-who and and clsql before things like weblocks, elephant, postmodern etc would need such thing to save some typing and got logical mapping of it’s code.
Just different viewpoints, I guess.
For me, STRING+ isn’t really essential. FORMAT NIL and CONCATENATE ‘STRING did the job well enough.
I only got the idea for this sometimes-nice-to-have thing when I read Franz’s announcement.
So basically I was just curious :)
Could you live without it? Of course.
Why I made it? Because concatenate is generic thing for concatening sequences, while string+ matches nice into my model. Anyway it proved itself worthy even for a simple wrapper.
I have long maintained that + is a silly name for string concatenation: 1+2=2+1=3, not 12. Concatenation isn’t the equivalent for strings, it’s just borrowing a piece of punctuation.
FWIW, I’ve been using S. for exactly the same thing since forever (I think it’s in Araneida somewhere). #\. is from the Perl operator, #\S is mnemonic for “string” and to avoid gymnastics with input syntax.