let churchZero = f => x => x; let nextChurch = n => f => x => f(n(f)(x)); let toInt = c => c((x) => x + 1)(0); let toChurch = n => n === 0 ? churchZero : nextChurch(toChurch(n - 1)); console.log(toInt(toChurch(5)) === 5);
The first two lines of this snippet are the real heart of the implementation. In two functions, you have a way to define every natural number. It works by creating a functional call each time
nextChurch is called. For example, nextChurch(churchZero(0)) gives you the equivalent of what would be 1. Call that with nextChurch and you would have 2, and so on.
The next two lines are creating helper functions. They allow you to easily create and verify what the numbers look like in
int (decimal) format.
The final line is a simple test that asserts that any number you pass in and then decode will equal that same number.