closures

F# Closure

Anyone have a decent example, preferably practical/useful, they could post demonstrating the concept? ...

Can you explain closures (as they relate to Python)?

I've been reading a lot about closures and I think I understand them, but without clouding the picture for myself and others, I was wondering if anyone can explain closures as succinctly and clearly as possible to me and others? I'm looking for a simple explanation that might help me wrap my head around why I should be using them in cer...

Strange boo language syntax

I've run into a strange syntax in Boo Language Guide : setter = { value | a = value } What does the | operator mean? ...

Does Java need closures?

I've been reading a lot lately about the next release of Java possibly supporting closures. I feel like I have a pretty firm grasp on what closures are, but I can't think of a solid example of how they would make an Object-Oriented language "better". Can anyone give me a specific use-case where a closure would be needed (or even prefer...

How does a javascript closure work ?

Like the old Albert said : "If you can't explain it to a six-year old, you really don't understand it yourself.”. Well I tried to explain JS closures to a 27 years old friend and completely failed. Can anybody consider than I am 6 and strangely interested in that subject ? EDIT : I have seen the scheme example given in SO, and it did n...

What’s the current state of closures in Java?

Does anybody know, if closures will be in Java 7? ...

What's the nearest substitute for a function pointer in Java?

I have a method that's about 10 lines of code. I want to create more methods that do the exact same thing except for a calculation that's going to change one line of code. This is a perfect application for passing in a function pointer to replace that one line, but Java doesn't have function pointers. What's my best alternative? ...

How do I write a generic memoize function?

I'm writing a function to find triangle numbers and the natural way to write it is recursively: function triangle (x) if x == 0 then return 0 end return x+triangle(x-1) end But attempting to calculate the first 100,000 triangle numbers fails with a stack overflow after a while. This is an ideal function to memoize, but I want a...

Is it possible to define a Ruby singleton method using a block?

So, I want to define a singleton method for an object, but I want to do it using a closure. For example, def define_say(obj, msg) def obj.say puts msg end end o = Object.new define_say o, "hello world!" o.say This doesn't work because defining a singleton method via "def" is not a closure, so I get an exception that "msg" is...

Why results of map() and list comprehension are different?

The following test fails: #!/usr/bin/env python def f(*args): """ >>> t = 1, -1 >>> f(*map(lambda i: lambda: i, t)) [1, -1] >>> f(*(lambda: i for i in t)) # -> [-1, -1] [1, -1] >>> f(*[lambda: i for i in t]) # -> [-1, -1] [1, -1] """ alist = [a() for a in args] print(alist) if __name__ == '__...

What limitations have closures in Python compared to language X closures?

Where X is any programming language (C#, Javascript, Lisp, Perl, Ruby, Scheme, etc) which supports some flavour of closures. Some limitations are mentioned in the Closures in Python (compared to Ruby's closures), but the article is old and many limitations do not exist in modern Python any more. Seeing a code example for a concrete l...

Closures in PHP... what, precisely, are they and when would you need to use them?

So I'm programming along in a nice, up to date, object oriented fashion. I regularly make use of the various aspects of OO programming that PHP implements but I am wondering when might I need to use closures. Any experts out there that can shed some light on when it would be useful to implement closures? ...

Local variables with Delegates

This is clearly not appears like it wouldn't be a best practice, but can someone explain why or how this works. Or recommend a good book to learn more. //The constructor public Page_Index() { //create a local value string currentValue = "This is the FIRST value"; //use the local variable in a delegate that fires later ...

How do I use BGGA closures prototype on standard Mac JDK6?

I am trying to use the BGGA closures prototype with an existing JDK 6 (standard on Mac OS X Leopard). The sample code I'm compiling is from a BGGA tutorial: public static void main(String[] args) { // function with no arguments; return value is always 42 int answer = { => 42 }.invoke(); System.out.println(answer); } I have trie...

ActionScript 3.0 using closures for event handlers

I tried doing this: root.addEventListener("click", function () { navigateToURL(ClickURLRequest,"_self"); }); And it does add the event listener. I like using closures because they work well in this situation, however, removing the event listener requires a reference to the original function, and since I used an ano...

Can you explain lambda expressions?

I don't really get lambda expressions. While they've been around since the days of ALGOL, I didn't start hearing about them until fairly recently, when Python and Ruby became very popular. Now that C# has the => syntax, people in my world (.NET) are talking about lamdba expressions more and more. I've read the Wikipedia article on the ...

BGGA closures as a bolt-on solution to java?

Yesterday @headius / Charles Nutter came up with a very interesting idea on twitter: @danny_l Gafter made the same mistake; I don't mean a forked Java any more than Groovy is a fork. I want a "mostly Java" with closures. or the reply by @danny_l / Danny Lagrouw: @headius or could the BGGA prototype be "bolted on" any future version o...

Best pattern for simulating "continue" in Groovy closure

It seems that Groovy does not support break and continue from within a closure. What is the best way to simulate this? revs.eachLine { line -> if (line ==~ /-{28}/) { // continue to next line... } } ...

Function pointers, Closures, and Lambda

I am just now learning about function pointers and as I was readying the K&R chapter on the subject the first thing that hit me was, "Hey, this is kinda like a closure." I knew this assumption is fundamentally wrong somehow and after a search online wasn't really to find any analysis of this comparison. So why are C style function point...

JavaScript - How do I learn about "closures" usage?

From Wikipedia, the free encyclopedia: Closure (computer science) In computer science, a closure is a function that is evaluated in an environment containing one or more bound variables. When called, the function can access these variables. The explicit use of closures is associated with functional programming and with ...