Skip to content

Instantly share code, notes, and snippets.

@pre63
Created October 28, 2020 03:39
Show Gist options
  • Select an option

  • Save pre63/48a7c8c47aae7639fd34072f9ffe96c5 to your computer and use it in GitHub Desktop.

Select an option

Save pre63/48a7c8c47aae7639fd34072f9ffe96c5 to your computer and use it in GitHub Desktop.
const runStatsProgram = ({kms, writer}) => {
const Id = x => ({
map: f => Id(f(x)),
fold: f => (f(x)),
})
const merge = a => b => Object.assign({},a,b)
const round = x => Math.round(x * 100) / 100
Id(kms)
.map(x =>
x.reduce((accumulator, item) => ({
time: accumulator.time + item,
ks: accumulator.ks + 1,
}), { ks:0, time: 0 }))
.map(stats =>
Id(stats)
.map(merge)
.fold(m =>
Id(stats.time / stats.ks)
.map(round)
.map(average => ({ average }))
.fold(m)))
.map(stats => [
`Best time for ${stats.ks}k = ${stats.time}.`,
`Time per minute ${stats.ks}k = ${stats.average}`
])
.map(messages =>
messages.map(writer))
}
runStatsProgram({
kms: [4.20,4.09,4.16,4.30,4.20],
writer: x => console.log(x)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment