Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
49
Task/Binary-strings/Erlang/binary-strings-1.erl
Normal file
49
Task/Binary-strings/Erlang/binary-strings-1.erl
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
-module(binary_string).
|
||||
-compile([export_all]).
|
||||
|
||||
%% Erlang has very easy handling of binary strings. Using
|
||||
%% binary/bitstring syntax the various task features will be
|
||||
%% demonstrated.
|
||||
|
||||
|
||||
%% Erlang has GC so destruction is not shown.
|
||||
test() ->
|
||||
Binary = <<0,1,1,2,3,5,8,13>>, % binaries can be created directly
|
||||
io:format("Creation: ~p~n",[Binary]),
|
||||
Copy = binary:copy(Binary), % They can also be copied
|
||||
io:format("Copy: ~p~n",[Copy]),
|
||||
Compared = Binary =:= Copy, % They can be compared directly
|
||||
io:format("Equal: ~p = ~p ? ~p~n",[Binary,Copy,Compared]),
|
||||
Empty1 = size(Binary) =:= 0, % The empty binary would have size 0
|
||||
io:format("Empty: ~p ? ~p~n",[Binary,Empty1]),
|
||||
Empty2 = size(<<>>) =:= 0, % The empty binary would have size 0
|
||||
io:format("Empty: ~p ? ~p~n",[<<>>,Empty2]),
|
||||
Substring = binary:part(Binary,3,3),
|
||||
io:format("Substring: ~p [~b..~b] => ~p~n",[Binary,3,5,Substring]),
|
||||
Replace = binary:replace(Binary,[<<1>>],<<42>>,[global]),
|
||||
io:format("Replacement: ~p~n",[Replace]),
|
||||
Append = <<Binary/binary,21>>,
|
||||
io:format("Append: ~p~n",[Append]),
|
||||
Join = <<Binary/binary,<<21,34,55>>/binary>>,
|
||||
io:format("Join: ~p~n",[Join]).
|
||||
|
||||
%% Since the task also asks that we show how these can be reproduced
|
||||
%% rather than just using BIFs, the following are some example
|
||||
%% recursive functions reimplementing some of the above.
|
||||
|
||||
%% Empty string
|
||||
is_empty(<<>>) ->
|
||||
true;
|
||||
is_empty(_) ->
|
||||
false.
|
||||
|
||||
%% Replacement:
|
||||
replace(Binary,Value,Replacement) ->
|
||||
replace(Binary,Value,Replacement,<<>>).
|
||||
|
||||
replace(<<>>,_,_,Acc) ->
|
||||
Acc;
|
||||
replace(<<Value,Rest/binary>>,Value,Replacement,Acc) ->
|
||||
replace(Rest,Value,Replacement,<< Acc/binary, Replacement >>);
|
||||
replace(<<Keep,Rest/binary>>,Value,Replacement,Acc) ->
|
||||
replace(Rest,Value,Replacement,<< Acc/binary, Keep >>).
|
||||
10
Task/Binary-strings/Erlang/binary-strings-2.erl
Normal file
10
Task/Binary-strings/Erlang/binary-strings-2.erl
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
215> binary_string:test().
|
||||
Creation: <<0,1,1,2,3,5,8,13>>
|
||||
Copy: <<0,1,1,2,3,5,8,13>>
|
||||
Equal: <<0,1,1,2,3,5,8,13>> = <<0,1,1,2,3,5,8,13>> ? true
|
||||
Empty: <<0,1,1,2,3,5,8,13>> ? false
|
||||
Empty: <<>> ? true
|
||||
Substring: <<0,1,1,2,3,5,8,13>> [3..5] => <<2,3,5>>
|
||||
Replacement: <<0,42,42,2,3,5,8,13>>
|
||||
Append: <<0,1,1,2,3,5,8,13,21>>
|
||||
Join: <<0,1,1,2,3,5,8,13,21,34,55>>
|
||||
Loading…
Add table
Add a link
Reference in a new issue