Re: Closures: summarizing suggestions a bit...
Victor Volle <[email protected]> Sat, 5 Feb 2005 20:30:24 +0100
| Newsgroups | gmane.comp.java.beanshell.devel |
|---|---|
| Message-ID | <[email protected]> |
Hi Slava, Hi all, > --- Slava Pestov <[email protected]> wrote: > > Hi all, > > I suggest a syntax for closures, somewhat inspired by Common Lisp, where > refer to a function object, either named or anonymous, you write #'foo, > where foo is either a function name, or an anonymous function block > ("lambda"). > > While #' is a bit ackward, in BeanShell you can reserve the > currently-unused @ character to look up inside the function namespace -- > giving the ability to have function object literals. Using @ in a C like > language is not that weird, and Objective C uses this character. > > So, suppose you had a function map() that took a list and a function, > and returned a new list. You could do: > > sq(x) { return x*x; } > > map(list,@sq); > > For anonymous closures, just use a syntax like > > map(list,@(x) { return x*x; }); > > So its @(arguments) { body } > > (in Common Lisp, its #'(lambda (x y) (+ x y)) -- notice the parallel...). > > Now, I missed method literals. What you could do is return an object > that is internally a pair; a 'this', and a BeanShell method to invoke. > So the syntax could be like: > > map(list,myObj.@foo); > > Where myObj.@foo is an object that when called in some way, invokes > myObj.foo(...). > -- 1 -- Is "myObj" the placeholder for an element of the list? Or is the list element inserted as parameter to the call of foo? If the list element is "$_" (like Perl gibberish) the two alternatives could be expressed like: map(list, $_.@foo()); and map(list, myObj.@foo($_)); -- 2 -- What I yet do not understand: How I would implement the map method as instance method of the list. The call would be something like: list.map(myObj@foo); or list.map(@(x) { return x*x; }); But the class? public class List { ... public List map( Closure closure ) { } } Is that it? Victor ------------------------------------------------------- This SF.Net email is sponsored by: IntelliVIEW -- Interactive Reporting Tool for open source databases. Create drag-&-drop reports. Save time by over 75%! Publish reports on the web. Export to DOC, XLS, RTF, etc. Download a FREE copy at http://www.intelliview.com/go/osdn_nl