Created
November 18, 2019 16:21
-
-
Save yuki-takeichi/180980240e7153d7615763c81cb4a6a6 to your computer and use it in GitHub Desktop.
Yoneda as CPS transformer
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 yoneda = f => cont => a => cont(f(a)) | |
const cpsTransform = yoneda | |
const f = x => x + 1 | |
const g = x => x * 2 | |
const apply = (g, f) => x => g(f(x)) | |
/* | |
apply(cpsTransform(f), cpsTransform(g))(cont) | |
== cpsTransform(f)(cpsTransform(g)(cont)) | |
== cpsTransform(f)(b => cont(g(b))) | |
== (cont1 => a => cont1(f(a)))(b => cont(g(b))) | |
== a => (b => cont(g(b)))(f(a)) | |
== a => cont(g(f(a))) | |
== a => cont(apply(g, f)(a)) | |
*/ | |
const assert = require('assert') | |
apply(cpsTransform(f), cpsTransform(g))(x => assert.equal(x, apply(g, f)(1)))(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment