What the if?

Máté Marjai - Erlang Ireland 2016 January

 

@erlangireand

Helping to build software ideas, products, teams and companies.

@theproductworks

Forget all you know about conditionals

# greetings in Ruby

def greetings(name, language)
  greeting = if language == "EN"
               "Hi #{name}!"
             elsif language == "FR"
               "Salut #{name}!"
             elsif language == "HU"
               "Szia #{name}!"
             else
               "Hello #{name}!"
             end
  puts greeting
end

Greetings human

% greetings in Erlang with Ifs

greetings(Name, Locale) ->
    Greeting = if
                   Locale =:= "EN" -> "Hi " ++ Name ++ "!";
                   true ->
                       if
                           Locale =:= "FR"  -> "Salut " ++ Name ++ "!";
                           true ->
                               if
                                   Locale =:= "HU"  -> "Szia " ++ Name ++ "!";
                                   true -> "Hello " ++ Name ++"!"
                               end
                       end
               end,
    io:format(Greeting, []).

Literal translation

% greetings in Erlang with Ifs

greetings(Name, Locale) ->
    Greeting = if
                   Locale =:= "EN" -> "Hi " ++ Name ++ "!";
                   Locale =:= "FR" -> "Salut " ++ Name ++ "!";
                   Locale =:= "HU" -> "Szia " ++ Name ++ "!";
                   true            -> "Hello " ++ Name ++"!"
               end,
    io:format(Greeting, []).

Literal translation

% greetings in Erlang with case statement

greetings(Name, Locale) ->
    Greeting = case Locale of
                   "EN" -> "Hi " ++ Name ++ "!";
                   "FR" -> "Salut " ++ Name ++ "!";
                   "HU" -> "Szia " ++ Name ++ "!";
                   _    -> "Hello " ++ Name ++ "!"
               end,
    io:format(Greeting, []).

In case ... of

Guards

% greetings in Erlang with an internal function using guards

greetings(Name, Locale) ->
    Greeting = greetings_1(Name, Locale),
    io:format(Greeting, []).

greetings_1(Name, Locale) when Locale =:= "EN" ->
    "Hi " ++ Name ++ "!";
greetings_1(Name, Locale) when Locale =:= "FR" ->
    "Salut " ++ Name ++ "!";
greetings_1(Name, Locale) when Locale =:= "HU" ->
    "Szia " ++ Name ++ "!";
greetings_1(Name, _) ->
    "Hello " ++ Name ++ "!".

Using guards

% greetings in Erlang with pattern matching

greetings(Name, Locale) ->
    Greeting = greetings_1(Name, Locale),
    io:format(Greeting, []).

greetings_1(Name, "EN") -> "Hi " ++ Name ++ "!";
greetings_1(Name, "FR") -> "Salut " ++ Name ++ "!";
greetings_1(Name, "HU") -> "Szia " ++ Name ++ "!";
greetings_1(Name, _)    -> "Hello " ++ Name ++ "!".

Matching on Locale

% greetings in Erlang with pattern matching

greetings(Name, Locale) ->
    GreetingFmt = greetings_format(Locale),
    io:format(GreetingFmt, [Name]).

greetings_format("EN") ->  "Hi ~s !";
greetings_format("FR") ->  "Salut ~s !";
greetings_format("HU") ->  "Szia ~s !";
greetings_format(_)    ->  "Hello ~s !".

Matching on Locale

% greetings in Erlang with pattern matching
% local format is "FR" or "FR-CA"

greetings(Name, Locale) ->
    GreetingFmt = greetings_format(Locale),
    io:format(GreetingFmt, [Name]).

greetings_format("EN") ->  "Hi ~s !";

% Combine some the different approaches
greetings_format("EN-" ++ Country) ->
    case Country of
        "IE" -> "Heya ~s!";
        "GB" -> "Jolly good day to you ~s!";
        _ -> greetings_format("EN")
    end;

% Use the guards as the second check
greetings_format("DE-" ++ Country) when Country =:= "DE" ->
    "Hallo, ~s!";
greetings_format("DE-" ++ Country) when Country =:= "AT" ->
    "Grüss gott ~s!";
greetings_format("DE" ++ _) ->
    "Hallo, ~s!";

% Or create a granular matching on the param
greetings_format("FR-CA") ->  "Bonjour ~s !";
greetings_format("FR" ++ _) ->  "Salut ~s !";

greetings_format("HU") ->  "Szia ~s !";
greetings_format(_) ->     "Hello ~s !".

Combine the options

What the if?

By Máté Marjai

What the if?

Erlang Ireland about using conditionals in Erlang

  • 699