Skip to content

Instantly share code, notes, and snippets.

@homam
Last active July 6, 2016 22:07
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 homam/b5f7b2d5e37009a84e9071385671848b to your computer and use it in GitHub Desktop.
Save homam/b5f7b2d5e37009a84e9071385671848b to your computer and use it in GitHub Desktop.
class Kleisli {
// given f :: x -> Monad y, constructs a category of type:
// Cat (x ↣ y)
constructor(f) {
// this.run = f
this.run = x => f(x)
// this :: Cat (x ↣ y)
// Cat (y ↣ z) -> Cat (x ↣ z)
this.pipe = g => new Kleisli(x => this.run(x).then(g.run)) // then == bind
// utility functon:
// this :: Cat (x ↣ y)
// (y -> Monad z) -> Cat (x ↣ z)
this.pipeTo = g => new Kleisli(x => this.run(x).then(g)) // then == bind
}
}
// Monad => Cat (x ↣ x)
Kleisli.id = monad => new Kleisli(x => monad.resolve(x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment