is one of the fixed-point combinators in untyped lambda calculus
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.gitignore
.travis.yml
LICENSE
Makefile
README.md
index.js
package-lock.json
package.json
test.js

README.md

y-combinator

is one of the fixed-point combinators in untyped lambda calculus

All credits go to Haskell Curry.

Installation | Annotated source | Example | License

KLP Build Status

Installation

npm install y-combinator

Annotated source

I have no idea what is the Y combinator operator (maybe one day I will :P), I have just stolen the code from a guy called Douglas Crockford a.k.a Walker Texas JS.

Here it is the code, it is a function that consumes a function and returns a function, that consumes a function that returns a function ...

function Y(le) {
  return (function (f) {
    return f(f)
  }(function (f) {
    return le(function (x) {
      return f(f)(x)
    })
  }))
}

... ok, now export the Y function

module.exports = Y

Example

Q. What can I do with the Y Combinator?

A. Mmh, for example you can use it to get a factorial function, the following code works!

var Y = require('y-cominator')

var factorial = Y(fac => n => n <= 2 ? n : n * fac(n - 1))

console.log(factorial(5)) // 120

License

MIT