DEV Community

Discussion on: Anonymous Recursion in JavaScript

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!