In your function it wasn't clear to me why you were adding the 'expand' atom to the destination list T. Maybe something like this could do:<br><br><br>-module(zrle).<br>-export([decompress/1]).<br><br>decompress(Bin) when is_binary(Bin) -><br>
decompress(Bin, []).<br><br>decompress(<<0, Count, Tail/binary>>, Acc) -><br> decompress(Tail, [<<0:(Count * 8)>> | Acc]);<br>decompress(<<Head, Tail/binary>>, Acc) -><br> decompress(Tail, [Head | Acc]);<br>
decompress(<<>>, Acc) -><br> list_to_binary(lists:reverse(Acc)).<br><br><br>e.g.<br><br>1> zrle:decompress(<<1, 0, 5, 2, 3, 4, 0, 3>>).<br><<1,0,0,0,0,0,2,3,4,0,0,0>><br><br><br>
<br><div class="gmail_quote">On Sat, Jan 17, 2009 at 8:46 AM, Steve Davis <span dir="ltr"><<a href="mailto:steven.charles.davis@gmail.com">steven.charles.davis@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Problem - to decompress a binary where zeros are run-length encoded.<br>
The following seems to work but also seems ugly and causes unnecessary<br>
overhead.<br>
<br>
%% Decompress binary where Zeros are Run-Length Encoded<br>
decompress(Bin) when is_binary(Bin) -><br>
L = binary_to_list(Bin),<br>
F = fun(X, [H|T]) when H =:= expand -> lists:duplicate(X, 0) ++ T;<br>
(X, Acc) when X =:= 0 -> [expand] ++ Acc;<br>
(X, Acc) -> [X] ++ Acc<br>
end,<br>
Decompressed = lists:foldl(F, [], L),<br>
list_to_binary(lists:reverse(lists:flatten(Decompressed))).<br>
<br>
Any bids on an improvement?<br>
<br>
Thanks for any attention.<br>
<br>
Side note: the erlang bit syntax is *amazing* when you actually get<br>
down to doing something with IP packets<br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote></div><br>