Questions tagged [anonymous-function]

Anonymous functions use a block of code as a value, defining it as an inline function without a name.

2121 questions
55
votes
14 answers

Choose Python function to call based on a regex

Is it possible to put a function in a data structure, without first giving it a name with def? # This is the behaviour I want. Prints "hi". def myprint(msg): print msg f_list = [ myprint ] f_list[0]('hi') # The word "myprint" is never used…
Tim
  • 13,904
  • 10
  • 69
  • 101
55
votes
4 answers

How to pass two anonymous functions as arguments in CoffeScript?

I want to pass two anonymous functions as arguments for jQuery's hover, like so: $('element').hover( function() { // do stuff on mouseover }, function() { // do stuff on mouseout } ); It's easy with just one – hover -> – but what is…
glortho
  • 13,120
  • 8
  • 49
  • 45
53
votes
4 answers

Python: pass statement in lambda form

A Python newbie question, why is this syntax invalid: lambda: pass, while this: def f(): pass is correct? Thanks for your insight.
Rez
  • 615
  • 1
  • 6
  • 9
47
votes
9 answers

Why use anonymous function?

Possible Duplicate: How do you use anonymous functions in PHP? Why should i use an anonymous function? I mean, what's the real deal using it? I just don't really get this. I mean, you use function to make the code more clean or to use it more…
Shoe
  • 74,840
  • 36
  • 166
  • 272
46
votes
7 answers

Anonymous recursive function in Scala

Is there a way to write an anonymous function that is recursive in Scala? I'm thinking of something like this: ((t: Tree) => { print(t.value); for (c <- t.children) thisMethod(c) })(root) (Related question: Which languages support…
aioobe
  • 413,195
  • 112
  • 811
  • 826
46
votes
6 answers

Why and how do you use anonymous functions in PHP?

Anonymous functions are available from PHP 5.3. Should I use them or avoid them? If so, how? Edited: just found some nice trick with PHP anonymous functions: $container = new DependencyInjectionContainer(); $container->mail =…
Kirzilla
  • 16,368
  • 26
  • 84
  • 129
44
votes
5 answers

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 ,…
Daniel LeCheminant
  • 50,583
  • 16
  • 120
  • 115
42
votes
2 answers

Difference between function with a name and function without name in Javascript

1. function abc(){ alert("named function"); } v/s 2. function(){ alert("Un-Named function"); } Kindly explain from beginners point.
Maxx
  • 709
  • 5
  • 11
  • 22
41
votes
5 answers

Dollar sign before self declaring anonymous function in JavaScript?

What is the difference between these two: $(function () { // do stuff }); AND (function () { // do stuff })();
xil3
  • 16,305
  • 8
  • 63
  • 97
40
votes
3 answers

How can I access local variables from inside a C++11 anonymous function?

I'm doing a simple normalization on a vector (weights), trying to make use of STL algorithms to make the code as clean as possible (I realize this is pretty trivial with for loops): float tot = std::accumulate(weights.begin(), weights.end(),…
bd1
  • 505
  • 1
  • 5
  • 8
39
votes
4 answers

Anonymous functions in WordPress hooks

WordPress hooks can be used in two ways: using callback function name and appropriate function add_action( 'action_name', 'callback_function_name' ); function callback_function_name() { // do something } using anonymous function…
Aleksandr Levashov
  • 403
  • 1
  • 4
  • 7
38
votes
5 answers

Recursion and anonymous functions in elixir

I'm trying to define an anonymous function to do a dot product, I can code this as a private function without any problem but I am struggling with the anonymous function syntax. I know I could implement this differently but I am trying to understand…
Batou99
  • 869
  • 10
  • 19
37
votes
4 answers

this value in JavaScript anonymous function

Can anybody explain to me why A is true and B is false? I would have expected B to be true as well. function MyObject() { }; MyObject.prototype.test = function () { console.log("A", this instanceof MyObject); (function () { …
Corno
  • 5,448
  • 4
  • 25
  • 41
37
votes
4 answers

Can you name the parameters in a Func type?

I have a "dispatch map" defined as such: private Dictionary, string>> _messageProcessing; This allows me to dispatch to different methods easily depending on the name of the…
mavnn
  • 9,101
  • 4
  • 34
  • 52
36
votes
5 answers

What does this "(function(){});", a function inside brackets, mean in javascript?

Possible Duplicates: What does this mean? (function (x,y)){…}){a,b); in JavaScript What do parentheses surrounding a JavaScript object/function/class declaration mean? Hi All I don't know what the following does: (function(){ // Do something…
Shaoz
  • 10,573
  • 26
  • 72
  • 100