Last active
September 20, 2021 04:44
-
-
Save mfirhas/9bc56185b7a4fe5781471bb1fefe702f to your computer and use it in GitHub Desktop.
Print your name 1000 times without looping in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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