eff.js
One-Shot Algebric Effects on JavaScript Generators
It is JavaScript port of eff.lua.
Many thanks to @Nymphium!
Install
NPM:
$ npm install eff.js
Yarn:
$ yarn add eff.js
Example
import {inst, handler, combineHandlers, execute} from 'eff.js';
const write = inst('write');
const httpGet = inst('httpGet');
const main = async function* () {
yield write('hello world');
yield write((yield httpGet('https://example.com')).replace(/\n/, '⏎').slice(0, 20) + '...');
};
const handleWrite = handler(
write,
async function* (v) {
return v;
},
async function* (k, text) {
console.log(text);
return yield* k();
},
);
const handleWriteWithTimestamp = handler(
write,
async function* (v) {
return v;
},
async function* (k, text) {
console.log(new Date(), text);
return yield* k();
}
);
const handleHttpGet = handler(
httpGet,
async function* (v) {
return v;
},
async function* (k, url) {
const text = await (await fetch(url)).text();
return yield* k(text);
},
);
(async () => {
console.log('write + httpGet:');
await execute(combineHandlers(handleWrite, handleHttpGet)(main));
console.log();
console.log('write with timestamp + httpGet:');
await execute(combineHandlers(handleWriteWithTimestamp, handleHttpGet)(main));
console.log();
})();
API
See API.md.
Technical Note
What's difference between this and eff.lua?
eff.lua is built on Lua's asymmetric coroutines. It is more powerful than
JavaScript generators: Lua's one can do yield
from anywhere, but JavaScript's
one can do yield
only from generator functions (function *
). Thus, eff.js
is less convenient than eff.lua. However this porting is interesting and
important technically.
How to emulate stackful coroutine on stackless coroutine.
According to Moura et al [1], both of coroutines call asymmetric. Lua's
coroutine calls stackful that can yield
over call stacks, and JavaScript
generator calls stackless on the other hand. In general, stackful coroutine
is more powerful than stackless one. So, it is important how to emulate stackful
coroutine on stackless coroutine.
Of course, it is impossible that usual stackless corutine emulates stackful
coroutine. Therefore, considering restricted JavaScript is needed. This
JavaScript has only generator functions, but generator operations are exception,
then relations between asymmetric coroutine operation and JavaScript shows the
following table:
| Asymmetric coroutine | JavaScript |
---|
Creates a coroutine | co = create f | co = f() |
Calls a function | f() | yield* f() |
Yields a coroutine | yield v | yield v |
Resumes a coroutine | resume co v | co.next(v) |
I implemented eff.js in accordance with this table. The interesting point is
vh
of handler
function is generator function on eff.js. It is important to
work resend
invocation correctly.
Reference
- Moura, Ana Lúcia De, and Roberto Ierusalimschy. "Revisiting coroutines."
ACM Transactions on Programming Languages and Systems (TOPLAS) 31.2 (2009):
6.
License
MIT
(C) 2019 TSUYUSATO "MakeNowJust" Kitsune