Last active
December 26, 2023 22:36
-
-
Save brito/c52557ac000934ea85898e764d56934c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
┬┌┐┌┌┬┐┌─┐┬─┐┌─┐┌─┐┌┬┐┬┌┐┌┌─┐ | |
││││ │ ├┤ ├┬┘├┤ └─┐ │ │││││ ┬ | |
┴┘└┘ ┴ └─┘┴└─└─┘└─┘ ┴ ┴┘└┘└─┘*/ | |
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