view 5-Erlang/translate_sync.erl @ 103:98be775c533c default tip

An odd "non-determinism" example from StackOverflow It is clever, but doesn't make much sense as to how it gets its results
author IBBoard <dev@ibboard.co.uk>
date Sun, 14 Jul 2019 13:44:13 +0100
parents 5860845f5dac
children
line wrap: on
line source

-module(translate_sync).
-export([loop/0]).
-export([translate/2]).

% Invoke with: Pid = spawn(fun translate_sync:loop/0).
% Send messages with: translate_sync:translate(Pid, Word).
% This is sync with no return value.
loop() ->
    receive
        { Pid, "casa" } ->
            Pid ! "house",
            loop();
        { Pid, "blanca" } ->
            Pid ! "white",
            loop();
        { Pid, _ } ->
            Pid ! "*blank stare*",
            loop()
end.

translate(To, Word) ->
    To ! { self(), Word },
    receive
        Translation -> io:format("~s~n", [Translation])
    end.