typestar

El módulo lists en Erlang

map, filter, foldl, sort: el kit diario.

%% The lists module: map, filter, foldl, sort -- the daily kit.
main(_) ->
    Durations = [12, 3, 21, 8, 15],

    Billed = lists:map(fun(M) -> M * 4 end, Durations),
    io:format("Pence per call: ~p~n", [Billed]),

    Long = lists:filter(fun(M) -> M > 10 end, Durations),
    io:format("Long calls: ~p~n", [Long]),

    Total = lists:foldl(fun(M, Acc) -> Acc + M end, 0, Durations),
    io:format("Minutes total: ~p~n", [Total]),

    io:format("Sorted: ~p~n", [lists:sort(Durations)]),
    io:format("Longest: ~p~n", [lists:max(Durations)]).

Cómo funciona

  1. lists:map y filter toman funs escritos en línea.
  2. foldl enhebra un acumulador de izquierda a derecha.
  3. sort, max y sum responden las preguntas comunes.

Palabras clave y builtins usados aquí

El intento, en números

Líneas
15
Caracteres a escribir
510
Tokens
170
Ritmo de tres estrellas
60 tpm

Al ritmo de tres estrellas de 60 tokens por minuto, este intento toma unos 170 segundos.

Escribe este fragmento

Paso 1 de 4 en El módulo lists; paso 11 de 31 en Fundamentos del lenguaje.

← Anterior Siguiente →