This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const state = run(v => s => [v, s], | |
(arg, fun) => s => { | |
const [nextArg, nextState] = arg(s) | |
return fun(nextArg)(nextState) | |
}) | |
function set(s) { return _ => [s, s] } | |
function get(s) { return [s, s] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const run = (of, chain) => fun => { | |
const i = fun()[Symbol.iterator]() | |
return walk() | |
function walk(arg) { | |
const step = i.next(arg) | |
return step.done ? of(step.value) : chain(step.value, walk) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
arr.forEach(function* i() { | |
yield i; | |
yield i; | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function* dup(arr) { | |
for(const i of arr) { | |
yield i; | |
yield i; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test("counterControl", async () => { | |
expect.assertions(3) | |
for await(const i of Counter.mainControl([ | |
{type:"MENU", value:<span>Menu</span>}, | |
{type:"VALUE", value:10}, | |
{type:"CONTROL", value:<span>Control</span>}, | |
{type:"FLUSH"}, | |
{type:"VALUE", value: 11}, | |
{type:"FLUSH"}])) | |
if (i.type === "CONTROL") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function incr(s) { | |
const ns = s + 1 | |
return [ns, ns] | |
} | |
function incrX2(s) { | |
const [s1, r1] = incr(s) | |
const [s2, r2] = incr(s1) | |
return [s2, r1 + r2] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function* incr() { | |
return (yield set((yield get) + 1)) | |
} | |
function* incrX2() { | |
return (yield* incr()) + (yield* incr()) | |
} | |
const main = state(incrX2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function order(customer, item) { | |
begin(); | |
customer.balance -= item.price; | |
item.vendor.balance += item.price; | |
if (customer.balance < 0) | |
throw new NegativeBalance(); | |
scheduleShipment(customer, item) | |
commit(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function reactiveComponent(fun) { | |
class Reactive extends React.PureComponent { | |
constructor(props) { | |
super(props) | |
this.state = {current: null} | |
} | |
componentDidMount() { | |
(async () => { | |
for await(const current of fun(this.props)) | |
this.setState({current}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function subject(...args) { | |
let callback | |
const queue = [] | |
const iter = thread() | |
let running = true | |
return { | |
send(event) { | |
if (callback) | |
callback() | |
queue.push(event) |