Skip to content

Instantly share code, notes, and snippets.

@calopez
Last active April 5, 2017 02:43
Show Gist options
  • Select an option

  • Save calopez/1c375932829d88898a55d51b1e86d072 to your computer and use it in GitHub Desktop.

Select an option

Save calopez/1c375932829d88898a55d51b1e86d072 to your computer and use it in GitHub Desktop.

Revisions

  1. Carlos Andres Lopez revised this gist Apr 5, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion palin.erl
    Original file line number Diff line number Diff line change
    @@ -109,7 +109,7 @@ response(String) ->
    Be = if Is =:= true -> " is ";
    true -> " is not "
    end,
    {result, lists:flatten(["\"", String, "\"", Be, "palindrome"])}.
    {result, lists:flatten([$", String, $", Be, "palindrome"])}.


    %% ------
  2. Carlos Andres Lopez revised this gist Apr 5, 2017. 1 changed file with 31 additions and 3 deletions.
    34 changes: 31 additions & 3 deletions palin.erl
    Original file line number Diff line number Diff line change
    @@ -70,20 +70,48 @@ client() ->
    %% Server
    %% -------
    server() ->
    Workers = palin_workers(),
    receive
    {check, Pid, String} ->
    Pid ! response(String),
    {check, Client, Payload} ->
    lb(Workers, Client, Payload),
    server();
    stop -> io:format("stopped ~n")
    end.


    lb(Workers, Client, Payload) when is_pid(Client) ->
    Worker = element(rand:uniform(2), Workers),
    Worker ! {Client, Payload}.

    palin_workers() ->
    {
    spawn(fun worker1/0),
    spawn(fun worker2/0)
    }.


    worker1() ->
    receive
    {Client, Payload} ->
    io:format("response from Sever 1!~n"),
    Client ! response(Payload)
    end.

    worker2() ->
    receive
    {Client, Payload} ->
    io:format("response from Sever 2!~n"),
    Client ! response(Payload)
    end.

    response(String) ->
    Is = palindrome(String),
    Be = if Is =:= true -> " is ";
    true -> " is not "
    true -> " is not "
    end,
    {result, lists:flatten(["\"", String, "\"", Be, "palindrome"])}.


    %% ------
    %% Usage
    %% ------
  3. Carlos Andres Lopez created this gist Apr 3, 2017.
    94 changes: 94 additions & 0 deletions palin.erl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,94 @@
    -module(palin).
    -export([
    palin/1,
    nopunct/1,
    palindrome/1,
    client/0,
    server/0
    ]).

    % palindrome problem
    %
    % palindrome("Madam I\'m Adam.") = true

    palindrome(Xs) ->
    palin(nocaps(nopunct(Xs))).

    nopunct([]) ->
    [];
    nopunct([X|Xs]) ->
    case lists:member(X,".,\ ;:\t\n\'\"") of
    true ->
    nopunct(Xs);
    false ->
    [ X | nopunct(Xs) ]
    end.

    nocaps([]) ->
    [];
    nocaps([X|Xs]) ->
    [ nocap(X) | nocaps(Xs) ].

    nocap(X) ->
    case $A =< X andalso X =< $Z of
    true ->
    X+32;
    false ->
    X
    end.

    % literal palindrome

    palin(Xs) ->
    Xs == reverse(Xs).

    reverse(Xs) ->
    shunt(Xs,[]).

    shunt([],Ys) ->
    Ys;
    shunt([X|Xs],Ys) ->
    shunt(Xs,[X|Ys]).

    %% -------
    %% Client
    %% -------

    client() ->
    receive
    {send, Pid, Msg} ->
    Pid ! Msg;
    {result, Msg } ->
    io:format("client received: ~p~n", [Msg]),
    client();
    stop ->
    io:format("client stopped!")
    end.


    %% -------
    %% Server
    %% -------
    server() ->
    receive
    {check, Pid, String} ->
    Pid ! response(String),
    server();
    stop -> io:format("stopped ~n")
    end.

    response(String) ->
    Is = palindrome(String),
    Be = if Is =:= true -> " is ";
    true -> " is not "
    end,
    {result, lists:flatten(["\"", String, "\"", Be, "palindrome"])}.

    %% ------
    %% Usage
    %% ------
    %% 1> Server = spawn(palin, server, []).
    %% 2> Client = spawn(palin, client, []).
    %% 3> Server ! {check, Client, "Air an aria"}.
    %% 4> Server ! {check, self(), "Air an aria"}.
    %% 5> flush().