# You may have to edit this file to delete header lines produced by # mailers or news systems from this file (all lines before these shell # comments you are currently reading). # Shell archive made by dwjones on Fri 23 Apr 2021 12:10:17 PM CDT # To install this software on a UNIX system: # 1) create a directory (e.g. with the shell command mkdir stuff) # 2) change to that directory (e.g. with the command cd stuff), # 3) direct the remainder of this text to sh (e.g. sh < ../savedmail). # This will make sh create files in the new directory; it will do # nothing else (if you're paranoid, you should scan the following text # to verify this before you follow these directions). Then read README # in the new directory for additional instructions. cat > README <<\xxxxxxxxxx These files all compute exactly the same output (2**30) Demo.java -- the base demonstration LazyDemo.java -- Demo.java converted to use lazy evaluation with lambda exprs AnonDemo.java -- LazyDemo.java rewritten to use anonymous inner classes InnerDemo.java -- AnonDemo.java rewritten to used named inner classes OuterDemo.java -- InnerDemo.java rewritten with no inner classes MemoDemo.java -- LazyDemo.java rewritten with memoization to speed evaluation All this code is in the public domain under the CC0 license, and major parts of it appeared in Wikipedia at various times because I put it there. Author: Douglas W. Jones Date: Apr. 23, 2021 xxxxxxxxxx cat > Demo.java <<\xxxxxxxxxx // the base from which all other files in this collection are developed public class Demo { public static void main(String[] args) { int a = 1; for (int i = 0; i < 30; i++) { a = a + a; } System.out.println( "= " + a ); } } xxxxxxxxxx cat > LazyDemo.java <<\xxxxxxxxxx // a rewrite of Demo.java using lazy evaluation interface Lazy { T eval(); } public class LazyDemo { public static void main(String[] args) { Lazy a = ()-> 1; for (int i = 0; i < 30; i++) { final Lazy b = a; a = ()-> b.eval() + b.eval(); } System.out.println( "= " + a.eval() ); } } xxxxxxxxxx cat > AnonDemo.java <<\xxxxxxxxxx // a rewrite of Lazy.java using anonymous inner classes instead of lambda exprs interface Lazy { T eval(); } public class AnonDemo { public static void main(String[] args) { // Lazy a = ()-> 1; // replaced with the following Lazy a = new Lazy() { public Integer eval() { return 1; } }; for (int i = 0; i < 30; i++) { final Lazy b = a; // a = ()-> b.eval() + b.eval(); // replaced with the following a = new Lazy() { public Integer eval() { return b.eval() + b.eval(); } }; } System.out.println( "= " + a.eval() ); } } xxxxxxxxxx cat > InnerDemo.java <<\xxxxxxxxxx // A rewrite of AnonDemo using named inner classes instead of anonymous ones interface Lazy { T eval(); } public class InnerDemo { public static void main(String[] args) { // Lazy a = ()-> 1; // replaced with the following class InitA implements Lazy { public Integer eval() { return 1; } }; Lazy a = new InitA(); for (int i = 0; i < 30; i++) { final Lazy b = a; // a = ()-> b.eval() + b.eval(); // replaced with the following class UpdateA implements Lazy { public Integer eval() { return b.eval() + b.eval(); } }; a = new UpdateA(); } System.out.println( "= " + a.eval() ); } } xxxxxxxxxx cat > OuterDemo.java <<\xxxxxxxxxx // a rewrite of InnerDemo using outer classes instead of inner classes interface Lazy { T eval(); } class InitA implements Lazy { public Integer eval() { return 1; } }; class UpdateA implements Lazy { private final Lazy b; public UpdateA( Lazy b ) { this.b = b; } public Integer eval() { return b.eval() + b.eval(); } }; public class OuterDemo { public static void main(String[] args) { // Lazy a = ()-> 1; // replaced with the following Lazy a = new InitA(); for (int i = 0; i < 30; i++) { final Lazy b = a; // a = ()-> b.eval() + b.eval(); // replaced with the following a = new UpdateA( b ); } System.out.println( "= " + a.eval() ); } } xxxxxxxxxx cat > MemoDemo.java <<\xxxxxxxxxx // a rewrite of LazyDemo with added memoization used to make it efficient interface Lazy { T eval(); } class Memo implements Lazy { // wrapper for a lazy object private Lazy lazy; // the lazy object wrapped in this memo private T memo = null; // the memorandum of that object's value public Memo( Lazy lazy ) { this.lazy = lazy; } public T eval() { if (memo == null) memo = lazy.eval(); return memo; } } public class MemoDemo { public static void main(String[] args) { Lazy a = ()-> 1; for (int i = 0; i < 30; i++) { final Lazy b = new Memo(a); a = ()-> b.eval() + b.eval(); } System.out.println( "= " + a.eval() ); } } xxxxxxxxxx