Skip to content

Instantly share code, notes, and snippets.

@pimbrouwers
Last active January 24, 2019 13:23
Show Gist options
  • Select an option

  • Save pimbrouwers/f07cc81654280021cd71364e5eea7924 to your computer and use it in GitHub Desktop.

Select an option

Save pimbrouwers/f07cc81654280021cd71364e5eea7924 to your computer and use it in GitHub Desktop.
Awesome F# - Running Total
type Split = double
type Lap = { Num : int; Split : Split }
type RunnerLap = { Lap : Lap; TotalTime : double }
let lap1 = { Num = 1; Split = 1.23 }
let lap2 = { Num = 2; Split = 1.13 }
let lap3 = { Num = 3; Split = 1.03 }
let laps = [lap1;lap2;lap3]
let runnerLapsAccumulator =
Seq.scan
(fun rl l -> { rl with Lap = l; TotalTime = rl.TotalTime + l.Split })
{ Lap = { Num = 0; Split = 0.0 }; TotalTime = 0.0 }
let runnerLaps = laps |> runnerLapsAccumulator
printfn "%A" runnerLaps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment