Skip to content

Instantly share code, notes, and snippets.

@mfirhas
Last active September 20, 2021 04:44
Show Gist options
  • Save mfirhas/9bc56185b7a4fe5781471bb1fefe702f to your computer and use it in GitHub Desktop.
Save mfirhas/9bc56185b7a4fe5781471bb1fefe702f to your computer and use it in GitHub Desktop.
Print your name 1000 times without looping in Java
// Y = πœ†π‘“.(πœ†π‘₯.𝑓(π‘₯π‘₯))(πœ†π‘₯.𝑓(π‘₯π‘₯))
public class YCombinator {
public interface F<T,U> {
Void apply(T name,U n);
}
public interface FF<T,U> {
F<T,U> apply(F<T,U> f);
}
public interface YF<T,U> {
F<T,U> apply(YF<T,U> yf);
}
static <T,U> F<T,U> Y(FF<T,U> ff) {
YF<T,U> g = yf -> ff.apply((name, n) -> yf.apply(yf).apply(name,n));
return g.apply(g);
}
public static void main(String... arguments) {
F<String,Integer> printName1000x = Y(f -> (name, n) ->
{
if (n > 0) {
System.out.printf("%d. %s\n",n,name);
f.apply(name,n-1);
}
return null;
});
printName1000x.apply("Fathir",1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment