Showing posts with label Erlang. Show all posts
Showing posts with label Erlang. Show all posts

Tuesday, May 16, 2017

It's a notes about Erlang. It's a generator that uses a lazy calculating.

Hello my young Erlang programmers!























































It's a result (I apologize for the strange syntax highlighting):

bodro@vbodrov-pc:~/workspace/develop/test/test58$ erl
Erlang/OTP 18 [erts-7.3] [source] [64-bit] [smp:8:8] [async-threads:10] [kernel-poll:false]

Eshell V7.3  (abort with ^G)
1> c(test).
{ok,test}
2> test:test_all().
TEST #1
0:10   -> [0,1,2,3,4,5,6,7,8,9]
1000:5 -> [1000,1001,1002,1003,1004]
1:2    -> [1,2]
1:1    -> [1]
1:0    -> []
TEST #2
0:10   -> [0,1,2,3,4,5,6,7,8,9]
1000:5 -> [1000,1001,1002,1003,1004]
1:2    -> [1,2]
1:1    -> [1]
1:0    -> []
TEST #3
0:10   -> [0,1,2,3,4,5,6,7,8,9]
1000:5 -> [1000,1001,1002,1003,1004]
1:2    -> [1,2]
1:1    -> [1]
1:0    -> []
TEST #4
0:10   -> true
1000:5 -> true
1:2    -> true
1:1    -> true
1:0    -> true
TEST #5
0:10   -> true
1000:5 -> true
1:2    -> true
1:1    -> true
1:0    -> true
TEST #6
0:10   -> true
1000:5 -> true
1:2    -> true
1:1    -> true
1:0    -> true
TEST #7
0:10   -> true
1000:5 -> true
1:2    -> true
1:1    -> true
1:0    -> true
ok
3> q().
ok
4> bodro@vbodrov-pc:~/workspace/develop/test/test58$

Best regards,
Vasiliy V. Bodrov aka Bodro,
May 16, 2017.

How do you swap two variable?

Hi!

How do you swap two variable?

Look at this! It's a C++ code.
#include <iostream>

using namespace std;

inline void swap_var(int& _x, int& _y) {
    int _ = _x; _x = _y; _y = _;
}

inline void swap_xor(int& _x, int& _y) {
    _x ^= _y; _y ^= _x; _x ^= _y;
}

inline void swap_math(int& _x, int& _y) {
    _x += _y; _y = _x - _y; _x -= _y;
}

int main(void) {
    int a = 5;
    int b = 77201;

    cout << a << ":" << b << endl;
    swap_var(a, b);
    cout << a << ":" << b << endl;
    swap_xor(a, b);
    cout << a << ":" << b << endl;
    swap_math(a, b);
    cout << a << ":" << b << endl;
}


It's the result:
$ rm -f t; g++ -Wall -Wextra -Werror -O2 t.cpp -o t; ./t;
5:77201
77201:5
5:77201
77201:5
$


This is a C code.
#include <stdlib.h>
#include <stdio.h>

void swap_var(int* _x, int* _y) {
    int _ = *_x; *_x = *_y; *_y = _;
}

void swap_xor(int* _x, int* _y) {
    *_x ^= *_y; *_y ^= *_x; *_x ^= *_y;
}

void swap_math(int* _x, int* _y) {
    *_x += *_y; *_y = *_x - *_y; *_x -= *_y;
}

int main(void) {
    int a = 5;
    int b = 77201;

    (void) printf("%d:%d\n", a, b);
    swap_var(&a, &b);
    (void) printf("%d:%d\n", a, b);
    swap_xor(&a, &b);
    (void) printf("%d:%d\n", a, b);
    swap_math(&a, &b);
    (void) printf("%d:%d\n", a, b);

    return EXIT_SUCCESS;
}


It's the result:
$ rm -f t; gcc -Wall -Wextra -Werror -O2 t.c -o t; ./t
5:77201
77201:5
5:77201
77201:5
$

And finally, Erlang :)
-module(t).
-export([t/0]).

swap({X, Y}) ->
    {Y, X}.

t() ->
    A = {1, 2},
    swap(A).

It's the result:
$ erl
Erlang/OTP 18 [erts-7.3] [source] [64-bit] [smp:8:8] [async-threads:10] [kernel-poll:false]

Eshell V7.3  (abort with ^G)
1> c(t).
{ok,t}
2> t:t().
{2,1}
3> q().
ok
4> $

Best regards,
Vasiliy V. Bodrov aka Bodro,
May 16, 2017.

Friday, May 12, 2017

It's a notes about Erlang. It's a work with "crypto" (DES CBC).

Hi, my readers!

I continue the experiments with Erlang. Today I want to introduce a code that works with library "crypto".
I'm trying to encrypt any strings. I'm using DES CBC algorithm.

Look at this!

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Module: des.erl
% Description: it's a program for a work with crypto module (tests)
% Author: Vasiliy V. Bodrov aka Bodro (bodrov@qtech.ru)
% Date: it's the 19-th of April, 2017 year
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The MIT License (MIT)
%
% Copyright (c) 2017 Vasiliy V. Bodrov aka Bodro
%
% Permission is hereby granted, free of charge, to any person obtaining a
% copy of this software and associated documentation files (the "Software"),
% Software is furnished to do so, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included
% in all copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
% OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
% MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
% IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
% CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
% OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
% THE USE OR OTHER DEALINGS IN THE SOFTWARE.
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% See: http://directory.fsf.org/wiki/License:Expat
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

-module(des).
-export([test/0, license/0]).
-vsn(1.0).

-define(BLOCK_SIZE, 8).
-define(INPUT_PROMPT, "    Input data: ").

