Skip to content

Instantly share code, notes, and snippets.

@yuki-takeichi
Created November 18, 2019 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuki-takeichi/180980240e7153d7615763c81cb4a6a6 to your computer and use it in GitHub Desktop.
Save yuki-takeichi/180980240e7153d7615763c81cb4a6a6 to your computer and use it in GitHub Desktop.
Yoneda as CPS transformer
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