Created
May 21, 2017 20:54
-
-
Save considerate/824f896c7b7c5f0a115a287ec8592454 to your computer and use it in GitHub Desktop.
Small example of Java Functions without generics.
This file contains 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
Compiled from "Function.java" | |
public abstract class Function { | |
public Function(); | |
Code: | |
0: aload_0 | |
1: invokespecial #1 // Method java/lang/Object."<init>":()V | |
4: return | |
public abstract java.lang.Object apply(java.lang.Object); | |
} |
This file contains 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
public abstract class Function { | |
public abstract Object apply(Object a); | |
} |
This file contains 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
Compiled from "Main.java" | |
// inner function of add | |
class Main$1$1 extends Function { | |
final java.lang.Object val$a; | |
final Main$1 this$0; | |
Main$1$1(Main$1, java.lang.Object); | |
Code: | |
0: aload_0 | |
1: aload_1 | |
2: putfield #1 // Field this$0:LMain$1; | |
5: aload_0 | |
6: aload_2 | |
7: putfield #2 // Field val$a:Ljava/lang/Object; | |
10: aload_0 | |
11: invokespecial #3 // Method Function."<init>":()V | |
14: return | |
public java.lang.Integer apply(java.lang.Object); | |
Code: | |
0: aload_0 | |
1: getfield #2 // Field val$a:Ljava/lang/Object; | |
4: checkcast #4 // class java/lang/Integer | |
7: invokevirtual #5 // Method java/lang/Integer.intValue:()I | |
10: aload_1 | |
11: checkcast #4 // class java/lang/Integer | |
14: invokevirtual #5 // Method java/lang/Integer.intValue:()I | |
17: iadd | |
18: invokestatic #6 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; | |
21: areturn | |
public java.lang.Object apply(java.lang.Object); | |
Code: | |
0: aload_0 | |
1: aload_1 | |
2: invokevirtual #7 // Method apply:(Ljava/lang/Object;)Ljava/lang/Integer; | |
5: areturn | |
} |
This file contains 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
Compiled from "Main.java" | |
// add | |
final class Main$1 extends Function { | |
Main$1(); | |
Code: | |
0: aload_0 | |
1: invokespecial #1 // Method Function."<init>":()V | |
4: return | |
public Function apply(java.lang.Object); | |
Code: | |
0: new #2 // class Main$1$1 | |
3: dup | |
4: aload_0 | |
5: aload_1 | |
6: invokespecial #3 // Method Main$1$1."<init>":(LMain$1;Ljava/lang/Object;)V | |
9: areturn | |
public java.lang.Object apply(java.lang.Object); | |
Code: | |
0: aload_0 | |
1: aload_1 | |
2: invokevirtual #4 // Method apply:(Ljava/lang/Object;)LFunction; | |
5: areturn | |
} |
This file contains 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
Compiled from "Main.java" | |
// factorial | |
final class Main$2 extends Function { | |
Main$2(); | |
Code: | |
0: aload_0 | |
1: invokespecial #1 // Method Function."<init>":()V | |
4: return | |
public java.lang.Object apply(java.lang.Object); | |
Code: | |
0: aload_1 | |
1: checkcast #2 // class java/lang/Integer | |
4: invokevirtual #3 // Method java/lang/Integer.intValue:()I | |
7: ifne 15 | |
10: iconst_1 | |
11: invokestatic #4 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; | |
14: areturn | |
15: aload_1 | |
16: checkcast #2 // class java/lang/Integer | |
19: invokevirtual #3 // Method java/lang/Integer.intValue:()I | |
22: getstatic #5 // Field Main.factorial:LFunction; | |
25: aload_1 | |
26: checkcast #2 // class java/lang/Integer | |
29: invokevirtual #3 // Method java/lang/Integer.intValue:()I | |
32: iconst_1 | |
33: isub | |
34: invokestatic #4 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; | |
37: invokevirtual #6 // Method Function.apply:(Ljava/lang/Object;)Ljava/lang/Object; | |
40: checkcast #2 // class java/lang/Integer | |
43: invokevirtual #3 // Method java/lang/Integer.intValue:()I | |
46: imul | |
47: invokestatic #4 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; | |
50: areturn | |
} |
This file contains 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
Compiled from "Main.java" | |
public class Main { | |
static Function add; | |
static Function factorial; | |
public Main(); | |
Code: | |
0: aload_0 | |
1: invokespecial #1 // Method java/lang/Object."<init>":()V | |
4: return | |
public static void main(java.lang.String[]); | |
Code: | |
0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream; | |
3: getstatic #3 // Field factorial:LFunction; | |
6: bipush 11 | |
8: invokestatic #4 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; | |
11: invokevirtual #5 // Method Function.apply:(Ljava/lang/Object;)Ljava/lang/Object; | |
14: checkcast #6 // class java/lang/Integer | |
17: invokevirtual #7 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V | |
20: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream; | |
23: getstatic #8 // Field add:LFunction; | |
26: iconst_5 | |
27: invokestatic #4 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; | |
30: invokevirtual #5 // Method Function.apply:(Ljava/lang/Object;)Ljava/lang/Object; | |
33: checkcast #9 // class Function | |
36: iconst_3 | |
37: invokestatic #4 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; | |
40: invokevirtual #5 // Method Function.apply:(Ljava/lang/Object;)Ljava/lang/Object; | |
43: invokevirtual #7 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V | |
46: return | |
static {}; | |
Code: | |
0: new #10 // class Main$1 | |
3: dup | |
4: invokespecial #11 // Method Main$1."<init>":()V | |
7: putstatic #8 // Field add:LFunction; | |
10: new #12 // class Main$2 | |
13: dup | |
14: invokespecial #13 // Method Main$2."<init>":()V | |
17: putstatic #3 // Field factorial:LFunction; | |
20: return | |
} |
This file contains 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
public class Main { | |
static Function add = new Function() { | |
public Function apply(Object a) { | |
return new Function() { | |
public Integer apply(Object b) { | |
return ((Integer) a) + ((Integer) b); | |
} | |
}; | |
} | |
}; | |
static Function factorial = new Function() { | |
public Object apply(Object a) { | |
if((Integer) a == 0) { | |
return 1; | |
} else { | |
return (Integer) a * (Integer) factorial.apply((Integer) a - 1); | |
} | |
} | |
}; | |
public static void main(String []args) { | |
System.out.println((Integer) factorial.apply(11)); | |
System.out.println(((Function) add.apply(5)).apply(3)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment