String join with
From Erlang Community
[edit] Problem
You need to combine several items of a list into a string using a separator between each item representation.
In python, this is done by:
Python example |
python> ','.join(['a', 'b', 'c']) 'a,b,c' python> ','.join(map(str, [1,2,3])) '1,2,3' |
[edit] Solution
This solution interleave the input list with as many copy of the separator, then use the builtin concat function to create the final string:
Erlang code |
string_join(Join, L) ->
string_join(Join, L, fun(E) -> E end).
string_join(_Join, L=[], _Conv) ->
L;
string_join(Join, [H|Q], Conv) ->
lists:flatten(lists:concat(
[Conv(H)|lists:map(fun(E) -> [Join, Conv(E)] end, Q)]
)).
|
Usage is as follow:
Erlang example |
erl> test:string_join(",", ["1", "2", "3"]).
"1,2,3"
erl> test:string_join(",", [1, 2, 3], fun(X) -> io_lib:format("~B", [X]) end).
"1,2,3"
|
[edit] Solution (alternative)
There is another code which is simplier to read and works faster for me (goryachev):
Erlang code |
my_string_join(Items, Sep) ->
lists:flatten(lists:reverse(my_string_join1(Items, Sep, []))).
my_string_join1([Head | []], _Sep, Acc) ->
[Head | Acc];
my_string_join1([Head | Tail], Sep, Acc) ->
my_string_join1(Tail, Sep, [Sep, Head | Acc]).
|
Usage:
Erlang code |
erl> string_join:my_string_join(["123", "456", "789"], $,). "123,456,789" |
And here is a benchmark:
Erlang code |
erl> Items = ["bar foo" || _ <- lists:seq(1, 100000)], ok.
ok
erl> {Time, _} = timer:tc(string_join, string_join, [",", Items]), Time.
163565
erl> {MyTime, _} = timer:tc(string_join, my_string_join, [Items, $,]), MyTime.
87379
|

Digg It
Del.icio.us
Reddit
Facebook
Stumble Upon
Technorati
Click here to order from amazon.com