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
- The nested style reads inside out, compactly.
- Named stages read top to bottom like a recipe.
lists:sort/2takes the ordering fun directly.
Keywords and builtins used here
endformatfunlist_to_integermainsortsumtrim
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.
Step 3 of 3 in Recursion & comprehensions, step 10 of 31 in Language basics.