Published
Edited
Dec 3, 2019
Insert cell
Insert cell
a = new Array(20).fill(0)
Insert cell
b = {
var array = [];
for (var i=0; i<20; i++) {
array.push(i);
}
return array;
}
Insert cell
Insert cell
long = charseq((x) => x * x + 5, 1024)
Insert cell
Insert cell
// Given a function `f` that maps from integers to integers, and a number
// `n` of characters to generate, produces a character sequence.

function charseq(f, n) {
return range(n).map(f).map(toChar).join("")
}
Insert cell
// For example, using the identity function x => x, we get the
// alphabet.
alphabet = {
function identity(x) {
return x;
}
return charseq(identity, 26)
}

Insert cell
// Given an integer `x`, return an array consisting of the range [0, 1, ... x-1]
function range(x) {
var arr = [];
for (var i=0; i<x; i++) {
arr.push(i);
}
return arr;
}
Insert cell
range(10)
Insert cell
// Convert an integer to a letter in the range [a-z].
function toChar(x) {
return String.fromCharCode(x % 26 + 97)
}
Insert cell
toChar(0)
Insert cell
toChar(25)
Insert cell
toChar(26)
Insert cell
// Reformat a long string into groups of 4 characters at a time, set on lines of 16 groups
function linebreaks(s) {
// Inefficient, but who cares
var x = ""
for (var i=0; i<s.length; i++) {
if (i % 64 === 0) x += "\n"
else if (i % 4 === 0) x += " "
x += s[i]
}
return x.slice(1)
}
Insert cell
linebreaks(charseq((x) => x, 1024))
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