sandbox/pe/chapter7.erl

54 lines
1.8 KiB
Erlang
Raw Normal View History

2019-04-25 17:36:15 +00:00
-module(chapter7).
-export([breverse/1, term_to_packet/1, packet_to_term/1, reverse_byte/1,
reverse_bits/1, reverse_bitlist/1, test/0]).
%% Exercises
%% 1. Write a function that reverses the order of bytes in a binary.
breverse(Bin) ->
binary:list_to_bin(lists:reverse(binary:bin_to_list(Bin))).
%% 2. Write a function term_to_packet(Term) -> Packet that returns a
%% binary consisting of a 4-byte length header N followed by N
%% bytes of data produced by calling term_to_binary(Term).
%% Need to packetize
term_to_packet(Term) ->
2019-04-26 02:18:39 +00:00
BinaryTerm = term_to_binary(Term),
Length = byte_size(BinaryTerm),
2019-04-25 17:36:15 +00:00
<<Length:32, BinaryTerm:Length/bytes>>.
%% 3. Write the inverse function packet_to_term(Packet) -> Term that
%% is the inverse of the previous function.
packet_to_term(<<Length:32, BinaryTerm:Length/bytes>>) ->
2019-04-26 02:18:39 +00:00
binary_to_term(BinaryTerm).
2019-04-25 17:36:15 +00:00
packet_test(Term) ->
Term = packet_to_term(term_to_packet(Term)).
%% 4. Write some tests in the style of Adding Tests to Your Code, on
%% page 46, to test that the previous two functions can correctly
%% encode terms into packets and recover the original terms by
%% decoding the packets.
test() ->
Squared = fun (X) -> X * X end,
packet_test(Squared),
packet_test(32),
packet_test(<<"Goodbye, Joe.">>),
2019-04-26 02:18:39 +00:00
<<0:7, 1:1>> = reverse_byte(<<1:1, 0:7>>),
<<246, 54, 54, 166, 22>> = reverse_bits(<<"hello">>).
2019-04-25 17:36:15 +00:00
%% 5. Write a function to reverse the bits in a binary.
2019-04-26 02:57:56 +00:00
%% This feels pretty inelegant.
2019-04-25 17:36:15 +00:00
reverse_byte(<<A:1, B:1, C:1, D:1, E:1, F:1, G:1, H:1>>) ->
<<H:1,G:1,F:1,E:1,D:1,C:1,B:1,A:1>>;
reverse_byte(<<>>) -> <<>>.
reverse_bitlist([]) -> [];
reverse_bitlist([H|T]) ->
2019-04-26 02:18:39 +00:00
[reverse_byte(<<H>>) | reverse_bitlist(T)].
2019-04-25 17:36:15 +00:00
reverse_bits(Binary) ->
2019-04-26 02:18:39 +00:00
ReversedBits = reverse_bitlist(binary_to_list(Binary)),
erlang:list_to_binary(lists:reverse(ReversedBits)).