Anonymous Recursion in JavaScript
Tags: recursion, javascript, closures
Published: 2017-08-06 10:46:09

---
title: Anonymous Recursion in JavaScript
published: true
description: Anonymous Recursion in JavaScript
tags: recursion, javascript, closures
cover_image: https://thepracticaldev.s3.amazonaws.com/i/pgio2u4u438cq49tnoqp.png
---


```js
(
  (
    (f) => f(f)
  )
  (
    (f) =>
      (l) => {
        console.log(l)
        if (l.length) f(f)(l.slice(1))
        console.log(l)
      }
  )
)
(
  [1, 2, 3]
)
```

Yes, there is such thing, and I thought it would be an interesting example to share. It features: [closures][closure], [self-executing functions][self-executing], [arrow functions][arrow-function], [functional programming][fp], and [anonymous recursion][anon-recursion].

You can copy/paste the above example in your browser's console. The output is as follows:

```
[ 1, 2, 3 ]
[ 2, 3 ]
[ 3 ]
[]
[]
[ 3 ]
[ 2, 3 ]
[ 1, 2, 3 ]
```

Speaking of functional programming, here is how similar example look like in [Scheme][scheme] (one of the languages JavaScript was influenced by):

```scheme
(
  (
    (lambda (f) (f f))
    (lambda (f)
      (lambda (l)
        (print l)
        (if (not (null? l)) ((f f) (cdr l)))
        (print l)
      )
    )
  )
  '(1 2 3)
)
```

## Unwind

Like in many other programming languages calling a function is done by appending parentheses `()` after its name:

```js
function foo () { return 'hey' }
foo()
```

In JavaScript we can wrap any number of expressions in parentheses:

```js
('hey', 2+5, 'dev.to')
```

The result of the above snippet is `'dev.to'`. The reason why is because JavaScript returns the last expression as result.

Wrapping a single anonymous (lambda) function in parentheses `()` means that the result will be the [anonymous function][anon-function] itself:

```js
(function () { return 'hey' })
```

That in itself is not very useful because the anonymous function does not have a name, and we won't be able to reference it unless we call it immediately during initialization.

Like a regular function, we can append parentheses `()` after it to call it:

```js
(function () { return 'hey' })()
```

The same goes with the arrow functions:

```js
(() => 'hey')()
```

Again, appending parentheses `()` after the anonymous function means that we are executing it, also known as [self-executing function][self-executing].


## Closures

A [closure][closure] is the combination of a function and the lexical environment within which that function was declared. Combined with [arrow functions][arrow-function] we can define it like this:

```js
var foo = (hi) => (dev) => hi + ' ' + dev
```

Calling the above function in the browser's console will print `'hey dev.to'`:

```js
foo('hey')('dev.to')
```

Note that we have access to the `hi` argument from the outer scope of the enclosing function inside the enclosed inner one.

The above code is identical to:

```js
function foo (hi) {
  return function (dev) { return hi + ' ' + dev }
}
```

And the self-executing version would be:

```js
(
  (hi) =>
    (
      (dev) => `${hi} ${dev}`
    )
    ('dev.to')
)
('hey')
```

First the `hey` parameter is passed to the outermost scope to the above function as `hi` argument. Then that function returns yet another self-executing function that needs to be evaluated first. The `dev.to` parameter is then passed as the `dev` argument to the innermost function, and that function return the final result: `'hey dev.to'`.


## Going Deep

Here is a slightly modified version of the above self-executing function:

```js
(
  (
    (dev) =>
      (hi) => `${hi} ${dev}`
  )
  ('dev.to')
)
('hey')
```

First the `hey` parameter is passed as argument to the outermost scope, but instead of a function there we have yet another expression that needs to be evaluated first. So the `dev.to` parameter is then passed to the inner self-executing function as the `dev` argument and returns another function. That last function is what satisfies the outermost scope and therefore receives the `hey` parameter.

It's important to note that the [self-executing functions][self-executing] and [closures][closure] are used to initialize and encapsulate state, and this is what we're going to use in our next example.


## Anonymous Recursion

Going back to our initial example, this time annotated:

```js
(
  (
    (f) => f(f) // 3.
  )
  (
    (f) => // 2.
      (l) => { // 4.
        console.log(l)
        if (l.length) f(f)(l.slice(1))
        console.log(l)
      }
  )
)
(
  [1, 2, 3] // 1.
)
```

1. The input array `[1, 2, 3]` is passed to the outermost scope
2. This entire function is passed as argument to the function above
3. This function receives the bottom one as argument `f` and calls it with itself
4. `2.` being called in `3.` results in returning the `4.` function which is the one that satisfies the outermost scope and therefore receives the input array as the `l` argument

The reason for all of this is to have a reference to the `f` function inside the recursive one that receives the input array `l`. That way we can call it:

```js
f(f)(l.slice(1))
```

Note that `f` is a closure so we need to call it with itself just to get access to the innermost function that operates on the input array.

For explanatory purposes the first `console.log(l)` statement represents the recursive top-down, and the second one the recursive bottom-up.

## Conclusion

I hope you enjoyed this article and learned something new out of it. Closures, self-executing functions and functional programming patterns are not black magic. They follow simple principles that are easy to understand and fun to play with.

That being said you have to develop a sense of your own when to use them, or not. If your code becomes harder to maintain, then it's probably a good idea to refactor it a little bit.

Nevertheless understanding these fundamental techniques is pivotal to creating clean and elegant solutions, as well as leveling up.

Happy Coding!


  [anon-recursion]: https://en.wikipedia.org/wiki/Anonymous_recursion
  [anon-function]: https://en.wikipedia.org/wiki/Anonymous_function
  [closure]: https://en.wikipedia.org/wiki/Closure_(computer_programming)
  [self-executing]: https://en.wikipedia.org/wiki/Immediately-invoked_function_expression
  [arrow-function]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions
  [fp]: https://en.wikipedia.org/wiki/Functional_programming
  [scheme]: https://en.wikipedia.org/wiki/Scheme_(programming_language)