title: How is this piece of Recursive lambda call in Java working
body: <p>I recently came across this piece of code in Java. It involves Function and printing fibonacci numbers and it works.</p>
  
  <pre><code>public class AppLambdaSubstitution {
  
  public static Function&lt;Integer, Integer&gt; Y(Function&lt;Function&lt;Integer, Integer&gt;, Function&lt;Integer, Integer&gt;&gt; f) {
      return x -&gt; f.apply(Y(f)).apply(x);
  }
  
  public static void main(String[] args) {
      Function&lt;Integer, Integer&gt; fib = Y(
              func -&gt; x -&gt; {
          if (x &lt; 2)
              return x;
          else
              return func.apply(x - 1) + func.apply(x - 2);
      });
  
      IntStream.range(1,11).
      mapToObj(Integer::valueOf).
      map(fib).forEach(System.out::println);
    }
  }
  </code></pre>
  
  <p>The part that has me confused is <code>return x -&gt; f.apply(Y(f)).apply(x);</code>. Isn't <code>Y(f)</code> a recursive call to the method <code>Y</code>? We keep calling it with the Function <code>f</code> as a parameter. To me, there's no base case for this recursive call to return from. Why is there no overflow resulting from an endless recursive call?</p>