Skip to content

Instantly share code, notes, and snippets.

@gaswelder
gaswelder / coroutines.js
Created June 23, 2019 23:56
Coroutines emulation in Javascript
// This is an example of how cooperative multitasking could be
// emulated in Javascript.
// Javascript has only generators, which are "half-coroutines",
// meaning they can only yield to their called and not other
// generators. We can add a special "coordinator" generator and
// a yielding convention to make it look almost exactly like
// we have full coroutines.
// Note that the yield* operator is not what we need here.
// https://github.com/yankouskia/love-triangle
module.exports = function getLoveTrianglesCount(preferences = []) {
return (
preferences.map((x, i) => getTriangle(preferences, i)).filter(x => x)
.length / 3
);
};
const getTriangle = (preferences, index) => {
if (preferences[index] - 1 == index) {
(ns clojure-noob.core
(:gen-class))
(defn parse [url]
(println url)
(org.jsoup.Jsoup/parse (slurp url)))
(defn href [a] (.attr a "href"))
(defn fetch [a] (parse (href a)))