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
36
votes
4 answers

Difference between expression lambda and statement lambda

Is there a difference between expression lambda and statement lambda? If so, what is the difference? Found this question in the below link but could not understand the answer What is Expression Lambda? C# interview Questions The answer mentioned in…
ckv
  • 10,539
  • 20
  • 100
  • 144
35
votes
9 answers

Can I store RegExp and Function in JSON?

Given a block like this: var foo = { "regexp": /^http:\/\//, "fun": function() {}, } What is a proper way to store it in JSON?
Huang
  • 1,919
  • 3
  • 15
  • 11
35
votes
2 answers

How to document anonymous functions (closure) with jsdoc-toolkit

I am trying to document my code using JSDoc-toolkit. My code starts by being wrapped with a self-executing anonymous function. How in the world do I document this? I've spent nearly all day on this. JS Docs will not recognize anything inside of the…
Jesse Atkinson
  • 10,586
  • 13
  • 42
  • 45
35
votes
4 answers

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); …
Malfist
  • 31,179
  • 61
  • 182
  • 269
35
votes
2 answers

Return value from anonymous function postgresql

How to? For easy example. I have a simple function: DO LANGUAGE plpgsql $$ DECLARE BEGIN EXECUTE 'SELECT NOW()'; END $$; How I can return value of "NOW()" or other values from also anonymous function? The function is given as an example I have a…
arturgspb
  • 1,004
  • 1
  • 12
  • 19
34
votes
3 answers

Are there any drawbacks to using anonymous functions in JavaScript? E.g. memory use?

At some point in the past, I read something that gave me the idea that anonymous functions in JavaScript can use up a surprising amount of memory (because they carry the entire current scope around with them), whereas named (static?) functions don’t…
Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
33
votes
2 answers

Scala return statements in anonymous functions

Why does an explicit return statement (one that uses the return keyword) in an anonymous function return from the enclosing named function, and not just from the anonymous function itself? E.g. the following program results in a type error: def foo:…
amaurremi
  • 777
  • 1
  • 5
  • 11
30
votes
2 answers

PHP use() function for scope?

I have seen code like this: function($cfg) use ($connections) {} but php.net doesn't seem to mention that function. I'm guessing it's related to scope, but how?
kristian nissen
  • 2,809
  • 5
  • 44
  • 68
30
votes
5 answers

Reason behind this self invoking anonymous function variant

While looking at code on github, I found the following: (function() { }).call(this); This is clearly a self invoking anonymous function. But why is it written this way? I'm used to seeing the canonical variant (function() {})(). Is there any…
Sean McMillan
  • 10,058
  • 6
  • 55
  • 65
30
votes
6 answers

Equivalent of C# anonymous methods in Java?

In C# you can define delegates anonymously (even though they are nothing more than syntactic sugar). For example, I can do this: public string DoSomething(Func someDelegate) { // Do something involving someDelegate(string s) }…
Callum Rogers
  • 15,630
  • 17
  • 67
  • 90
29
votes
1 answer

Why does Kotlin have two syntaxes for lambdas / anonymous functions?

Kotlin has two ways of declaring an anonymous function (aka a lambda). The two different syntaxes are: val lambda = { input : String -> "received ${input}" } and val anonymousFunction = fun (input : String): String { return "received…
gypsydave5
  • 542
  • 7
  • 15
29
votes
3 answers

Is it possible to specify an anonymous function's return type, in Scala?

I know you can create an anonymous function, and have the compiler infer its return type: val x = () => { System.currentTimeMillis } Just for static typing's sake, is it possible to specify its return type as well? I think it would make things a…
Geo
  • 93,257
  • 117
  • 344
  • 520
29
votes
6 answers

Javascript arguments.callee what is it for

I haven't found any complete cross-browser doc of this variable. What is arguments.callee for? how does it work? Which arguments does it have?
Jordi P.S.
  • 3,838
  • 7
  • 36
  • 59
28
votes
6 answers

Are anonymous functions a bad practice in JavaScript?

I was reading that using anonymous functions in javascript is bad practice, because it can make debugging a pain, but I haven't seen this for myself. Are anonymous functions in JavaScript really bad practice and, if so, why?
stevebot
  • 23,275
  • 29
  • 119
  • 181
27
votes
3 answers

C# ToDictionary lambda select index and element?

I have a string like string strn = "abcdefghjiklmnopqrstuvwxyz" and want a dictionary like: Dictionary(){ {'a',0}, {'b',1}, {'c',2}, ... } I've been trying things like strn.ToDictionary((x,i) => x,(x,i)=>i); ...but I've…
mowwwalker
  • 16,634
  • 25
  • 104
  • 157