DEV Community

Cover image for Anonymous Recursion in JavaScript

Anonymous Recursion in JavaScript

simo on August 06, 2017

( ( (f) => f(f) ) ( (f) => (l) => { console.log(l) if (l.length) f(f)(l.slice(1)) console.log...
Collapse
 
thereversengineer profile image
the-reversengineer

Hi. Thank you.

Only one suggestion: maybe the "self executing function" should be:

(
  (hi) =>
    (
      (dev) => `${hi} ${dev}`
    )
)
('hey')
('dev.to')
Enter fullscreen mode Exit fullscreen mode
Collapse
 
simov profile image
simo

That would work too. And it's looking good.

Collapse
 
thereversengineer profile image
the-reversengineer

Not only, but it better separates the code term (one) from the input terms (two of them).

In my version, you can see that all the long consecutive block:

(
  (hi) =>
    (
      (dev) => `${hi} ${dev}`
    )
)
Enter fullscreen mode Exit fullscreen mode

is the code term as a whole, and the next two terms are inputs.
Getting all the "input" as a whole ensures portability (that is, you can take that self-sufficient code and use somewhere else).

For any novice reader of my reply: in JavaScript (and, more in general, in classical lambda-calculus theory), the convention is to left-associate terms together in more-than-two-items sequences, so the two input terms cannot be grouped in one term only (like we did for the code one).

A fully parenthesiszed equivalent expression would be:

(
  (
    (
      (hi) =>
        (
          (dev) => `${hi} ${dev}`
        )
    )
    ('hey')
  )
  ('dev.to')
)
Enter fullscreen mode Exit fullscreen mode

indeed.

The outermost parentheses, in my notation, indicates that we want to reduce (or, in classical programming languages speaking, to run) what's inside of them.


Regarding my first reply, I was just comparing your version of the invocation code (the one with the 'dev.to' term inside the function to be returned) with this other invocation
foo('hey')('dev.to')
you wrote right before.

Simply, my version applies the arguments like this 😊.
Regards.

Thread Thread
 
simov profile image
simo

Now I see, I was looking at the Going Deep example where I intentionally wanted to nest the arguments, so that the example resembles more closely the Anonymous Recursion one below it.

I should probably update the example in the Closures chapter. Thanks for the feedback!

Collapse
 
yuriss profile image
Yuri Santos • Edited

Correct me if I'm wrong.
Function that returns another function is high order function not a closure. Closure relates to scope and not to return.

Collapse
 
bitwiselover profile image
undefined

That is absolutely correct, a closure has access to the function environment (scope) of it's parent. A higher-order function can be described as a function that accepts a function as an argument and/or returns a function.

Collapse
 
simov profile image
simo • Edited

Thank you for your input!

I've fixed the definition, this time I got it straight from MDN just to make sure I'm not mistaken something.

Collapse
 
proddi profile image
proddi

Usually this is a perfect example howto to not write code. But for an interview it's great!

Collapse
 
simov profile image
simo

I agree, the anonymous recursion is a bit extreme and hard to maintain, though each part of this example taken separately is actually very useful in day to day programming.

Collapse
 
mechrisreed profile image
Chris Reed

You should bring up the y-combinator in there...

Collapse
 
ismagilovkamil profile image
Kamil

Hi, may i translate your article in Russian and publish it on my resource: lovefrontend.ru/ . I put links to your original article. Thanks.

Collapse
 
simov profile image
simo

Sure, if there is a link to the original article - then no problem.