Skip to content

Instantly share code, notes, and snippets.

@wentout
Last active January 13, 2020 11:57
Show Gist options
  • Save wentout/1b11cffa9d0342121829fd75eb7476c2 to your computer and use it in GitHub Desktop.
Save wentout/1b11cffa9d0342121829fd75eb7476c2 to your computer and use it in GitHub Desktop.
Example of Real Question you should ask your engineer if there is something about Y-Combinator work to be done...
const log = function (message, s) {
process.stdout.write(`${String(message).padEnd(17)} : ${s || ''}\n`);
};
const YCombinator = async function (realWork) {
log('start', this);
return await (async function (cb_x) {
log('inside', this);
return await cb_x.call(this, cb_x);
}).call(this, async function (cb_y) {
log('outside', this);
return await realWork.call(this, async function (...subargs) {
log('out_in_side', this);
return await (await cb_y.call(this, cb_y)).call(this, ...subargs);
});
});
};
const factorialWithYCombinator = async function (itself) {
log('real_work combo', this);
return async function (n) {
if (n < 2) {
log('real_work finish', this);
return 1;
}
log('real_work', this);
return n * (await itself.call(this, n - 1));
};
};
(async () => {
const calculation = await YCombinator.call('"this" -> combine', factorialWithYCombinator);
debugger;
log(await calculation.call('"this" -> work', 5), 'done')
})();
const log = function (message, s) {
process.stdout.write(`${String(message).padEnd(17)} : ${s || ''}\n`);
};
const YCombinator = function (realWork) {
log('start', this);
return (function (cb_x) {
log('inside', this);
return cb_x.call(this, cb_x);
}).call(this, function (cb_y) {
log('outside', this);
return realWork.call(this, function (...args) {
log('out_in_side', this);
return cb_y.call(this, cb_y).call(this, ...args);
});
});
};
const factorialWithYCombinator = function (itself) {
log('real_work combo', this);
return function (n) {
if (n < 2) {
log('real_work finish', this);
return 1;
}
log('real_work', this);
return n * itself.call(this, n - 1);
};
};
const calculation = YCombinator.call('"this" -> combine', factorialWithYCombinator);
debugger;
log(calculation.call('"this" -> work', 5), 'done');
@wentout
Copy link
Author

wentout commented Jan 13, 2020

So, we should ask about this keyword, whether we will use from "combine" or from "work".
And might be the aswer is: we should use the Factory of Y-Combinators :)
But then what will be this for that factory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment