Skip to content

Instantly share code, notes, and snippets.

View awto's full-sized avatar

Vitalii Akimov awto

View GitHub Profile
@awto
awto / state.js
Last active June 11, 2018 17:04
state via abstract layer
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] }
@awto
awto / abstract.js
Created June 11, 2018 16:57
Iterable to abstrace
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)
}
}
@awto
awto / dup-foreach.js
Created June 10, 2018 16:35
dup for each
arr.forEach(function* i() {
yield i;
yield i;
})
@awto
awto / dup.js
Created June 10, 2018 16:32
abstract matters dup
function* dup(arr) {
for(const i of arr) {
yield i;
yield i;
}
}
@awto
awto / controls.test.js
Created June 9, 2018 10:29
testing with async generators
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")
@awto
awto / state.js
Created June 8, 2018 17:53
State Monad Inlined
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]
}
@awto
awto / state.js
Last active June 9, 2018 11:15
Converting Coroutine to State monads
function* incr() {
return (yield set((yield get) + 1))
}
function* incrX2() {
return (yield* incr()) + (yield* incr())
}
const main = state(incrX2)
@awto
awto / shop.js
Last active June 6, 2018 11:26
workflow sample
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();
}
@awto
awto / funreact.js
Last active April 5, 2018 13:33
Functional Reactive Component
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})
@awto
awto / leak.js
Created March 5, 2018 17:02
leaking async generator
function subject(...args) {
let callback
const queue = []
const iter = thread()
let running = true
return {
send(event) {
if (callback)
callback()
queue.push(event)