Public
Edited
Jun 16, 2024
Paused
Comments locked
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
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
buffer = {
try {
const stream = await data.file(label).stream();
const reader = stream.getReader();
let done, value;
while ((({ done, value } = await reader.read()), !done)) {
yield value;
}
} catch (error) {
return [];
}
}
Insert cell
Insert cell
Insert cell
Insert cell
async function* pipe(
container /* node */,
trickle /* iterable */,
{ amount = 1000, chunk = 5, start = -1 } = {}
) {
let i = start;
container.textContent = "";
for (const n of await trickle) {
container.textContent +=
i < 0
? n + (i === -1 ? ".\n" : "")
: (i && i % chunk === 0 ? " " : "") + n;
yield container.textContent.replace(/\s/g, "").split("");
if (++i === amount) break;
}
}
Insert cell
/*!
* GOTCHA: Avoid defining the spigot functions using shorthand syntax
*/
spigot = ({
/* {{{ Pi: */
pi: function* (n = NaN) {
for (
let p, q, k = 2n, a = 4n, b = 1n, a_ = 12n, b_ = 4n;
isNaN(+n) || n > -1;
k++
) {
[p, q] = [k * k, 2n * k + 1n];
[a, b, a_, b_] = [a_, b_, p * a + q * a_, p * b + q * b_];
for (let d; (d = a / b) === a_ / b_ && (isNaN(+n) || n > -1); n--) {
yield Number(d);
[a, a_] = [10n * (a % b), 10n * (a_ % b_)];
}
/* thank you, Matz! - mg */
}
},
pi_: function* (n = NaN) {
SWEEP: for (
let i = 2n, q = 1n, r = 180n, t = 60n, u, y;
isNaN(+n) || n > -1;
i++, n--
) {
y = (q * (27n * i - 12n) + 5n * r) / (5n * t);
u = 3n * (3n * i + 1n) * (3n * i + 2n);
r = 10n * u * (q * (5n * i - 2n) + r - y * t);
q = 10n * q * i * (2n * i - 1n);
t *= u;
yield Number(y);
}
},
/* }}} */
/* {{{ Napier's constant: */
e: function (digits = 1000) {
const e = [];
SWEEP: for (
let bump = 9,
A = Array.from(Array((digits += bump)), (_, i) =>
!i ? 0 : !(1 - i) ? 2 : 1
),
x = 0;
digits > bump;

) {
for (let n = digits--; --n; ) {
A[n] = x % n;
x = 10 * A[n - 1] + Math.trunc(x / n);
}
e.push(x);
/* thank you, Xavier Gourdon - mg */
}
return (e.shift() + "").split("").map(Number).concat(e);
},
e_: function (digits = 1000) {
const e = [2];
SWEEP: for (
let i = 0,
A = Array(digits + 3).fill(1),
B = Array.from(A, (_, i) => i + 2),
j,
q;
i < digits;
i++
) {
A = Array.from(A, (x) => x * 10);
for (j = A.length - 1; j > -1; --j > -1 && (A[j] += q)) {
q = Math.trunc(A[j] / B[j]);
A[j] = A[j] % B[j];
}
e.push(q);
}
return e;
}
/* }}} */
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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