/* Bank Accounts! Using closures like in Scheme/Perl. The difference being that these accounts are typed. */ public class topping { static (String,int)->void makeaccount(int balance) { String name = "fred"; (int)->void withdraw; withdraw = fun (int amt)->void { balance = balance - amt; System.out.println(balance); }; (int)->void deposit; deposit = fun (int amt)->void { balance = balance + amt; System.out.println(balance); }; (String,int)->void gateway; gateway = fun (String Op,int amt)->void { if (Op.equals("deposit")) deposit(amt); else if (Op.equals("withdraw")) withdraw(amt); else System.out.println("operation not allowed"); }; return gateway; } // end makeaccount // another sample closure function, this time typed! static ()->boolean makeflag(boolean value) { ()->boolean f; f = fun ()->boolean { value = !value; return !value; }; return f; } // myflag = makeflag(true); // myflag() == myflag() -> false public static void main(String[] args) { (String,int)->void myaccount; (String,int)->void youraccount; myaccount = makeaccount(200); youraccount = makeaccount(300); myaccount("withdraw",30); youraccount("deposit",40); myaccount("withdraw",10); ()->boolean f1 = makeflag(true); ()->boolean f2 = makeflag(false); // etc... System.out.println(f1() == f1()); } // end main } // end class topping