anonymous-function

Javascript Anonymous Functions and Global Variables

I thought I would try and be clever and create a Wait function of my own (I realise there are other ways to do this). So I wrote: var interval_id; var countdowntimer = 0; function Wait(wait_interval) { countdowntimer = wait_interval; interval_id = setInterval(function() { --countdowntimer <=0 ? clearInterval(interval_id) : nu...

Want to add a new property to a class, can I use anonymous functions for this?

I have 2 Lists: List<User> List<UserStats> So I want to add a property Count to User (it doesn't have one now, and I can't change the implementation at this point). For a web service call, that returns json, I want to modify the User object. Basically I add the Users to a collection. So I want to add a modified user class (via anon...

Multiple return points in scala closure/anonymous function

As far as I understand it, there is no way in Scala to have multiple return points in an anonymous function, i.e. someList.map((i) => { if (i%2 == 0) return i // the early return allows me to avoid the else clause doMoreStuffAndReturnSomething(i) // thing of this being a few more ifs and returns }) raises an error: return outs...

Arguments to JavaScript Anonymous Function

for (var i = 0; i < somearray.length; i++) { myclass.foo({'arg1':somearray[i][0]}, function() { console.log(somearray[i][0]); }); } How do I pass somearray or one of its indexes into the anonymous function ? somearray is already in the global scope, but I still get somearray[i] is undefined ...

php Set a anonymous function in an instance

I am just starting out with PHP, and I am wondering if there is a way to add an anonymous function to a class instance. For instance, lets say... class A{ public B; } $c = new A(); //This is where I am getting a little confused... //The following wont work $c->B = function(){echo('HelloWorld');}; $c->B(); What I am hoping to d...

Javascript: Why use an anonymous function here?

I was browsing the JIT's code, and I saw this: var isGraph = ($type(json) == 'array'); var ans = new Graph(this.graphOptions); if(!isGraph) //make tree (function (ans, json) { ans.addNode(json); for(var i=0, ch = json.children; i<ch.length; i++) { ans.addAdjacence(json,...

Skipping outputs with anonymous function in MATLAB

Say I want to create an anonymous function from a m-file-function that returns two outputs. Is it possible to set up the anonymous function such that it only returns the second output from the m-file-function? Example: ttest2 returns two outputs, t/f and a probability. If I want to use the t-test with cellfun, I might only be intereste...

JavaScript: Passing changing parameters to a callback

Now here's a fun problem. I have an object array as the following: objRequests = [ { url: "/cgi-bin/script1.cgi", dest: "#div1" }, { url: "/cgi-bin/script1.cgi", dest: "#div2" } ]; Now, I iterate through these objects to load some information from the server at particular addresses using jQuery's $....

SortedSet<T> and anonymous IComparer<T> in the constructor is not working

How come anonymous functions works as arguments on methods, but not in constructor arguments? If I create a List<string>, it has a Sort method with the following signature: public void Sort(IComparer<T> comparer) where the following works: List<string> list = new List<string>(); list.Sort( (a,b) => a.CompareTo(b) ); SortedSet ha...

Function calling itself not working (infinite loop, Javascript)

I'm trying to wait and then get a message when all images in an array have completed loading (using .complete), per the answer here. As such I set up an infinite loop like the below. however, when I run this I get an error that checkForAllImagesLoaded() is not defined. This code is being run through a bookmarklet, and as such it's all ...

What is the type of a lambda function?

In C++0x, I'm wondering what the type is of a lambda function. Specifically: #include<iostream> type1 foo(int x){ return [x](int y)->int{return x * y;}; } int main(){ std::cout<<foo(3)(4);//would output 12 type2 bar = foo(5); std::cout<<bar(6);//would output 30 return 0; } What do I need to replace type1/type2 with to get the...

javascript anonymous function scope

I have the following anonymous function: (function() { var a = 1; var b = 2; function f1() { } function f2() { } // this => window object! // externalFunction(this); })(); function externalFunction(pointer) { // pointer.f1(); => fail! } I need to call external function from this anonymous function and pass it's pointer to...

Scala: How do I define an anonymous function with a variable argument list?

In Scala, how do I define an anonymous function which takes a variable number of arguments? scala> def foo = (blah:Int*) => 3 <console>:1: error: ')' expected but identifier found. def foo = (blah:Int*) => 3 ^ ...

PHP Anonymous function with array_walk

Hello, I'm trying to use array_walk with an anonymous function, but I always get the error // Parse error: syntax error, unexpected T_FUNCTION in ... on line X if(!empty($myArray)) { array_walk($myArray, function(&$value, $key){ // Line X $value = '"'.$value.'"'; // Add quotes }); } The surrounding file syntax is corre...

How do I make this anonymous Javascript function reference the right variable?

Example: var o = {}; for(var i = 0; i < 5; i++) { o[i] = function () { console.log(i); }; } o[3](); When I call o3, it will always display 5 on the console, even if I call o0, o4, or any one of those. It will always display 5 because that's the last value i had. How do I make it display the value of i when the anonymous fun...

Transform an array into a string inline

What is the tersest way to transform an integer array into a string enumerating the elements inline? I'm thinking something like an anonymous function that performs the conversion. var array = new int[] { 1, 2 } var s = string.Format("{0}", new [] { /*inline transform array into the string "...

Erlang - Anonymouos Function

If i call the test(), it doesnt work. Can someone explain this ?. -module(anony). -export([test/0, test1/0]). test1() -> "hello". test() -> C = fun(F) -> Val = F(), io:format("~p ", [Val]) end, lists:foreach(debug, [test1]). ...

setInterval / clearInterval Issue inside it's scope

I have the following code, and it works fine until i hit the the #play button. I'm assuming it's because the var intID is set in another place and it's not in the same scope when i window.clearInterval() it... how do I fix this? BTW, this is the Google Maps API Version 3 function intervalTrigger(){ return window.setInterval(fu...

Modern C#: how to make a function private to a method

I'm working on a method that needs to repeat a small operation at different spots, but the code to be repeated should be private to the method. The obvious solution is a nested function. Whatever I try however, the C# compiler barfs at me. Something roughly equal to this Perl snippet: my $method = sub { $helper_func = sub { code to...

Location of parenthesis for auto-executing anonymous JavaScript functions?

I was recently comparing the current version of json2.js with the version I had in my project and noticed a difference in how the function expression was created and self executed. The code used to wrap an anonymous function in parenthesis and then execute it, (function () { // code here })(); but now it wraps the auto-executed fun...