typestar

El módulo List en F#

map, filter, sortBy, sumBy: el kit de todos los días.

// The List module is the daily toolkit: map, filter, sortBy, sum.
let harvestsKg = [("Willow", 14.2); ("Bramble", 8.9); ("Clover", 18.4)]

harvestsKg
|> List.sortByDescending snd
|> List.iter (fun (hive, kg) -> printfn "%-8s %5.1f kg" hive kg)

let totalKg = harvestsKg |> List.sumBy snd
printfn "Season total: %.1f kg" totalKg

let goodYears =
    harvestsKg |> List.filter (fun (_, kg) -> kg > 10.0)
               |> List.map fst
printfn "Above ten kilos: %A" goodYears

Cómo funciona

  1. sortByDescending snd ordena pares por su segunda casilla.
  2. sumBy snd pliega un campo de cada tupla.
  3. filter y luego map extraen los nombres de los años buenos.

Palabras clave y builtins usados aquí

El intento, en números

Líneas
14
Caracteres a escribir
454
Tokens
111
Ritmo de tres estrellas
65 tpm

Al ritmo de tres estrellas de 65 tokens por minuto, este intento toma unos 102 segundos.

Escribe este fragmento

Paso 2 de 3 en Listas; paso 11 de 27 en Fundamentos del lenguaje.

← Anterior Siguiente →