anonymous-function

Reference Instance Variables in Javascript Constructor

I'm trying to maintain state on an object by doing something like this: obj = function() { this.foo = undefined; this.changeState = function () { (function () { this.foo = "bar" })(); // This is contrived, but same idea. }; }; I want to set the instance variable foo to "bar" when I call the changeState method. ...

Having problems with anonymous functions in JavaScript.

jQuery.fn.testeee = function(_msg) { alert(_msg); $(this[0]).overlay({ onBeforeLoad: function() { alert(_msg); } }).load(); }; $("#popup").testeee ('test'); $("#popup").testeee ('another_test'); This displays: test test another_test test The alert() inside de anonymous function asigned to onBeforeLoad keeps showing "t...

How different programming languages use closures?

To my knowledge, combined with the knowledge of others, among the mainstream languages Objective C C# VB.net Java Python Ruby Javascript Lisp Perl have closures and anonymous functions. Plain C/C++ doesn't have either of those. Do closures in these languages have the same semantics? How important are they for everyday programming? ...

Anonymous methods/functions: a fundamental feature or a violation of OO principles?

Is the recent movement towards anonymous methods/functions by mainstream languages like perl and C# something important, or a weird feature that violates OO principles? Are recent libraries like the most recent version of Intel's Thread Building Blocks and Microsofts PPL and Linq that depend on such things a good thing, or not? Are lan...

How to get the "this" (scope) of a Javascript anonymous function?

Let's say I get an anonymous function an need to act on its context, but it's different whether it's binded to "window" or an unknown object. How do I get a reference to the object an anonymous function is called from? EDIT, some code : var ObjectFromOtherLibIAmNotSupposedToknowAbout = { foo : function() { // do something ...

scala anonymous function syntax

I'm learning more about Scala and I'm having a little trouble understanding the example of anonymous functions here http://www.scala-lang.org/node/135. I've copied the entire code block below: object CurryTest extends Application { def filter(xs: List[Int], p: Int => Boolean): List[Int] = if (xs.isEmpty) xs else if (p(xs.head)...

Variables in Anonymous Functions -- Can someone explain the following?

I've been trying to assign a function to onclick event of a dynamically created "a" tag in JavaScript. All of the tags are created in a loop as follows: for ( var i = 0; i < 4; i++ ) { var a = document.createElement( "a" ); a.onclick = function( ) { alert( i ) }; document.getElementById( "foo" ).appendChild( a ); } The alerted v...

Best way to run a simple function on a new Thread?

I have two functions that I want to run on different threads (because they're database stuff, and they're not needed immediately). The functions are: getTenantReciept_UnitTableAdapter1.Fill(rentalEaseDataSet1.GetTenantReciept_Unit); getTenantReciept_TenantNameTableAdapter1.Fill(rentalEaseDataSet1.GetTenantReciep...

Scala: How to "store" a function in a var?

I'm learning Scala and I'm trying to store a function in a var to evaluate it later: var action:() => Any = () => {} def setAction(act: => Any) { action = act } but that doesn't compile: error: type mismatch; found: Any required: () => Any action = act So it seems to me that in action = act instead of assigning the fu...

Every element in an array being incremented simultaneously when only one should be.

I am using the following code to increment the elements in a 2d array surrounding a given element. EmptyCell = {number: 0}; //This has several parts in the actual code. list = new Array(); function init(w,h){ for (var x = 0; x <= w; x++){ list[x] = new Array(); for (var y = 0 ; y <= h; y++){ list[x][y]...

MATLAB functions refusing to function depending on placement

I've written a very simple GUI in MATLAB that will convert temperatures. It is meant to serve as a tutorial for a class of students. A strange thing has happened though. As with any MVC design pattern, there is a model object, a view object and a controller function. In order to set the output field of the GUI (the converted temperat...

Explain JavaScript's encapsulated anonymous function syntax

Summary Can you explain the reasoning behind the syntax for encapsulated anonymous functions in JavaScript? Why does this work: (function(){})(); but this doesn't: function(){}();? What I know In JavaScript, one creates a named function like this: function twoPlusTwo(){ alert(2 + 2); } twoPlusTwo(); You can also create an ano...

Fake anonymous functions in C

In this SO thread, Brian Postow suggested a solution involving fake anonymous functions: make a comp(L) function that returns the version of comp for arrays of length L... that way L becomes a parameter, not a global How do I implement such a function? ...

Any way to avoid using anonymous functions in jQuery?

If I have a chunk of code like this: .hover( function () { hoverState($("#navbar a").index(this),1); }, function () { hoverState($("#navbar a").index(this),-1); }); Is there any way to get rid of the anonymous functions and just say: .hover( hoverState($("#navbar a").index(this),1), hoverState($("#n...

Slow performance using anonymous functions in MATLAB... have others noticed this?

In order to refactor my MATLAB code, I thought I'd pass around functions as arguments (what MATLAB calls anonymous functions), inspired by functional programming. However, it seems performance is hit quite severely. In the examples below, I compare different approaches. (The code snippet is wrapped in a function in order to be able to u...

Javascript variable scope in addEventListener anonymous function

When clicking on each div it should alert '1' if div 1 was clicked on or '5' if div 2 was clicked on. I have tried to make this code as easy to as possible because this is needed in a much larger application. <html> <head> <style type="text/css"> #div1 { background-color: #00ff00; margin: 10px; padding: 10px; } #div2 { background-color:...

How can I approximate Python's or operator for set comparison in Scala?

After hearing the latest Stack Overflow podcast, Peter Norvig's compact Python spell-checker intrigued me, so I decided to implement it in Scala if I could express it well in the functional Scala idiom, and also to see how many lines of code it would take. Here's the whole problem. (Let's not compare lines of code yet.) (Two notes: You...

How To Access jQuery Event Without Using Anonymous Callback Parameter

Hello all, Typically, when needing to access an event, you do so via the parameter specified in the callback function: $button.live("click", function(ev) { // do something with ev here, like check 'ev.target' } But instead (for reasons too complicated to get into here), I do not want to use an anonymous callback function, but inste...

Can I use a block when defining a Scala anonymous function?

Let's say I have this method: def myMethod(value:File,x: (a:File) => Unit) = { // some processing here // more processing x(value) } I know I can call this as: myMethod(new File("c:/"),(x:File) => println(x)) Is there a way I could call it using braces? Something like: myMethod(new File("c:/"),{ (x:File) => if(x.toSt...

How do I make an "empty" anonymous function in MATLAB?

I use anonymous functions for diagnostic printing when debugging in MATLAB. E.g., debug_disp = @(str) disp(str); debug_disp('Something is up.') ... debug_disp = @(str) disp([]); % diagnostics are now hidden Using disp([]) as a "gobble" seems a bit dirty to me; is there a better option? The obvious (?) method doesn't work: debug_disp ...