Skip to content

Instantly share code, notes, and snippets.

View jooyunghan's full-sized avatar

Jooyung Han jooyunghan

View GitHub Profile
function coro(g) {
let gId = 0;
wireTimeout(g.next(), gId);
function resume(value) {
wireTimeout(g.next(value), ++gId);
};
function wireTimeout(result, id) {
if (result && result.value instanceof Promise) {
function* knockCodeHandler(preset) {
while (true) {
const input = [];
// after read first input
input.push(getCode(yield));
// keep reading input with timeout(1s)
while (true) {
const e = yield timeout(1000);
if (e) {
function* map(values, gf) {
const result = [];
for (let i = 0; i < values.length; i++) {
result.push(yield* gf(values[i], i, values));
}
return result;
}
function* knockCodeHandler(preset) {
while (true) {
function success() {
run(showToast("pass"));
}
function* showToast(message) {
var div = document.createElement("div");
div.className = "toast in";
div.textContent = message;
document.body.appendChild(div);
yield delay(1000);
@jooyunghan
jooyunghan / index.html
Created December 11, 2017 09:27
Knock Code Demo
<h1>Knock code : 0113</h1>
<div id="knock-code-2" class="knock">
</div>
function coro(g) {
g.next();
return g.next.bind(g);
}
function* knockCodeHandler(preset) {
while (true) {
const input = [];
for (let i=0; i<preset.length; i++) {
input.push(getCode(yield));
}
if (input.join("") === preset) {
success();
} else {
failure();
function makeKnockCodeHandler(preset) {
let input = [];
return function knockCodeHandler(e) {
input.push(getCode(e));
if (input.length === preset.length) {
if (input.join("") === preset) {
success();
} else {
failure();
}
@jooyunghan
jooyunghan / index.js
Created December 3, 2017 13:15
One-shot continuation using symmetric coroutines
const coro = require('symcoro');
const {create, transfer} = coro;
// Figure 4. Implementing one-shot continuations with symmetric coroutines
// http://www.inf.puc-rio.br/~roberto/docs/MCC15-04.pdf
function* call1cc(f) {
// save the continuation "creator"
let currentCoro = coro.current;
@jooyunghan
jooyunghan / index.js
Last active December 3, 2017 12:43
Pattern matching using JavaScript generators
// http://www.inf.puc-rio.br/~roberto/docs/MCC15-04.pdf
function prim(str) {
const len = str.length;
return function* primMatcher(S, pos) {
if (S.substr(pos, len) === str) {
yield pos + len;
}
};
}