typestar

temp_stats.fsx in F#

A week of temperatures: minBy/maxBy, a bar chart, and pairwise swings.

// 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

How it works

  1. List.minBy snd and maxBy snd find the record days.
  2. The chart loop destructures each pair and prints columns.
  3. List.pairwise hands over the overnight neighbors.

Keywords and builtins used here

The run, in numbers

Lines
26
Characters to type
880
Tokens
224
Three-star pace
60 tpm

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

Type this snippet

Step 3 of 3 in Encore, step 27 of 27 in Language basics.

← Previous