Public
Edited
Jan 9, 2024
Insert cell
Insert cell
Insert cell
n = 64
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
nseq = aq
.table({ index: [0]})
.derive({index: op.sequence(1,16,1),
rank: op.sequence(1,8,1),
pos: op.sequence(1,4,1)})
.unroll('index')
.unroll('rank')
.unroll('pos')
.objects()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
mutable element = []
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function iterate(obj, out = []) {

// recurse over keys
Object.keys(obj).forEach((key) => {
out.push(key);
if (check(obj[key], {}, []) && obj[key] !== null) {
iterate(obj[key], out) }});
return out }
Insert cell
check = (prop, ...types) =>
types.some((type) =>
Object.prototype.toString.call(prop) ===
Object.prototype.toString.call(type));
Insert cell
Insert cell
Insert cell
targetTree =
reduceTree( (pool, value, path) =>
[ ...pool, { p:path.join('.'), value } ], [] , sourceTree )
Insert cell
// https://stackoverflow.com/questions/52807498/
// nested-reduce-functions-recursion-functional-programming-tree-traversal/52822287#52822287

function* traverse (tree = {}, path = []) {
for (const [ key, value ] of Object.entries (tree))
if (check(value, {},[])) {
yield* traverse (value, append (path, key))}
else
yield [ append (path, key), value ]
}
Insert cell
reduceTree = (proc, state, tree) =>
reduce
( (acc, [ path, value ]) =>
proc (acc, value, path)
, state
, Array.from (traverse (tree))
)
Insert cell
reduce = (proc, state, arr) =>
arr .reduce (proc, state)
Insert cell
append = (xs, x) =>
xs .concat ([ x ])
Insert cell
pathGen(sourceTree)
Insert cell
function pathGen(obj) {
let acc = {};
for (let [path, value] of pathGen(obj)) {
//acc[value] = acc[value] || [];
acc[path] = value; //path.split('.').reverse()[0] ;
}

function* pathGen(obj) {
for (let k in obj) {
if (check(obj[k], {}, [])) {
for (let [childPath, value] of pathGen(obj[k])) {
yield [`${k}.${childPath}`, value];
}
} else {
yield [k];
}
}
}
return acc;
}
Insert cell
// https://stackoverflow.com/questions/60352470
// get-all-paths-to-a-specific-key-in-a-deeply-nested-object

function getAllPaths(obj, prev = '') {
let result = []
for (let k in obj) {
let path = prev + (prev ? '.' : '') + k;

result.push([path,k])
if (check(obj[k],{},[])) {
result.push(getAllPaths(obj[k], path))
}
} return result
}
Insert cell
Insert cell
integerTee = {
let tree = {};
index(1,tree)
index(21,tree)
index(301,tree)
index(401,tree)
index(2401,tree)
return tree
}
Insert cell
function index(uint, tree) {
let size = fastLen(uint);
let last;

for (let i = 0; i < size; ++i) {
let digit = ((uint * 10 ** -i) | 0) % 10;
if (last) {
last = last[digit] ? last[digit] : last[digit] = {};
} else if (tree[digit]) {
last = tree[digit];
} else {
last = tree[digit] = {};
}
}
last.freq ? last.freq++ : (last.freq = 1);
}

Insert cell
fastLen = (input) =>
(input < 10) ? 1 :
(input < 100) ? 2 :
(input < 1000) ? 3 :
(input < 10000) ? 4 :
(input < 100000) ? 5 :
(input < 1000000) ? 6 :
(input < 10000000) ? 7 :
(input < 100000000) ? 8 :
(input < 1000000000) ? 9 :
(input < 10000000000) ? 10 :
(input < 100000000000) ? 11 :
(input < 1000000000000) ? 12 :
(input < 10000000000000) ? 13 :
(input < 100000000000000) ? 14 :
(input < 1000000000000000) ? 15 : 16
Insert cell
Insert cell
Insert cell
a = html`<input type = range>`
Insert cell
b = html`<input type = range>`
Insert cell
resolve()
Insert cell
async function* resolve(obj) {
let a_ = Generators.input(a)
let b_ = Generators.input(b)
let sub = {sub:{a_,b_}}
let out = {}
for await (let [key,val] of Object.entries(sub) ) {
if(key == 'sub'){
out[key] = {}; resolve(val)
} else { out[key] = 0; }

yield out
}

}
Insert cell
Promise.allDeep = function (entity) {
let keys = Object.keys(entity)
return Promise.all(keys.map(key=>{
let value = entity[key]
if(check(value,{})&&!value.then){
return Promise.allDeep(value)
} return value
}))
.then(data => {
return keys.reduce((pool, key, i) => { pool[key] = data[i]
return pool },
Array.isArray(entity) ? [] : {})}
)
}
Insert cell
Insert cell
// https://stackoverflow.com/a/16152333
{
let out = [];
let tw = document.createTreeWalker(frag, NodeFilter.SHOW_COMMENT, null, null),
comment;
while ((comment = tw.nextNode())) {
out.push(comment);
}
return out;
}
Insert cell
frag = html`<div>Hello<!-- world --></div>`
Insert cell
Insert cell
// { for(var i in window) { return window[i] } }
Insert cell
Insert cell
Insert cell
Insert cell
import {aq, op} from "@uwdata/arquero"
Insert cell
import {share} from '@tomlarkworthy/shareview@308'
Insert cell
Insert cell
Insert cell
viewof options = {
const n = Inputs.range([1, 30], {value: 19, step: 1, label: "size"});
const a = Inputs.range([0, 45], {value: 27.5, step: 0.1, label: "angle"});
const x = Inputs.range([-100, 100], {value: 0, step: 1, label: "x offset"});
const y = Inputs.range([-100, 100], {value: 0, step: 1, label: "y offset"});
return Object.defineProperty(html`${[n, a, x, y]}`, "value", {
get() {
return {
n: n.value,
a: a.value,
p: [x.value, y.value]
};
},
set({n: _n, a: _a, p: _p}) {
if (_n !== undefined) n.value = _n;
if (_a !== undefined) a.value = _a;
if (_p !== undefined) [x.value, y.value] = _p;
}
});
}
Insert cell
// viewof options.value = ({x: 9})
Insert cell
share('nested', viewof options)
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