Public
Edited
Oct 24, 2023
1 fork
Insert cell
Insert cell
Insert cell
{
const groupUntil = (isTrueFn) => [
// init
() => [[]],
// completion (nothing to do in this case)
(acc) => acc,
// step
(acc, x) => (acc.push(x), acc)
];
}
Insert cell
class B {
constructor() {
this.name = "hello";
}
meth() {
return new this.constructor;
}
}
Insert cell
B2 = {
function B2() {
this.name = "hello";
};
B2.prototype.meth = function () {
return new this.constructor;
};

return B2;
}
Insert cell
(new B2()).meth()
Insert cell
(new B()).meth()
Insert cell
t = import("@thi.ng/transducers");
Insert cell
R = import("ramda");
Insert cell
{
let xform = t.comp(
t.filter((x) => (x & 1) > 0), // odd numbers only
t.distinct(), // distinct numbers only
t.map((x) => x * 3) // times 3
);

// collect into array (push)
return t.transduce(xform, t.push(), [1, 2, 3, 4, 5, 4, 3, 2, 1])
}
Insert cell
1 - 1 || "hi"
Insert cell
group = {
return n => a => t.transduce
(t.comp
(t.slidingWindow(n, true),
t.map(xs => xs.join(" "))
), t.push(), a)
}
Insert cell
pool = (xxs) => xxs[0].map((_, i) => xxs.map(a => a[i]).reduce((a, b) => a + b) / xxs.length)
Insert cell
pool([[0, 5, 10], [5, 10, 0], [10, 0, 5]]) //[5, 5, 5]
Insert cell
// f32[], f32[] -> f32
cos = (e1, e2) => {
let total = 0;
for (let i = 0; i < e1.length; i++) {
total += e1[i] * e2[i];
}
return total;
}
Insert cell
Insert cell
// [[0, 0.5], [0.5, 0]] , [2, 1]
ratio = (xs, rxs) => {
let total = rxs.reduce((a, b) => a + b, 0);
let arr = xs[0].map((_, i) => xs.map((x, j) => (rxs[j] / total) * x[i]).reduce((a, b) => a + b, 0) );
return arr;
}
Insert cell
r1 = ratio([[1, 2], [1, 1]], [3, 2])
Insert cell
pipe = {
let tf = await import('https://cdn.jsdelivr.net/npm/@xenova/transformers');
let pipe = await tf.pipeline('feature-extraction', 'Supabase/gte-small');
// string[] -> Iterator<{ dims: [1, 384], type: "float32", data: Float32Array, size: 384 }>
return (s) => pipe(s, { pooling: 'mean', normalize: true });
}
Insert cell
embed = async (xs, ys=xs) => {
return await pipe(xs).then(e => Array.from(e).map((e1, i) => ({embedding: e1.data, value: ys[i]})))
}
Insert cell
emojiEmbs = embed(emojis.map(e => `Emoji: ${e.Group} ${e.Subgroup} ${e.Name} ${e.Representation}`), emojis.map(e => `${e.Representation} (${e.Name})`));
// DOM.download(new Blob([JSON.stringify(emojiEmbs)], {type: "text/json"}), "emojiembs.json")
Insert cell
// emojiEmbs = FileAttachment("emojiembs.json").json()
Insert cell
items = `I've never seen a diamond in the flesh
I cut my teeth on wedding rings in the movies
And I'm not proud of my address
In a torn up town, no postcode envy
But every song's like
Gold teeth, Grey Goose, trippin' in the bathroom
Bloodstains, ball gowns, trashin' the hotel room
We don't care
We're driving Cadillacs in our dreams
But everybody's like
Cristal, Maybach, diamonds on your timepiece
Jet planes, islands, tigers on a gold leash
We don't care
We aren't caught up in your love affair
And we'll never be royals (royals)
It don't run in our blood
That kind of luxe just ain't for us
We crave a different kind of buzz
Let me be your ruler (ruler)
You can call me queen bee
And baby, I'll rule (I'll rule, I'll rule, I'll rule)
Let me live that fantasy`
.split("\n");
Insert cell
overallEmb = embed(items.join(" ")).then(x => x[0])
Insert cell
groupedEmbs = embed(items.map((_, i) => items.at(-1) + " " + (items.at(i + 1) || items[i])))
Insert cell
itemEmbs = embed(items)
Insert cell
combinedEmbs = itemEmbs.map((x, i) => ({...x, embedding: ratio([x.embedding, groupedEmbs[i].embedding, overallEmb.embedding], [0.5, 0.4, 1.0])}))
Insert cell
allocate = (axs /* Item A */, bxs /* Item B */) => {
// Item<T> = {value: T, embedding: Float32Array}
let result = new Map(); // Map<A, B>;
let isAvailableA = axs.map((_, i) => true);
let isAvailableB = bxs.map((_, i) => true);

let done = () => {
for (const b of isAvailableA) {
if (b === true) return false;
}
return true;
}
let scores = axs.map((ax) => bxs.map((bx) => cos(ax.embedding, bx.embedding)));

let isDone = false;
while(!isDone) {
let _i = -1;
let _j = -1;
let bestValue = -1;
for (let i = 0; i < scores.length; i++) {
if (!isAvailableA[i]) continue;
for (let j = 0; j < scores[i].length; j++) {
if (!isAvailableB[j]) continue;
if (scores[i][j] > bestValue) {
bestValue = scores[i][j];
_i = i;
_j = j;
}
}
}
if (_i === -1 || _j === -1 || bestValue === -1) throw new Error("did not have enough values on the left side to match to");
result.set(
axs[_i].value,
bxs[_j].value
);
isAvailableA[_i] = false;
isAvailableB[_j] = false;
isDone = done();
}
return axs.map(a => [a.value, result.get(a.value)]);
};
Insert cell
{
let r = allocate(itemEmbs, emojiEmbs).map(x => `<i>${x[0]}</i> <b>${x[1]}</b> <br>`).join("\n");
return html`${r}`;
}
Insert cell
{
let r = allocate(combinedEmbs, emojiEmbs).map(x => `<i>${x[0]}</i> <b>${x[1]}</b> <br>`).join("\n");
return html`${r}`;
}
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