How do I write an arrow function in ES6 recursively?

后端 未结 12 882
南旧
南旧 2020-12-08 06:36

Arrow functions in ES6 do not have an arguments property and therefore arguments.callee will not work and would anyway not work in strict mode even

12条回答
  •  执念已碎
    2020-12-08 07:23

    I found the provided solutions really complicated, and honestly couldn't understand any of them, so i thought out a simpler solution myself (I'm sure it's already known, but here goes my thinking process):

    So you're making a factorial function

    x => x < 2 ? x : x * (???)
    

    the (???) is where the function is supposed to call itself, but since you can't name it, the obvious solution is to pass it as an argument to itself

    f => x => x < 2 ? x : x * f(x-1)
    

    This won't work though. because when we call f(x-1) we're calling this function itself, and we just defined it's arguments as 1) f: the function itself, again and 2) x the value. Well we do have the function itself, f remember? so just pass it first:

    f => x => x < 2 ? x : x * f(f)(x-1)
                                ^ the new bit
    

    And that's it. We just made a function that takes itself as the first argument, producing the Factorial function! Just literally pass it to itself:

    (f => x => x < 2 ? x : x * f(f)(x-1))(f => x => x < 2 ? x : x * f(f)(x-1))(5)
    >120
    

    Instead of writing it twice, you can make another function that passes it's argument to itself:

    y => y(y)
    

    and pass your factorial making function to it:

    (y => y(y))(f => x => x < 2 ? x : x * f(f)(x-1))(5)
    >120
    

    Boom. Here's a little formula:

    (y => y(y))(f => x => endCondition(x) ? default(x) : operation(x)(f(f)(nextStep(x))))
    

    For a basic function that adds numbers from 0 to x, endCondition is when you need to stop recurring, so x => x == 0. default is the last value you give once endCondition is met, so x => x. operation is simply the operation you're doing on every recursion, like multiplying in Factorial or adding in Fibonacci: x1 => x2 => x1 + x2. and lastly nextStep is the next value to pass to the function, which is usually the current value minus one: x => x - 1. Apply:

    (y => y(y))(f => x => x == 0 ? x : x + f(f)(x - 1))(5)
    >15
    

提交回复
热议问题