Skip to content

Instantly share code, notes, and snippets.


for-of, early termination, iterators, and generators

If we terminate a loop early, for-of invokes method .return() of the iterator. Generators return generator objects (that implement both the Iterable interface and the Iterator interface). If we invoke .return() on such an object, the generator is terminated. We can prevent that by overriding .return().

More information:


function logIter(iter) {
View math-problem.js
// Problem: ABCDE × A = EEEEEE. Each letter is a different digit.
// Source:
// Equation to solve: EEEEEE / A = ABCDE
// You have to try 9 digits E and 8 digits A per E (with A neither zero nor E)
for (let E=1; E <= 9; E++) {
for (let A=1; A <= 9; A++) {
if (A === E) continue;
View twitter-img-alt.js
// Bookmarklet: display the alt text of images on Twitter
// Instructions: copy the whole text block, starting at `javascript:` and make it the address of a bookmark
// You can test it here:
let img;
const match = /\/photo\/([0-9]+)$/.exec(String(location));
if (match) {
const photoNumber = match[1];
View partition.js
function partition(inputArray, callback) {
const result = {};
for (const [indexOfValue, value] of inputArray.entries()) {
const propertyKey = callback(value, indexOfValue);
if (propertyKey === null || propertyKey === '') {
if (!{}, propertyKey)) {
result[propertyKey] = [];
View weighted-choices.js
/** Get a random integer 0 <= i < max */
function getRandomInteger(max) {
return Math.floor(Math.random() * max);
function getWeightedChoice(choices, weights) {
const sumOfWeights = weights.reduce((acc, x) => acc + x, 0);
let randomInt = getRandomInteger(sumOfWeights) + 1;
for (const [index, weight] of weights.entries()) {
randomInt = randomInt - weight;
if (randomInt <= 0) {
View dual-mode-csp.js
* Dual-mode (sync and async) code via channels (think CSP).
* Check out the example at the end.
* - In async mode, every `yield` is an `await`.
* - In sync mode, `yield` simply passes on the operand.
* More on CSP:
View dual-mode.js
Proof of concept: Writing dual-mode (sync and async) code via generators
Recommendation: start by reading the example (at the end).
The API object is called `def`.
Dual-mode `await`: const unwrapped = yield wrapped;
Dual-mode `yield`: yield def.$one(singleValue)
Dual-mode `yield*`: yield def.$all(iterable)

Better iPad window management

Key idea: All window management is done in App Switcher.

  • Start with one space: everything works like it does on iOS
  • All window management is done in App Switcher:
    • Creating new spaces
    • Splitting windows – via drag and drop:
      • Drag one window into another one to split.
      • Drag a half-window out to unsplit.
View pandoc-indexer.lua
-- Cross-format support for indices (LaTeX, HTML, EPUB, ...)
-- Syntax is based on LaTeX:
-- Creating entries: \index{key}
-- Creating the index: \printindex
-- ########## Shared data ##########
local indexEntryCounter = 0
local indexEntries = {}

Functional decorators

Disclaimer: random thoughts of mine; not sure if they are useful.

Motivating example

class StringBuilder {
  #data = '';
  proto add = logged @ function (str) {
You can’t perform that action at this time.