Share an Erlang or dev-env tip a day

..or as and when you can think of one :icon_cool:

I accidentally pasted my banking password into Terminal, so wanted to delete it to be on the safe side!

# close the app, then: 
rm ~/.zsh_history
rm -rf ~/.zsh_sessions

Edit: Or, as mentioned by @jswanner on EF, just edit the file to remove that line :see_no_evil_monkey:

Reverse a binary

reverse(Bin) →
Size = byte_size(Bin) * 8,
<<X:Size/integer-little>> = Bin,
<<X:Size/integer-big>>.

Binary concatenation

<<_:8, BinStr/binary>> = <<<<“,”, OneB/binary>> || OneB ← BinaryList>>,
BinStr.

3 Likes

A safer version (depending on how big the integer is of Bin):

reverse(<<>>) -> <<>>;
reverse(Bin) ->
    %% available since OTP 14! Similar 1-6ms on big input
    binary:encode_unsigned(binary:decode_unsigned(Bin, 'big'), 'little').

We found that if the binary is > 562,868 characters, your reverse/1
(which we had a version of too) would blow up.

%% other implementations we tried
%% -define(REV_CHUNK, 8192). % 2^13
%% reverse(<<Front:?REV_CHUNK/integer-little, Rest/binary>>) ->
%%     list_to_binary(reverse(Rest, [<<Front:?REV_CHUNK/integer-big>>]));
%% reverse(Binary) ->
%%     %% this fails for large binaries
%%     Size = bit_size(Binary),
%%     <<X:Size/little-integer>> = Binary,
%%     <<X:Size/big-integer>>.

%% %% side note: it was observed that creating a binary as the
%% %% accumulator caused execution time to range 30-60ms while using the
%% %% list as accumulator and converting to binary at the end caused
%% %% execution time to range 1-6ms typically, hence the list of binaries here
%% reverse(<<Front:?REV_CHUNK/integer-little, Rest/binary>>, Acc) ->
%%     reverse(Rest, [<<Front:?REV_CHUNK/integer-big>> | Acc]);
%% reverse(Rest, Acc) ->
%%     [reverse(Rest) | Acc].

%% execution for the above typically is less than 10ms on "large"
%% (byte_size = 562868 B) payloads

%% anoother implementation:
%% this one took 9007807 (or 9 seconds) on the big input
%% reverse(Binary) ->
%%     reverse(Binary, <<>>).
%% reverse(<<>>, Acc) -> Acc;
%% reverse(<<H:1/binary, Rest/binary>>, Acc) ->
%%     reverse(Rest, <<H/binary, Acc/binary>>).

%% this took a range of 15..64 ms on the big input
%%    list_to_binary(lists:reverse(binary_to_list(Binary))).
2 Likes

So this particular implementation method is not applicable. So, is this error message abnormal? It seems that this byte-sized binary data should not have exceeded any limitations. I have tested the function integer_to_binary. If the integer is very large, it will run very slowly. However, it should have nothing to do with the above issue.

1 Like
reverse(Bin) ->
    Size = byte_size(Bin),
    <<X:Size/integer-little-unit:8>> = Bin,
    <<X:Size/integer-big-unit:8>>.
2 Likes

It still return an error.

utBinary:reverse(list_to_binary(lists:duplicate(600000, 1))).
** exception error: no match of right hand side value
<<1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,…>>
in function utBinary:reverse/1 (utBinary.erl:10)

1 Like

This one probably falls under dev-env, Wrote up a quick example on how to use emacs and erlang with a remote shell during development.

1 Like