'&$%CSE 341:Programming LanguagesWinter 2005 Lecture 28— Closures in Java; What We Didn’t Do;Wrap-UpCSE341 Winter 2005, Lecture 28 1'&$%Goals for today• Anonymous inner classes in Java• Give a flavor of big areas of PL we didn’t even get to• Put in context what we did get toCSE341 Winter 2005, Lecture 28 2'&$%Anonymous inner class exampleclass NPresses {int objMax;NPresses(int n) { objMax = n; }void addToButton(JButton b, final int buttonMax) {b.addActionListener(new ActionListener() { // ActionListener a library classint m = 0;public void actionPerformed(ActionEvent e) {++m;if(m==objMax)System.out.println("enough presses (obj)!");else if(m==buttonMax)System.out.println("enough presses (button)!");}});}}CSE341 Winter 2005, Lecture 28 3'&$%Just sugar?Did we ”nee d” an anonymous class?• No: could use an inner class• No: could use a top-level class and share an object holdingobjMax and a second field for buttonMaxCSE341 Winter 2005, Lecture 28 4'&$%Higher-order functions in Java?• Anonymous inner classes are a convenienc e for makinghigher-order functions less burdensome.• Regardless, OO and downcasts let you manually create closures(not shown).• C# has delegates, which are even closer to first-class functions.CSE341 Winter 2005, Lecture 28 5'&$%What else?Are all programming languages imperative, OO, or FP? No.• Logic languages (e.g., Prolog)• Scripting languages (Perl, Python, Ruby)• Query languages (SQL)• Purely functional languages (no ref or set!)• Visual languages, spreadsheet languages, GUI-builders,text-formatters, hardware-synthesis, ...CSE341 Winter 2005, Lecture 28 6'&$%Prolog in one exampleappend(cons(Hd,Tl), Lst2, cons(Hd,Tl2)) :=append(Tl, Lst2, Tl2).append(nil, Lst2, Lst2).append(cons(1, cons(2, nil)), cons(3, cons(4, nil)), X)% X = cons(1,cons(2,cons(3,cons(4,nil))))append(cons(1, nil), cons(2,nil), cons(1, cons(2, nil)))% yesappend(nil, cons(2,nil), cons(1, cons(2, nil)))% noappend(cons(Hd,nil), Y, cons(1, cons(2, cons(3, nil))) )% Hd = 1 Y = cons(2,cons(3,nil))CSE341 Winter 2005, Lecture 28 7'&$%Prolog key ideas• A program is a set of declarative proof rules.• Operationally, it’s like a function that doesn’t distinguish inputsfrom outputs.• The implementation searches for the minimal constraintsnecessary for a formula to be true.• Different “queries” can run “forward” or “backward”• This is Turing-complete; killer app is inherently search-orientedtasks, which are comm on in AI.CSE341 Winter 2005, Lecture 28 8'&$%Scripting LanguagesFew “new” language constructs, but convenience for som equick-and-dirty programs.• File-system access very lightweight• Lots of support for s tring-processing via regular expressions (adifferent “pattern-matching”)• Dynamically typed with implicit coercions (such as int to string)• Tend to have very few “errors” (array resizing, implicit variabledeclaration, etc.)Opinion:• A fine tool for small tasks• They tend to hide bugs rather than prevent them• But you should learn to automate repetitive tasks!CSE341 Winter 2005, Lecture 28 9'&$%Query LanguagesCanonical example: Suppose there’s a big database and many peopleneed data from it. We c ould make lots of copies or let people submitqueries.Key idea: Move the code to the data, not the data to the code.Interestingly: We do not necessarily want the query language to be aspowerful as a Turing-machine!SQL was carefully designed so every query terminates.CSE341 Winter 2005, Lecture 28 10'&$%Purely Functional LanguagesMutation seemed necessary in ML and Scheme for building datastructures with cycles. It’s not:• You can build equivalent structures without cycles.• You can build cycles by cleverly applying functions to themselves• In fact, you can build recursion the same way(recall ((lambda (x) x x) (lambda (x) x x))).• In fact, this subset of Scheme is Turing-complete:e ::= x | (lambda (x) e) | (e1 e2)This language is “impractical” but it’s an im portant fact. For example,SQL can’t include these features.CSE341 Winter 2005, Lecture 28 11'&$%Real Purely Functional LanguagesExample: HaskellTo make life without refs palatable, the default is “lazy” (call-by-need)evaluation.One-line example: let ones = 1::onesLaziness can lead to elegant programming and really increases thenumber of equivalent programs. In Haskell, (f x) + (f x) and(f x) * 2 are contextually equivalent, always.• Haskell does have monads, which allow a more imperative style.• The implementation of laziness uses mutation, but in a controlledway (we did this in Scheme).CSE341 Winter 2005, Lecture 28 12'&$%Each-of types and operations• Given a type with several variants/subtypes and severalfunctions/methods, there’s an obvious 2D-grid of code you need:Int Negate Add MultevaltoStringhasZero• OO and FP lay out the code differently.• Which is more convenient depends on what you’re doing and howthe variants/operations “fit together”• Often, tools let you view “the other dimension”• Opinion: Dimensional structure of code is greater than we have ona computer, so we ’ll always have limits in text-based languages.CSE341 Winter 2005, Lecture 28 13'&$%ExtensibilityLife gets interesting if need to extend code w/o changing existing code.• ML makes it e asy to w rite new operations; Java does not.• Java makes it e asy to w rite new variants; ML does not.• In ML the original code must plan for extensibility:– For operations, use polymorphism and function arguments. Forexample, use folds or e ven abstract the c onstructors.– For types, can use polymorphism, but the lack of subtypingmakes it awkward to use the extended types.• In Java the extended code can suffer from downcasts...– For types, use interfaces and have new classes implement them.– For operations, must downcast to know the operations exist.• ... or plan for extensibility (e.g., the “visitor pattern”)CSE341 Winter 2005, Lecture 28 14'&$%UnextensibilityExtensibility is not all it’s c racked up to be:• Makes original code more difficult to c hange later.• Makes code harder to reason about locally (e.g., dynamic dispatchor functions-as-arguments mean you never know what code mightexecute next)CSE341 Winter 2005, Lecture 28 15'&$%Ignored Language Features• Threads (potential safety problem: race conditions)• Interoperability (component / software-architecture languages,foreign-function interfaces, more “open” garbage
View Full Document