I want to do what title says:
{"a","b",1,2,3,4,"a",2,2,2,"edg","?"} => {"ab",1,2,3,4,"a",2,2,2,"edg?"}
in the most elegant way. For example one replacement rule for ReplaceAll
.
I have manage to do this:
test = {"a", "a", 1, 2, "A", "b", 123, "a", "end"}SplitBy[test, Head] /. {x__String} :> StringJoin[{x}] // Flatten
{"aa", 1, 2, "Ab", 123, "aend"}
But I find this way inelegant. Could You help me with creating good pattern?
Edit
Bonus request: It should be comparable in duration.
Also there is not need to do this via pattern-matching.
Conclusion:
There are many great answers. Each one is educational and worth an upvote. I should now accept one, the problem is I have not stated this question's goal so clearly, my fault.
- rm -rf's is the fastes and the shortest however,
- I decided to accept Jacob's answer
as the fastest one andbecause it seems it has not focused attention as it deserves. - Yves Klett answer fulfill my need of a single pattern solution.
As Mr. Wizard pointed out one could think I should accept rm- rf's answer. One could also think Yves Klett should receive it as his answer fits my primordial need.I will abuse a vague form of this question to give them a bounty insted.