Published
Edited
Oct 21, 2018
1 fork
Importers
32 stars
Insert cell
Insert cell
group(data, d => d.name)
Insert cell
group([1, 1, 1, 2, 3, 4, 4, 2], d => d & 1)
Insert cell
group([1, 1, 1, 2, 3, 4, 4, 2])
Insert cell
group = {
const reduce = (p, v) => (p.push(v), p);
const init = () => [];
return function group(values, keyof) {
return groupReduce(values, keyof, reduce, init);
};
}
Insert cell
Insert cell
groupMap(data, d => d.name, d => d.value)
Insert cell
groupMap = {
const reduce = valueof => (p, ...args) => (p.push(valueof(...args)), p);
const reduceIdentity = (p, v) => (p.push(v), p);
const init = () => [];
return function groupMap(values, keyof, valueof) {
return groupReduce(
values,
keyof,
valueof === undefined ? reduceIdentity : reduce(valueof),
init
);
};
}
Insert cell
Insert cell
groupReduce(
data,
d => d.name, // key
(p, v) => p + v.value, // reduce
() => 0 // initialize
)
Insert cell
function groupReduce(values, keyof = identity, reduce, init = noop) {
const map = new Map();
let index = -1;
for (const value of values) {
const key = keyof(value, ++index, values);
map.set(key, reduce(map.has(key) ? map.get(key) : init(key), value, index, values));
}
return map;
}
Insert cell
Insert cell
groupSum(data, d => d.name, d => d.value)
Insert cell
groupSum = {
const reduce = valueof => (p, ...args) => p + valueof(...args);
const reduceIdentity = (p, v) => p + v;
const init = () => 0;
return function groupSum(values, keyof, valueof) {
return groupReduce(
values,
keyof,
valueof === undefined ? reduceIdentity : reduce(valueof),
init
);
};
}
Insert cell
groupMin(data, d => d.name, d => d.value)
Insert cell
groupMin = {
const reduce = valueof => (p, ...args) => Math.min(p, valueof(...args));
const reduceIdentity = (p, v) => Math.min(p, v);
const init = () => Infinity;
return function groupMin(values, keyof, valueof) {
return groupReduce(
values,
keyof,
valueof === undefined ? reduceIdentity : reduce(valueof),
init
);
};
}
Insert cell
groupMax(data, d => d.name, d => d.value)
Insert cell
groupMax = {
const reduce = valueof => (p, ...args) => Math.max(p, valueof(...args));
const reduceIdentity = (p, v) => Math.max(p, v);
const init = () => -Infinity;
return function groupMax(values, keyof, valueof) {
return groupReduce(
values,
keyof,
valueof === undefined ? reduceIdentity : reduce(valueof),
init
);
};
}
Insert cell
groupCount(data, d => d.date.toISOString())
Insert cell
groupCount = {
const reduce = p => p + 1;
const init = () => 0;
return function groupCount(values, keyof) {
return groupReduce(values, keyof, reduce, init);
};
}
Insert cell
groupSort(data, d => d.name, (a, b) => b.value - a.value)
Insert cell
function groupSort(values, keyof, compare = ascending) {
return new Map(Array.from(group(values, keyof), ([key, values]) => [key, values.sort(compare)]));
}
Insert cell
groupSelect(data, d => d.name, 2, (a, b) => b.value - a.value)
Insert cell
function groupSelect(values, keyof, k = 1, compare = ascending) {
return new Map(Array.from(group(values, keyof), ([key, values]) => {
quickselect(values, k, undefined, undefined, compare);
if (values.length > k) values.length = k;
return [key, values];
}));
}
Insert cell
Insert cell
function identity(x) { return x; }
Insert cell
function noop() {}
Insert cell
function ascending(a, b) { return a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN; }
Insert cell
data = (await d3.json("https://raw.githubusercontent.com/vega/vega-lite/b2338345973f4717979ad9140c06ee0970c20116/data/unemployment-across-industries.json")).map(({series, count, date}) => ({name: series, value: count, date: new Date(date)}))
Insert cell
quickselect = require("quickselect@2")
Insert cell
d3 = require("d3@5")
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more