typestar

Composing stages in Erlang

No pipe operator: nest the calls or name the stages.

%% No pipe operator: compose by nesting or naming the stages.
main(_) ->
    Raw = [" 12 ", "3", " 21", "8 "],

    %% Nested style reads inside out.
    Total = lists:sum([list_to_integer(string:trim(S)) || S <- Raw]),
    io:format("Total: ~p~n", [Total]),

    %% Named stages read top to bottom.
    Trimmed = [string:trim(S) || S <- Raw],
    Numbers = [list_to_integer(S) || S <- Trimmed],
    Sorted = lists:sort(fun(A, B) -> A > B end, Numbers),
    io:format("Ranked: ~p~n", [Sorted]).

How it works

  1. The nested style reads inside out, compactly.
  2. Named stages read top to bottom like a recipe.
  3. lists:sort/2 takes the ordering fun directly.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
458
Tokens
138
Three-star pace
60 tpm

At the three-star pace of 60 tokens a minute, this run takes about 138 seconds.

Type this snippet

Step 3 of 3 in Recursion & comprehensions, step 10 of 31 in Language basics.

← Previous Next →