Skip to content

Instantly share code, notes, and snippets.

@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 / 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) => {