Public
Edited
Nov 19, 2023
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("https://esm.sh/@thi.ng/transducers");
Insert cell
R = import("https://esm.sh/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
emojis = `1. 🌟 - Star (achievement, quality)
2. 📅 - Calendar (schedule, date)
3. 💌 - Heart with envelope (communication, love)
4. ⏳ - Hourglass (time, patience)
5. 🔒 - Lock (security, privacy)
6. 📚 - Books (knowledge, learning)
7. 🍀 - Clover (luck, nature)
8. 💡 - Lightbulb (idea, inspiration)
9. 🚀 - Rocket (innovation, adventure)
10. 🔄 - Arrows (cycle, refresh)
11. 🎯 - Dartboard (target, goal)
12. ⚙️ - Gear (settings, industry)
13. 🧭 - Compass (navigation, direction)
14. 🛒 - Shopping cart (purchase, commerce)
15. 🌍 - Globe (world, international)
16. 🏠 - House (home, stability)
17. 📈 - Chart increasing (growth, success)
18. 🕊️ - Dove (peace, freedom)
19. 🏥 - Hospital (health, care)
20. ⚖️ - Balance scale (justice, balance)`
Insert cell
emojiEmbs = embed(emojis.split("\n").map(x => x.split(".")[1].trim()));
// DOM.download(new Blob([JSON.stringify(emojiEmbs)], {type: "text/json"}), "emojiembs.json")// DOM.download(new Blob([JSON.stringify(emojiEmbs)], {type: "text/json"}), "emojiembs.json")
Insert cell
// emojiEmbs = FileAttachment("emojiembs.json").json()
Insert cell
items = `bleh`
.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>;

bxs = Array.from(bxs);
let prevBxs = bxs.slice();

for (let i = 0; i < Math.round(axs.length / prevBxs.length); i++) {
bxs = bxs.concat(prevBxs);
}
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