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

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