anonymous-function

Which languages support *recursive* function literals / anonymous functions?

It seems quite a few mainstream languages support function literals these days. They are also called anonymous functions, but I don't care if they have a name. The important thing is that a function literal is an expression which yields a function which hasn't already been defined elsewhere, so for example in C, &printf doesn't count. E...

How do you explain this structure in JavaScript?

(function() { //codehere } )(); What is special about this kind of syntax? What does ()(); imply? ...

Firefox Javascript Events Anonymous Function

I'm attempting to register an anonymous function when a user clicks a cell in an HTML table. Here's some of the raw, unadulterated code: document.getElementById( "course"+displayed_year_index+occurrences_indices[displayed_year_index]).onclick = eval("function() {PrintReceipt("+result.years[result_year_index].rul_code+");};"...

Is there syntactic sugar for binding a value inside an anonymous function in Scala?

Instead of writing ((x: Double) => (((y: Double) => y*y))(x+x))(3) I would like to write something like ((x: Double) => let y=x+x in y*y)(3) Is there anything like this sort of syntactic sugar in Scala? ...

Firefox: pass event from anonymous javascript function

In this abbreviated code, the inline event works - the "event" is passed to the testKeyPress function <textarea id="source" onkeydown= "showCursPos(this); var tf=testKeyPress(event); document.onkeypress=function(){return tf}; document.onkeydown=function(){return tf}; " ></textarea> function testKeyPress(e){ if (e...

How can I pass a reference to a function, with parameters?

I need to able to pass a reference to a function with a given set of parameters. Here is an example of passing a reference without parameters: var f = function () { //Some logic here... }; var fr = f; //Here I am passing a reference to function 'f', without parameters fr(); //the 'f' function is invoked, without parameters Now w...

Pass anonymous function by value in javascript?

I have a function that accepts an anonymous function as an argument and sets it to a variable (scoped) for reference. I then try to execute another function with that reference but it obviously fails since that function is out of scope. I was wondering if anyone knew of a simple way of passing the anonymous function straight through as...

How to execute multiple statements in a Matlab anonymous function?

I'd like to do something like this: >> foo = @() functionCall1() functionCall2() so that when I said >> foo() it would execute functionCall1() and then execute functionCall2(). (I feel that I need something like the C , operator) EDIT: functionCall1 and functionCall2 are not necessarily functions that return values. ...

arbitrary number of anonymous Loader handers with individual data ?

I have an arbitrary number of files that I need to load in an AIR app. I want to iterate through an array of File object and create and launch Loaders for each one's File.url. When they are done (event COMPLETED or IOErrorEvent.IO_ERROR), I want to stuff their data somewhere. If they fail, I want to make an exception report. I cannot f...

anonymous functions in unity via config file

I've been using unity with interfaces, but sometimes created an interface only to use a single method in a single class; and used unity as my IoC framework. But then I saw this post: http://codebetter.com/blogs/karlseguin/archive/2009/05/08/making-the-untestable-testable-with-anonymous-methods-and-dependency-injection.aspx which made m...

Pass FieldName as a Parameter in C#

I'm not quite sure what I'm attempting to do is called, so I'm struggling to find any clues from google. I have a couple of methods with the same logic in them, the only thing that differs is the property they are using on an object. class Foo { public int A(Bar bar) { return bar.A * 2; } public int B(Bar bar) { ...

advanced parameter usage

//This is the function that will run every time a new item is added or the //list is sorted. var showNewOrder = function() { //This function means we get serialize() to tell us the text of each //element, instead of its ID, which is the default return. var serializeFunction = function(el) { return el.get('text'); }; //W...

AS3: in addEventListener external anonymous function is not running

Basically I have this function: private function clickURL(url:String):Function{ trace("Function has been instantiated"); return function(event:MouseEvent):void{ trace("Function has been run"); ExternalNavigation.newBrowserWindow(url); } } The purpose of this function is to take an url, put the url in another fun...

anonymous function usefulness

Why not write the anonymous function content only instead of the anonymous function AND the anonymous function content? ...

When is an anonymous function executed? It doesn't have a name to call!

I know it's not executed immediatly, but then when? ...

How do you know you have to use function() {}?

code 1 $(document).ready(function() { $('.poem-stanza').addClass('highlight'); }); code 2 $(document).ready( $('.poem-stanza').addClass('highlight'); ); ...

How does an anonymous function in JavaScript work?

Hello JS experts! I'm reading some posts about closures and see this stuff all over the places, but there is no explanation how does it works - just every time I'm told to use it...: // Create a new anonymous function, to use as a wrapper (function(){ // The variable that would, normally, be global var msg = "Thanks for visitin...

How to pass a variable by value to an anonymous javascript function?

The Objective I want to dynamically assign event handlers to some divs on pages throughout a site. My Method Im using jQuery to bind anonymous functions as handlers for selected div events. The Problem The code iterates an array of div names and associated urls. The div name is used to set the binding target i.e. attach this event ...

JavaScript-like anonymous functions in C#

Can the following be done in C#?: var greeting = "Hello" + function () { return " World"; }() + "!"; I want to do something along the lines of this (C# pseudo code): var cell = new TableCell { CssClass = "", Text = return delegate () { return "logic goes here"; }}; Basically I want to implement in-line scoping of some logi...

C# Anonymous function-scope

var foo = "bar"; new Func<String>(() => { var foo = ""; // This can't be done in C#. Why is that? /* In JavaScript, this is perfectly valid, since this scope (the anonymous function) is disconnected from the outer scope, and any variable declared within this scope will not affect variables in the outer scope */ ...