view 5-Erlang/translate_sync.erl @ 93:39084e2b8744

Add a function for word-aware text wrapping Potentially hugely inefficient because we iterate through the string character by character, but then splitting it first and iterating over words still needs to iterate over the string to know where to split.
author IBBoard <dev@ibboard.co.uk>
date Tue, 18 Jun 2019 21:05:00 +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.