[erlang-questions] list:join() for erlang?

Bob Ippolito bob@REDACTED
Fri Sep 14 11:11:46 CEST 2007


On 9/13/07, ok <ok@REDACTED> wrote:
> On 14 Sep 2007, at 8:38 am, Bob Ippolito wrote:
> > Using ++ all over the place is probably a bad idea, at least according
> > to the performance guide and my intuition. I haven't profiled it.
>
> I believe you have misunderstood the performance guide.
> When you want to concatenate lists, ++ is the tool for the job.

Sure, it works great for concatenating two lists, but it's not
necessarily great for O(N^2) lists. Given that most of them will
probably be short, I absolutely concede that ++ could very well be a
performance winner in some cases. However, given that lists:reverse is
special-cased I could imagine that lists:flatten could be also at some
point and then the performance landscape could change dramatically.
Either way, as I said before, this is absolutely not a bottleneck in
our code so we didn't try very many implementations.

> >
> > This is our join/2, which is largely the same but uses lists:flatten/1
> > instead of ++:
>
> But that changes the semantics.  It will accept stuff that I did not
> at all wish my code to accept.

lists:append/1 could be used in this case, but I saw no good reason to
specialize. If your code has incorrect input somewhere a library
function such as this is probably not the best place to figure that
out.

> > join([S], _Separator) ->
> >     lists:flatten(S);
>
> This does *more* copying than the ++ version.

This is a degenerate case where practically nothing happens in any
version, so it's hardly worth mention.

-bob



More information about the erlang-questions mailing list