Skip to content

Instantly share code, notes, and snippets.

@brito
Last active December 26, 2023 22:36
Show Gist options
  • Save brito/c52557ac000934ea85898e764d56934c to your computer and use it in GitHub Desktop.
Save brito/c52557ac000934ea85898e764d56934c to your computer and use it in GitHub Desktop.
function __SMOKE_TEST__Catalog(){ console.log(
Catalog([{a:1, b:2}, {b:4, c:7, d:0}, {a:0, b:2}])
)}
/*
┌─┐┌─┐┌┬┐┌─┐┬ ┌─┐┌─┐
│ ├─┤ │ ├─┤│ │ ││ ┬
└─┘┴ ┴ ┴ ┴ ┴┴─┘└─┘└─┘ 18w40*/
function Catalog(items){
return ([].concat(arguments)).reduce(catalog, {});
function catalog(dict, item){
for (var key in item){
var value = item[key],
initial = key in dict? dict[key]: {};
if (value)
switch (value.constructor.name){
case 'Array':
dict[key] = value.reduce(catalog, initial)
break;
case 'Object':
dict[key] = [value].reduce(catalog, initial)
break;
default:
dict[key] = archive(initial, value, item)
break;
}
else
dict[key] = archive(initial, value, item);
}
return dict;
function archive(context, key, item){
context[key] = key in context ?
context[key].concat(item)
: [item];
return context;
}
}
}
// for example, on a JSON body
var catalog = ( el =>
Catalog(...JSON.parse(el.innerText).entities)
)(document.body),
interesting = Interesting(catalog),
distinct = Object.entries(interesting)
.sort((p => (a,b) => a[1][p] - b[1][p])('distinct')),
distinguished = Catalog(...distinct.map(([k,v]) => v))
/*
┬┌┐┌┌┬┐┌─┐┬─┐┌─┐┌─┐┌┬┐┬┌┐┌┌─┐
││││ │ ├┤ ├┬┘├┤ └─┐ │ │││││ ┬
┴┘└┘ ┴ └─┘┴└─└─┘└─┘ ┴ ┴┘└┘└─┘*/
function Interesting(item){
return Object.entries(item).reduce(interesting, {});
function interesting(observations, [column, values]){
observations[column] =
Object.entries(values)
.reduce(counts, [{}])
.map(stats)[0];
return observations;
function counts([totals], [value, referent]){
totals['counts'] = totals['counts'] || {};
totals['counts'][value] = referent.length || +referent;
return [totals];
}
function stats(item){
let values = Object.values(item['counts']);
item['distinct'] = values.length;
values.reduce((a,b) => item['min'] = Math.min(a,b));
values.reduce((a,b) => item['max'] = Math.max(a,b));
item['avg'] = avg(values);
item['stddev'] =
Math.sqrt(item['var'] =
avg(values.map(v => Math.pow(v - item['avg'],2))));
values.sort((a, b) => a - b);
item['median'] =
(values[Math.floor((values.length-1)/2)] +
values[Math.ceil((values.length-1)/2)])/2;
// item['area'] = item['avg'] * item['distinct'];
// item['volume'] = item['area'] * item['var'];
return item
function avg(values){
return values.reduce((a,b) => a + b) / values.length }
}
}
}
//Object.entries(interesting).map((p => ([n,v]) => v[p])('volume'))
// .sort((a,b) => a - b)
//Object.entries(interesting)
// .sort((p => (a,b) => a[1][p] - b[1][p])('area'))
// .reduce((obj, [k, v]) => ({ ...obj, [k]: v }), {})
/*
┌─┐┌─┐┌┬┐┌─┐┬ ┌─┐┌─┐
│ ├─┤ │ ├─┤│ │ ││ ┬
└─┘┴ ┴ ┴ ┴ ┴┴─┘└─┘└─┘ 18w8*/
function Catalog(...items){
return items.reduce(catalog, {});
function catalog(dict, item){
for (let [key, value] of Object.entries(item)){
let initial = key in dict? dict[key]: {};
if (value)
switch (value.constructor.name){
case 'Array':
dict[key] = value.reduce(catalog, initial)
break;
case 'Object':
dict[key] = [value].reduce(catalog, initial)
break;
default:
dict[key] = archive(initial, value, item)
break;
}
else
dict[key] = archive(initial, value, item);
}
return dict;
function archive(context, key, item){
context[key] = key in context ?
context[key].concat(item)
: [item];
return context;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment