[erlang-questions] Interleave binaries

Wes James <>
Sat Aug 6 06:40:12 CEST 2011


Hi,

On Fri, Aug 5, 2011 at 3:26 PM, Bob Cowdery <> wrote:
> Hi
>
> I'm wanting to interleave two binaries.
>
> Bin1 = <<0,1>>,
> Bin2 = <<2,3>>,
> << <<A,B,C,D>> || <<A,B>> <= Bin1, <<C,D>> <= Bin2 >>.
> <<0,1,2,3>>
>
> Bin1 = <<0,1,2,3>>,
> Bin2 = <<4,5,6,7>>,
> << <<A,B,C,D>> || <<A,B>> <= Bin1, <<C,D>> <= Bin2 >>.
> <<0,1,4,5,0,1,6,7,2,3,4,5,2,3,6,7>>
>
> whereas I want:
> <<0,1,4,5,2,3,6,7>>

<snip>

I don't know if this will suffice:

-------------------------------------------------

% interleave module

-module(ilm).

-export([il/2]).

% interleave function

il(<<A1,A2,RestA/binary>>, <<B1,B2,RestB/binary>>) ->
    Rest=il(RestA, RestB),
    <<A1, A2, B1, B2, Rest/binary>>;

il(<<>>, <<>>) -> <<>>.

-----------------------------------------------

Erlang R14B02 (erts-5.8.3) [source] [smp:2:2] [rq:2] [async-threads:0]
[hipe] [kernel-poll:false]

Eshell V5.8.3  (abort with ^G)
1> c(ilm).
{ok,ilm}
2> ilm:il(<<0,1,2,3>>, <<4,5,6,7>>).
<<0,1,4,5,2,3,6,7>>

----------------------------------------------

-wes



More information about the erlang-questions mailing list