1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
%%% -*- erlang-indent-level: 2 -*-
%%%-------------------------------------------------------------------
%%% File : bs_build.erl
%%% Author : Per Gustafsson <[email protected]>
%%% Purpose :
%%%
%%% Created : 12 Sep 2007
%%%-------------------------------------------------------------------
-module(bs_build).
-export([test/0]).
test() ->
<<0,1,2,3,4,5,6>> = Bin = << <<X>> || X <- lists:seq(0, 6)>>,
test(Bin).
test(Bin) ->
<<0,1,2,3,4,5,6,0,1,2,3,4,5,6>> = RealBin = multiply(Bin, 2),
<<6,5,4,3,2,1,0,6,5,4,3,2,1,0>> = reverse(RealBin),
RealBin = copy(RealBin),
RealBin = bc(RealBin),
ok.
multiply(Bin, 1) ->
Bin;
multiply(Bin, N) when N > 0 ->
<<(multiply(Bin, N-1))/binary, Bin/binary>>.
bc(Bin) ->
<< <<X>> || <<X>> <= Bin >>.
reverse(<<X, Rest/binary>>) ->
<<(reverse(Rest))/binary, X>>;
reverse(<<>>) -> <<>>.
copy(Bin) ->
copy(Bin, <<>>).
copy(<<X, Rest/binary>>, Bin) ->
copy(Rest, <<Bin/binary, X>>);
copy(<<>>, Bin) -> Bin.
|