UPDATE: This is a very old article and the discussion here is interesting for historical reasons only. DO NOT use this code. Milton Inostroza helpfully and correctly pointed out that there is a lists:droplast/1 function in stdlib now.
I wanted to drop the last element of a list. I didn't want a queue, I simply wanted to remove the last character of a string. Specifically I wanted to remove the \n from ref: refs/heads/master\n that comes back when reading the .git/HEAD file.
Code listing for trim.erl module:
-module(trim). -export([string_strip_right/1, reverse_tl_reverse/1, bench/0]). bench() -> [nbench(N) || N {N, bench(["a" || _ {{string_strip_right, lists:sum([ element(1, timer:tc(trim, string_strip_right, [String])) || _ string:strip(String, right, $\n). reverse_tl_reverse(String) -> lists:reverse(tl(lists:reverse(String))).
Benchmark transcript:
Erlang R13B04 (erts-5.7.5) [source] [64-bit] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false] Eshell V5.7.5 (abort with ^G) 1> trim:bench(). [{1,{{string_strip_right,11261},{reverse_tl_reverse,1000}}}, {1000, {{string_strip_right,55131},{reverse_tl_reverse,17915}}}, {1000000, {{string_strip_right,79881856}, {reverse_tl_reverse,119920353}}}] 2>