test() ->
    io:format("*****************************************************~n", []),
    io:format("TEST #1:~n~n", []),
    test_impl(),
    io:format("*****************************************************~n", []).

test_impl() ->
    Key = <<16#01,16#23,16#45,16#67,16#89,16#ab,16#cd,16#ef>>,
    IVec = <<16#12,16#34,16#56,16#78,16#90,16#ab,16#cd,16#ef>>,
    Str = io:get_line(?INPUT_PROMPT),
    P = normalize_str(Str),
    C = crypto:block_encrypt(des_cbc, Key, IVec, P),
    R = crypto:block_decrypt(des_cbc, Key, IVec, C),
    print({P, C, R}).

print({P, C, R}) ->
    io:format("    --------------------------------------------~n", []),
    io:format("    Source: ~p~n", [P]),
    io:format("    --------------------------------------------~n", []),
    io:format("    Source as string: ~p~n", [denormalize_str(P)]),
    io:format("    --------------------------------------------~n", []),
    io:format("    Encrypted: ~p~n", [C]),
    io:format("    --------------------------------------------~n", []),
    io:format("    Decrypted: ~p~n", [R]),
    io:format("    --------------------------------------------~n", []),
    io:format("    Decrypted as string: ~p~n", [denormalize_str(erlang:binary_to_list(R))]).

list_size(L) when is_list(L) ->
    list_size_impl(L, 0).

list_size_impl([], S) ->
    S;
list_size_impl([_|T], S) ->
    list_size_impl(T, S + 1).

normalize_str([]) ->
    [];
normalize_str(L) when is_list(L)->
    C = list_size(L) rem ?BLOCK_SIZE,
    normalizator(L, ?BLOCK_SIZE - C).
    
normalizator(L, 0) ->
    L;
normalizator(L, N) ->
    normalizator(L ++ [0], N - 1).

denormalize_str([]) ->
    [];
denormalize_str(L) when is_list(L) ->
    denormalizator(L, []).

denormalizator([], X) ->
    X;
denormalizator([0|_], X) ->
    X;
denormalizator([10|_], X) ->
    X;
denormalizator([H|T], X) ->
    denormalizator(T, X ++ [H]).

license() ->
    io:format(" The MIT License (MIT)~n"),
    io:format("~n"),
    io:format(" Copyright (c) 2017 Vasiliy V. Bodrov aka Bodro~n"),
    io:format("~n"),
    io:format(" Permission is hereby granted, free of charge, to any person obtaining a~n"),
    io:format(" copy of this software and associated documentation files (the \"Software\"),~n"),
    io:format(" Software is furnished to do so, subject to the following conditions:~n"),
    io:format("~n"),
    io:format(" The above copyright notice and this permission notice shall be included~n"),
    io:format(" in all copies or substantial portions of the Software.~n"),
    io:format("~n"),
    io:format(" THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS~n"),
    io:format(" OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF~n"),
    io:format(" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.~n"),
    io:format(" IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY~n"),
    io:format(" CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT~n"),
    io:format(" OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR~n"),
    io:format(" THE USE OR OTHER DEALINGS IN THE SOFTWARE.").
    
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% End of file
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



It's a result:
bodro@vbodrov-pc:~/workspace/develop/test/test56$ erl
Erlang/OTP 18 [erts-7.3] [source] [64-bit] [smp:8:8] [async-threads:10] [kernel-poll:false]

Eshell V7.3  (abort with ^G)
1> des:license().
 The MIT License (MIT)

 Copyright (c) 2017 Vasiliy V. Bodrov aka Bodro

 Permission is hereby granted, free of charge, to any person obtaining a
 copy of this software and associated documentation files (the "Software"),
 Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included
 in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
 OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 THE USE OR OTHER DEALINGS IN THE SOFTWARE.ok
2> des:test().
*****************************************************
TEST #1:

    Input data: Hello, world!
    --------------------------------------------
    Source: [72,101,108,108,111,44,32,119,111,114,108,100,33,10,0,0]
    --------------------------------------------
    Source as string: "Hello, world!"
    --------------------------------------------
    Encrypted: <<202,49,22,168,11,91,77,221,161,37,37,51,151,179,205,184>>
    --------------------------------------------
    Decrypted: <<72,101,108,108,111,44,32,119,111,114,108,100,33,10,0,0>>
    --------------------------------------------
    Decrypted as string: "Hello, world!"
*****************************************************
ok
3> q().
ok
4> bodro@vbodrov-pc:~/workspace/develop/test/test56$

See you later! Bye! :)

Best regards,
Vasiliy V. Bodrov aka Bodro,
May 12, 2017.

Wednesday, May 10, 2017

It's a notes about Erlang. Do you know how do quicksort works in Erlang?

Hello, my friends!

Do you know how do quicksort works in Erlang? I created a test code for this.




























It's a result in Erlang VM (BEAM):

bodro@vbodrov-pc:~/workspace/develop/test/test60$ erl
Erlang/OTP 18 [erts-7.3] [source] [64-bit] [smp:8:8] [async-threads:10] [kernel-poll:false]

Eshell V7.3  (abort with ^G)
1> c(test).
{ok,test}
2> test:test_a().
[-10000,-7,0,0,1,2,3,4,4,5,6,6,7,8,9,25,100,104,1000]
3> test:test_d().
[1000,104,100,25,9,8,7,6,6,5,4,4,3,2,1,0,0,-7,-10000]
4> q().
ok
5> bodro@vbodrov-pc:~/workspace/develop/test/test60$

Best regards,
Vasiliy V. Bodrov aka Bodro,
the 10-th of May, 2017 year.


How to reverse singly linked list in C (trivial way)

There is a beautiful morning. It's snowing. And I decided to write one more article. Today it will be just simple article where I would...