typestar

temp_stats.fsx en F#

Una semana de temperaturas: minBy/maxBy, barras y saltos con pairwise.

// Summarize a week of temperature readings: extremes, mean, swings.

let readings =
    [ "Mon", -4.0; "Tue", -1.5; "Wed", 2.0; "Thu", 3.5
      "Fri", 1.0; "Sat", -2.5; "Sun", -6.0 ]

let temps = readings |> List.map snd
let coldest = readings |> List.minBy snd
let warmest = readings |> List.maxBy snd

printfn "Coldest: %s at %.1f C" (fst coldest) (snd coldest)
printfn "Warmest: %s at %.1f C" (fst warmest) (snd warmest)
printfn "Mean: %.1f C" (List.average temps)

// Chart each day against the freezing line.
for day, t in readings do
    let length = max 1 (int (t + 7.0))
    let side = if t > 0.0 then "above" else "below"
    printfn "%-4s %5.1f %s %s" day t (String.replicate length "#") side

// pairwise gives the overnight neighbors in one call.
let sharpest =
    temps |> List.pairwise
    |> List.map (fun (a, b) -> abs (b - a))
    |> List.max
printfn "Sharpest overnight swing: %.1f C" sharpest

Cómo funciona

  1. List.minBy snd y maxBy snd hallan los días récord.
  2. El bucle del gráfico desestructura cada par e imprime columnas.
  3. List.pairwise entrega los vecinos nocturnos.

Palabras clave y builtins usados aquí

El intento, en números

Líneas
26
Caracteres a escribir
880
Tokens
224
Ritmo de tres estrellas
60 tpm

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

Escribe este fragmento

Paso 3 de 3 en Bis; paso 27 de 27 en Fundamentos del lenguaje.

← Anterior