Skip to content

Instantly share code, notes, and snippets.

@CrossEye
CrossEye / crawl.js
Last active January 27, 2023 15:13
Simple crawler
// https://stackoverflow.com/a/75251817
// crawl :: ((a -> Promise b), (b -> [a]), (b -> c), ((Map a c) -> d), Int?) -> [a] -> Promise d
const crawl = (search, extract, convert, collect, simul = 1) => async (inits) => {
const toCheck = new Set (inits)
const found = new Map ()
while (toCheck .size !== 0) {
const nextBlock = [...toCheck .values ()] .slice (0, simul)
const results = await Promise .all (nextBlock .map ((x) => search (x)))
results .forEach ((res, i) => {
@CrossEye
CrossEye / LoopRecur.js
Last active September 9, 2024 17:35
Simple Loop-Recur implementation
const Loop = (fn, ... init) => {
let r = fn (... init)
while (r instanceof Recur) r = fn (... r)
return r
}
// Fixed based on a SO discussion: https://stackoverflow.com/a/78940799
const Recur = function ( ...v) {
return Object .create (
Recur .prototype,
@CrossEye
CrossEye / _unfold.js
Last active June 24, 2022 20:42
unfold variant
//------------------------------------------------------------------------------
// A variant of my standard `unfold`, which allows us to use the entire list of
// previous values in calculating the next one.
const _unfold = (fn, init) =>
fn (init, (x) => _unfold (fn, [...init, x]), () => init)
// My usual version looks like this:
//
// Get all potential NYT Spelling Bee puzzles agains Peter Norvig's enable1 word list.
// I know the NYT's list is different, but I don't know if it's available.
// Results written to the console, so piping to `SpellingBeePuzzles.js` or some
// such would be appropriate. Sorted by descending possible total score.
const fetch = require ('node-fetch') // version 2 for common.js
const sum = (ns) =>
ns .reduce ((a, b) => a + b, 0)
@CrossEye
CrossEye / Real World Specification.md
Created January 4, 2022 22:41 — forked from ForbesLindesay/Real World Specification.md
Functional Programming from the perspective of a JavaScript Programmer.

Real World Specification

(aka "Algebraic JavaScript Specification")

This project specifies the behavior of a number of methods that may optionally be added to any object. The motivation behind this is to encourage greater code reuse. You can create functions that just rely on objects having implementations of the methods below, and in doing so you can make them work with a wide variety of different, but related data structures.

Definitions

For the purposes of this, spec, an "entity" is an object that has an [[equivalent]] operation (see bellow) and may implement some or all of the other methods.

@CrossEye
CrossEye / results.md
Last active January 3, 2022 01:14
Changes to the Electoral College

Changes to the Electoral College

This shows how various scenarios play out in terms of Electoral College vote and the states' percentages of those votes. These are based on current apportionment rules against the 2019 Census estimates, with various House sizes of (the current) 435, 1000, and 5000. Note here that Wyoming, which has 0.18 percent of the population would have 0.51 percent of the representation under the current rules, whereas California, with 12.01 percent of the population would have 10.2 percent of the representation. With a 1000-seat House this would change to 0.35 percent and 11.19 percent, respectively. And with a 5000-seat House, they would be 0.21 percent and 11.83 percent.

This does not take into account the [Banzhaf][bz] or [Shapley-Shubik][ss] power indices, which could result in much different anaylses.

| State | Population | Pop % | 435 # | 435 % | 1000 # | 1000 % | 5000 # | 5000 % | |-------|------------|-------|-------|-------|-------|----

// Response to https://stackoverflow.com/a/59556845
const shrink = (len) => (str) => {
const words = str .split (' ')
const idx = words .findIndex (
(w, i) => words .slice(0, i) .join (' ') .length > len
)
return idx < 0 ? str : words .slice (0, idx - 1) .join ( ' ') + '...'
}
@CrossEye
CrossEye / sieveAll.js
Last active December 7, 2020 21:49
Sieve of Eratosthenes
// all primes
const sieve = function * () {
const C = {}
let q = 2
while (true) {
if (C [q]) {
C [q] .forEach (n => C [n + q] = [... (C [n + q] || []), n] )
delete C[q]
} else {
@CrossEye
CrossEye / Primary.md
Last active March 24, 2022 19:37
A New Primary Calendar

A New Primary Calendar

Photo by Josh Carter on Unsplash

A recent piece from FiveThirtyEight on [an imaginary primary calendar][pr] for the Democrats got me thinking. There really is no good reason to continue letting Iowa and New Hampshire dictate so much about U.S. elections. Candidates who would otherwise be viable are forced out because of poor showings in these unrepresentative states. And candidates who might otherwise sink are buoyed by winning one of these states.

There is simply no reason for this. Every state should be offered a chance to help with the winnowing of candidates. And yet a national primary day would hurt more than it would help.

As we can see, for any n, the actual output will be [n, n], not [n, -1]

While I have no problem with hand-waving a bit in a mathematical argument for popular consumption, "as we can see" is really begging the question. If we could simply see this, then we would really not need to proof by contradiction at all. I think this should be spelled out.

For example, the cardinality of [0, 1, 2, 3, Infinity] is 4, the same as its length.

Oops. There seems to be a miscount here.