# Church Numerals in ES6 JavaScript

I was reading up a bit on Lambda Calculus today and I came across the concept of Church Numerals. It has been since my university days since I had come across them, but they captured my interest once more. The idea that you can describe something as fundamental as natural numbers in a purely functional method is powerful. Since I have been dabbling quite a bit in JavaScript lately (and trying to make the most of capabilities as a first-class functional language), I decided to see what Church Numerals would look like in the language.

A lot of the implementations I came across where done in very old styles of JavaScript, before widespread use of its functional underpinnings. I took it upon myself to see what I could come up with using the new language feature in ES6.

```
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.