Say you have a list, array, vector, storage format doesn't matter:
std::vector<int> vec; vec.push_back(1); vec.push_back(17); vec.push_back(9);
And you want to format this list into a comma delimited string without the trailing comma, what is the best way to do this?
Example output:
1,17,9
In any language, with any list type. Post your answers here. Be sure to use code formatting for your answer, to make it easier for everyone to read.
<code language="lua"> --my code solution </code>
Comments
Absolutely trivial in
Absolutely trivial in Perl:
I figured there would be at
I figured there would be at least one language where this was the case. Make me wonder how exactly "join" is implemented tho.
Haskell
There might be slicker ways to do it, but here's a Haskell way:
From GHCi:
Haskell, again
I'm definitely not a haskell coder, by any stretch, but here's a stab at function by cases:
Very similar to any example from Simon Peyton Jones' "Taste of Haskell" talk: http://research.microsoft.com/en-us/um/people/simonpj/papers/haskell-tut...
Python
Similar idea in Python:
Ruby
Simple like perl
Minnow
A great example of why Minnow needs generics or dynamic types. That function should be parametric or be part of array:
I tried several different
I tried several different versions, including a templated function over the delimiter type. I settled for this since unnecessary templating is to be frowned upon (and it ran slightly slower).
This can easily be generalized to any sort of iterator, of course.
I made two versions. The second one runs slightly faster by my compiler but is "evil".
Here's a simpler one that's
Here's a simpler one that's more in CPP style.
Javascript, PHP, Groovy, Falcon, Boo, C#, Vala
Javascript, PHP, Groovy, Falcon and Boo are about as terse as Perl / Ruby / Python on this one:
Javascript:
PHP:
Groovy:
Boo:
Falcon:
C# is more verbose:
And Vala:
Oops. Typo on the C#, "return
Oops. Typo on the C#, "
return String.Join(",", result);" should have been "return String.Join(delimiter, result);". It will also except if you pass anything that can't be converted to a string (like an object[] with some nulls). In production I'd use something closer to the Vala code, a la:Better C# version
Rather than using an extra string object and the Substring method, this is better (also uses the fact that StringBuilder methods are composable):
in C# the solution is much
in C# the solution is much simpler
Kind of...but not really.
Eh...kind of. But the way you're using the Aggregate method, you'll end up with ",1,17,9". You want to use the form with an initializer (first list item) and (next) list item (ps. you had a typo capitalizing the ToString method as well):
Note that this will throw exceptions if you pass it arrays of nullable objects with actual nulls (or any other objects without a ToString method). Also, LINQ is nice and terse, but it doesn't always help readability.
Java
And a Java version (basically the same as Vala / C#):
NB. In the Vala, C#, Java versions, I'm appending the delimiter unconditionally, then truncating the final string to (length - delimiter length). This assumes that moving the end of string pointer once at the end (or updating the internal length variable, or whatever the implementation details may be) is less costly than checking some condition(s) once per loop to decide when to insert the delimiter.
Note also, that with Java,
Note also, that with Java, you can use StringUtils from Apache commons to get a join method that works similar to Perl, &c.
Clojure
of course
You can do it in-place as well:
Scala
From the REPL you can do:
Scheme
And, just to be really esoteric, in Scheme we could do something like:
Oops, forgot a require...
Ps. Need to "(require scheme/string)" first for that example to work.
Ocaml version
Here's an Ocaml version (might be a better way, I was just bored and hadn't tinkered with ML for a spell):
String.concat "," (List.map string_of_int [1;17;9]);;