view 5-Erlang/translate_sync.erl @ 76:5860845f5dac

Create module with synchronous translation and notes on how to run
author IBBoard <dev@ibboard.co.uk>
date Sat, 03 Feb 2018 20:25:47 +0000
parents
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.