closures

Stored Block Closure as IBAction

I am trying to reduce ceremony and out of academic curiosity I want to know how to do the following without the IBAction method defined in the .m file to use a closure whenever an Interface Builder wired action occurs such as a button press. You could say that I want to imply the cancelButtonPress method below instead of having to defin...

Tear down a closure in JavaScript

is there a way to tear down a closure in JavaScript to determine what the function is and what the scope is? Or, maybe more succinctly, is there a way to serialize a closure in JavaScript? edit What I am wondering if I am given a function declared as follows: var o = {}; var f = function() { return o; } Is there a way to look at ju...

Ruby regex closures always return false

Im setting up a simple checker program thing. For now, its extremely simple and is jsut checking integers. I dont think the regular expressions are wrong, but I am new to ruby. testing out the auto-regex def createRegexClosure(re) reg = Regexp.new(re) return lambda { |t| return reg.match(t) } end pr...

Java oneliner for list cleanup

Is there a construct in java that does something like this(here implemented in python): [] = [item for item in oldList if item.getInt() > 5] Today I'm using something like: ItemType newList = new ArrayList(); for( ItemType item : oldList ) { if( item.getInt > 5) { newList.add(item); } } And to me the first way looks a ...

bind event on generated element and get its current state.

Here is a plug-in that binds a click event on an element, which generates a container with a <textarea> and then i bind a keypress event on this new <textarea>, all is ok, but inside the keypress handler i can only get the state of text().length from when the initial click happened. How do i get the onKeyPress function to get the state...

Stop Timer in Closures Using F#

I want to ask a simple question how to stop this timer. Somebody's code is like this : let mutable timer = new DispatcherTimer() timer.Interval - new TimeSpan(0,0,0,0,100) timer.Start() and I want to add a function that cause the timer to stop whenever I click button So I put a code like this (I don't really have a clue) ...

F# closure: Map reference cell is always empty.

Given the following code: static member private getIntValue (_map:Map<string, int>) (_key:string) = if (_map.ContainsKey _key) then _map.[_key], _map else let i = doSomething () i, if i > 0 then _map.Add (_key, i) else _map static member private getDataFn<'T> (_getFn:Map<string, 'T> -> string -> 'T * Map...

What is a closure? Does java have closures?

I was reading Object Oriented Javascript and found the concept of closures. I didn't quite understand why and when it is used. Do other languages like Java also have closures? I basically want to understand how knowing the concept of closures can help me improve my coding. ...

Return from closure?

How does one return from a closure, without returning from the containing function? In the following function, the return statement actually returns from GM_xmlhttpRequest: not the closure. Naturally I can see that I could arrange my code so that that execution drops off the end of the closure, but I'm curious as to how to early return ...

Can I add a method to a closure

This is sort of what closures are meant to avoid, but I'm wondering if there's a way to a add a method to a closure. Basically I have a js file library that I'd like to augment for a specific client by adding a new method. I have a js file called library: var LIBRARY = (function(){ var name; return { setName: function(n) { name = n; }...

What's the opposite of the term "closed over"?

Consider the following (C#) code. The lambda being passed to ConvolutedRand() is said to be "closed over" the variable named format. What term would you use to describe how the variable random is used within MyMethod()? void MyMethod { int random; string format = "The number {0} inside the lambda scope"; ConvolutedRand(x =>...

Scala advantages after Java having closures

With closures being added to Java, what is Scala's advantage over Java as a language choice? Can someone elaborate on any advantages? ...

How can I serialize a closure in Perl?

I think this might be best asked using an example: use strict; use warnings; use 5.010; use Storable qw(nstore retrieve); local $Storable::Deparse = 1; local $Storable::Eval = 1; sub sub_generator { my ($x) = @_; return sub { my ($y) = @_; return $x + $y; }; } my $sub = sub_generator(1000); say $sub->(...

Scala: Rewrite code duplication with closures

Hi, I have this code: val arr: Array[Int] = ... val largestIndex = { var i = arr.length-2 while (arr(i) > arr(i+1)) i -= 1 i } val smallestIndex = { var k = arr.length-1 while (arr(largestIndex) > arr(k)) k -= 1 k } But there is to much code duplication. I tried to rewrite this with closures but I failed. I tried somethi...

Abuse of Closures? Violations of various principles? Or ok?

Edit: fixed several syntax and consistency issues to make the code a little more apparent and close to what I actually am doing. I've got some code that looks like this: SomeClass someClass; var finalResult = DoSomething(() => { var result = SomeThingHappensHere(); someClass = result.Data; return result; }) .DoSom...

Self Executing functions in PHP5.3?

I was trying to borrow some programing paradigms from JS to PHP (just for fun). Is there a way of doing: $a = (function(){ return 'a'; })(); I was thinking that with the combination of use this can be a nice way to hide variables JS style $a = (function(){ $hidden = 'a'; return function($new) use (&$hidden){ $hidden...

Determine value of encapsulated javascript function parameters

Alright, to start with let's look at some code: <html> <body> <script type="text/javascript"> function logFunction(fn) { console.log(fn.prototype); console.log(fn); console.log(fn(4)); } var num = 5; var add5 = function(x) { retur...

How can I find caller from inner function ?

<input type='button' id='btn' value='click' /> <script type="text/javascript"> var jObject = { bind : function(){ var o = document.getElementById('btn'); o.onclick = function(){ // How can I find caller from here ? } } }; jObject.bind(); </script> UPDATE I read some trick from here - http://www.mennovanslooten.nl/...

"this" keyword inside closure

I've seen a bunch of examples but can't seem to get some sample code to work. Take the following code: var test = (function(){ var t = "test"; return { alertT: function(){ alert(t); } } }()); and I have a function on window.load like: test.alertT(); That all works fine. However, when I try...

Recommended Solutions for Creating a Callback that Chains Multiple Methods and/or Properties ?

We are working on a Builder type interface that basically constructs a list for doing CRUD management of individual objects (since we're using ActiveRecord an object == a database record). In order to make specifying the column values and parameters for list options flexible we originally implemented the callback arguments as an array t...