Re: Overloading StringJoin
- To: mathgroup at smc.vnet.net
- Subject: [mg109797] Re: Overloading StringJoin
- From: David Bailey <dave at removedbailey.co.uk>
- Date: Tue, 18 May 2010 02:01:53 -0400 (EDT)
- References: <hsr8al$bev$1@smc.vnet.net>
Mark Adler wrote:
> I use Join enough that I like to overload StringJoin to do it:
>
> Unprotect[StringJoin];
> x_List <> y_List := x~Join~y
> Protect[StringJoin];
>
> Then I can do this:
>
> {1, 2} <> {3, 4}
> {1, 2, 3, 4}
>
> Why doesn't Mathematica already do this? It seem like an obvious use
> of <>. Or is there a good reason that they don't do this, and
> therefore if I do, it makes me a bad person?
>
> Mark
>
Well it might make you unwise, though not bad :) I do occasionally
Unprotect built-in symbols, but I always ask myself first if there is an
easier way to achieve the same thing! The general problems associated
with doing this are:
1) The function you modify may get used within other code (including
within other built-in functions) and cause confusion.
2) It is very hard for others to read your code if the built-in
operations don't do what they should.
3) I suspect at least some functions may not behave consistently if
augmented in this way.
String and structural operations are separated in Mathematica, and on
the whole this makes code easier to read because you don't have to ask
yourself if the arguments to StringJoin are meant to be strings or not!
I guess what you really want is an operator to join lists - so why not
overload an otherwise unused binary operator - like
\[ReverseEquilibrium] to perform this task?
BTW, you can always make yourself a palette (or change the Mathematica
menu) of special characters for easy insertion.
David Bailey
http://www.dbaileyconsultancy.co.uk