Skip to content

Instantly share code, notes, and snippets.

View jooyunghan's full-sized avatar

Jooyung Han jooyunghan

View GitHub Profile
@jooyunghan
jooyunghan / babel-inorder.js
Created December 2, 2016 01:58
Binary inorder traversal using generator (and its babel-transpiled)
var _marked = [inorder].map(regeneratorRuntime.mark);
function inorder(t) {
return regeneratorRuntime.wrap(function inorder$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
if (t) {
_context2.next = 2;
break;
function fibo2() {
var a, b; // local vars
return wrap(function (c) {
while (1) {
switch (c.next) {
case 0:
a=0, b=1;
case 1: // while begin
if (!true) { // while cond
c.next = 3;
function Context() {
this.next = 0;
}
var stop = {};
Context.prototype.stop = function stop() {
return stop;
}
function GenIter(c, genf) {
this.c = c;
this.genf = genf;
}
GenIter.prototype.next = function next() {
var f = this.genf;
var result = f(this.c);
if (result === stop) {
return {done: true};
@jooyunghan
jooyunghan / pair.js
Created August 30, 2017 01:39
Pair implementation in Smalltalk-72 style
function pair(left, right) {
return {
left() {
if (arguments.length == 1) left = arguments[0];
return left;
},
right() {
if (arguments.length == 1) right = arguments[0];
return right;
},
@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;
}
};
}
@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;
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();
}
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 coro(g) {
g.next();
return g.next.bind(g);
}