Published
Edited
Jan 22, 2021
1 fork
3 stars
Insert cell
Insert cell
Insert cell
cities = FileAttachment("cities.json").json()
Insert cell
Insert cell
properties = ["cost", "internetSpeed"]
Insert cell
Insert cell
Insert cell
propArray = curry(
(prop, list) =>
({
[prop]: map(k => k[prop], list)
})
)
Insert cell
Insert cell
costs = propArray("cost")
Insert cell
Insert cell
cityCostsObject = costs(cities)
Insert cell
Insert cell
cityCosts = cityCostsObject.cost
Insert cell
Insert cell
cityCostAverage = sum(cityCosts) / length(cityCosts)
Insert cell
Insert cell
transpose = curry((properties, list) => reduce(
(collector, item) =>
({
...collector,
...propArray(item)(list)
}),
{}
)(properties))
Insert cell
Insert cell
extractor = transpose(properties)
Insert cell
Insert cell
extractor(cities)
Insert cell
Insert cell
Insert cell
transposePusher = ((collector, item) => {
const { cost = [], internetSpeed = [] } = collector // initialize the collector with two empty arrays
return merge(
collector,
{
cost: append(item.cost, cost),
internetSpeed: append(item.internetSpeed, internetSpeed)
}
)
})
Insert cell
groupedByProp = reduce(transposePusher, {}, cities)
Insert cell
Insert cell
Insert cell
times = 1e5 // NOTA BENE: setting times to more than 1e5 may stall your browser tab/window; take care!
Insert cell
Insert cell
Insert cell
{
if (false) { // set to true if you want to do a timing test (times appear in the console)
console.time()
for (let i = 0; i < times; i++) transpose(properties, cities)
console.timeEnd()
return transpose(properties, cities)
}
}
Insert cell
Insert cell
{
if (false) { // set to true if you want to do a timing test (times appear in the console)
console.time()
for (let i = 0; i < 1e4; i++) reduce(transposePusher, {}, cities)
console.timeEnd()
return reduce(transposePusher, {}, cities)
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more