Skip to content

Instantly share code, notes, and snippets.

@homam
Created June 28, 2016 18:21
Show Gist options
  • Save homam/0aba5ecc424f665a2461590a322d6fbb to your computer and use it in GitHub Desktop.
Save homam/0aba5ecc424f665a2461590a322d6fbb to your computer and use it in GitHub Desktop.
const wrap1 = (arrow, before, after, f) =>
arrow.arr(before).pipe(arrow.arrM(f)).pipe(arrow.arr(after))
const wrap12 = (arrow, before, after, f) => new arrow(x => {
let b = arrow.arr(before).run(x)
let y = arrow.id.pipe(arrow.arrM(f)).run(b)
return arrow.id.pipe(arrow.arr(after)).run(y)
})
class Func {
constructor(f) {
this.pipe = g => new Func(x => g.run(this.run(x)))
this.map = g => new Func(x => g(this.run(x)))
this.bind = g => g(this.run(x))
this.run = x => f(x)
}
}
Func.id = new Func(x => x)
Func.arr = f => new Func(f)
Func.arrM = f => new Func(f)
console.log(
wrap1(Func,
x => x*2,
x => x+1,
x => x * x / 2
).run(3)
)
class Prom {
constructor(f) {
this.pipe = g => new Prom(x => this.run(x).then(y => g.run(y)))
this.map = g => new Prom(x => this.run(x).then(g))
this.bind = g => new Prom(x => this.run(x).then(g))
this.run = x => f(x)
}
}
Prom.id = new Prom(x => Promise.resolve(x))
Prom.arr = f => new Prom(x => Promise.resolve(f(x)))
Prom.arrM = f => new Prom(f)
wrap1(Prom,
x => x*2,
x => x+1,
x => Promise.resolve(x * x / 2)
).run(3)
.then(x => console.log(`P = ${x}`))
const wrap2 = (arrow, before, after, f) => new arrow(x => {
let b = arrow.arr(before).run(x)
let y = arrow.id.pipe(arrow.arrM(f)).run(x)
let a = arrow.id.pipe(arrow.arr(after)).run(y)
return arrow.id.run(y)
})
wrap2(Prom,
_ => Date.now(),
(y) => console.log(`${y} produced after ${Date.now()}`),
x => Promise.resolve(x * x / 2)
).run(3)
.then(x => console.log(`WRAP2 P = ${x}`))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment