Published
Edited
Jan 16, 2021
Importers
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
selection
Insert cell
Insert cell
Insert cell
function map(any, fun) {
if (any instanceof Array)
return any.map(fun)
if (any instanceof Object) {
const result = {}
for (let key in any)
result[key] = fun(any[key])
return result;
}

return fun(any)
}
Insert cell
Insert cell
function push(buffer = [], items = [], limit = 1) {
buffer.push(...items)
while (buffer.length > limit)
buffer.shift()
return buffer;
}
Insert cell
Insert cell
function slide(buffer = [], limit = 1) {
return (...items) => push(buffer, items, limit)
}
Insert cell
Insert cell
async function* tail(agen, opts) {
let {
n,
i = 0,
k = 60,
buf = [],
cap = Infinity,
fun = async v => v
} = opts || {};
for await (let item of agen) {
if (buf.push(await fun(item)) >= cap)
buf = buf.slice(-cap)
if (i++ >= n && n != null)
break;
else if (i % k == 0)
yield buf;
}
yield buf;
}
Insert cell
Insert cell
async function* tails(agen, opts) {
let {
n,
i = 0,
k = 1,
bufs = {},
cap = Infinity,
fun = async v => v
} = opts || {};
for await (let obj of agen) {
for (let key in obj) {
let buf = bufs[key] || (bufs[key] = [])
if (buf.push(...await fun(obj[key])) >= cap)
buf = bufs[key] = buf.slice(-cap)
if (i++ >= n && n != null)
break;
else if (i % k == 0)
yield bufs;
}
}
yield bufs;
}
Insert cell
Insert cell
function* windows(array, T = 1, W = T) {
// every T, window of size W
for (let i = 0; i < array.length; i += T) {
yield array.slice(i, i + W)
}
}
Insert cell
Insert cell
Insert cell
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