如何加入位串列表?

tar*_*aro 5 erlang

有一个位串列表,需要加入一个位串:

join(Bs) ->
    F = fun(B, Bt) -> <<B/bitstring, Bt/bitstring>> end,
    lists:foldr(F, <<>>, Bs).
Run Code Online (Sandbox Code Playgroud)

你能建议更快的方法吗?

Ric*_*rdC 5

您可以使用二进制理解:

join(Bs) = << <<B/bits>> || B <- Bs >>.
Run Code Online (Sandbox Code Playgroud)

例如,在 shell 中尝试以下操作:

1> <<N:16>> = << <<B/bits>> || B <- [<<1:4>>, <<2:4>>, <<3:4>>, <<4:4>>] >>.
<<18,52>>
2> io:format("~.16#~n", [N]).
16#1234
Run Code Online (Sandbox Code Playgroud)