factorial, final long number) { if (number == 1) return TailInvoke.done(factorial); else return TailInvoke.call(() -> factorialTailRecursion(factorial + number, number - 1)); }
²âÊÔ´úÂë
() { System.out.println(factorialTailRecursion(1,10_000_000).invoke()); }
·¢ÏÖ½á¹ûÔËתÁ¼ºÃ
50000005000000 Process finished with exit code 0ÓÉÓڽ׳˵ļÆËãÒ»°ã³õʼֵ¶¼Îª1,ËùÒÔÔÙ½øÒ»²½°ü×°Ò»ÏÂ,½«³õʼֵÉèÖÃΪ1
(final long number) { return factorialTailRecursion(1, number).invoke(); }
×îÖÕµ÷ÓôúÂëÈçÏÂ,ÍêÈ«ÆÁ±ÎÁËβµÝ¹éµÄʵÏÖϸ½Ú
() { System.out.println(factorial(10)); //½á¹ûΪ 3628800 }
×ܽ᱾ÎĽ²½âÁËÀûÓÃlambdaÀÁ¼ÓÔصÄÌØÐÔÍê³ÉÁ˵ݹéÖÐÕ»Ö¡µÄ¸´ÓÃ,ʵÏÖÁ˺¯ÊýʽÓïÑÔ±àÒëÆ÷µÄ'βµÝ¹é'ÓÅ»¯,ËäÈ»ÉÏÃæµÄÀý×Ӻܼòµ¥£¬µ«ÊÇÉè¼ÆµÄ½Ó¿ÚºÍ°ü×°À඼ÊÇͨÓõģ¬¿ÉÒÔ˵ÈκÎÐèҪʹÓÃβµÝ¹éµÄ¶¼¿ÉÒÔʹÓÃÉÏÃæµÄ´úÂëÀ´ÊµÏÖβµÝ¹éµÄÓÅ»¯£¬ÕâÒ²ËãÊÇΪ±àÒëÆ÷°ïÁ˵ãæ°É¡£
ÉÏһƪ:¿ªÊ¼Java8Ö®Âã¨Î壩 -- Java8ÖеÄÅÅÐò
¡¡