Skip to main content
Filter by
Sorted by
Tagged with
7610 votes
86 answers
1.6m views

How would you explain JavaScript closures to someone with a knowledge of the concepts they consist of (for example functions, variables and the like), but does not understand closures themselves? I ...
835 votes
43 answers
895k views

How can I create static variables in Javascript?
Rajat's user avatar
  • 34.3k
986 votes
26 answers
597k views

I'm trying to figure out Python lambdas. Is lambda one of those "interesting" language items that in real life should be forgotten? I'm sure there are some edge cases where it might be ...
meade's user avatar
  • 23.4k
1 vote
1 answer
104 views

I am trying to get the following simple apply function to compile (simlified for example): struct Context(i32); pub async fn apply<F, Fut>(ctx: &mut Context, f: F) where F: Fn(&mut ...
ChrisB's user avatar
  • 4,156
1 vote
1 answer
86 views

I was experimenting with closures in JavaScript and ran into something confusing. function outer() { let x = 10; return new Function("return x;"); } const fn = outer(); console.log(fn()); I ...
Prince verma's user avatar
5 votes
1 answer
75 views

I have the following code: fn test() -> (impl FnMut(&mut u8), impl FnMut(&mut u8)) { let a = |v: &mut u8| {*v = 0}; let b = move |v: &mut u8| { a(v); println!("{}&...
Nils Oskar Nuernbergk's user avatar
0 votes
1 answer
183 views

Consider the following code: int main() { int n = 0; return [n] { return n; }(); // ok } According to https://cppinsights.io, the code above will be translated into the following code: int ...
xmllmx's user avatar
  • 44.6k
580 votes
12 answers
123k views

A friend of mine and I are currently discussing what is a closure in JS and what isn't. We just want to make sure we really understand it correctly. Let's take this example. We have a counting loop ...
leemes's user avatar
  • 45.9k
491 votes
6 answers
250k views

I'm checking out some PHP 5.3.0 features and ran across some code on the site that looks quite funny: public function getTotal($tax) { $total = 0.00; $callback = /* This line here: */...
SeanDowney's user avatar
  • 17.8k
-2 votes
1 answer
71 views

I'm trying to set GLOBAL variable available across all modules of Node.js app. module.exports = app => { app.get('/config', async (req, res) => { ..... const { data } = await axios....
John Glabb's user avatar
  • 1,711
384 votes
10 answers
177k views

I saw the following in the source for WebKit HTML 5 SQL Storage Notes Demo: function Note() { var self = this; var note = document.createElement('div'); note.className = 'note'; note....
Thomas L Holaday's user avatar
2 votes
1 answer
92 views

My problem is fairly simple: I'm trying to execute a file on a timer elapsed event. I use a closure to preserve the value of my variable. But I think I struggle to understand what is the lifetime of ...
Rouge a's user avatar
  • 25
385 votes
8 answers
97k views

Recently I started playing around with Python and I came around something peculiar in the way closures work. Consider the following code: adders = [None, None, None, None] for i in [0, 1, 2, 3]: ...
Boaz's user avatar
  • 26.3k
0 votes
1 answer
59 views

I'm trying to implement an array where items can occupy multiple slots (details not relevant to question). I have created a method that allows me to iterate over a range of items: pub fn get(&self,...
Fred's user avatar
  • 497
0 votes
1 answer
96 views

I was considering the possibility of passing a closure that owns some thread-safe value, to a spawned thread. The thread would then be able to call something only knowing the signature, while the ...
goose_lake's user avatar
  • 1,627
1 vote
1 answer
51 views

I'm writing a bot on Rust via teloxide + tokio. I have this run method (called from main) pub async fn run() { dotenv::dotenv().ok(); pretty_env_logger::init(); log::info!("Starting ...
Skyman2413's user avatar
7 votes
1 answer
233 views

Here's a minimal reproduction for an issue I was having in a larger codebase: enum A<'b> { A1(&'b u8) } fn consume_fnmut(_f: &mut impl FnMut (A)) {} // Desugared: // fn ...
ais523's user avatar
  • 2,352
1 vote
1 answer
57 views

I'm curently exploring Lua (5.4.7) vm implementation, it's relatively simple but i can't figure our how return works in case of vararg functions. functions with fixed amount of params are quite simple,...
vanilla's user avatar
  • 145
189 votes
3 answers
134k views

I have a protocol: enum DataFetchResult { case success(data: Data) case failure } protocol DataServiceType { func fetchData(location: String, completion: (DataFetchResult) -> (Void)) ...
Lukasz's user avatar
  • 20k
294 votes
11 answers
83k views

I met an interesting issue about C#. I have code like below. List<Func<int>> actions = new List<Func<int>>(); int variable = 0; while (variable < 5) { actions.Add(() =&...
Morgan Cheng's user avatar
  • 76.4k
237 votes
9 answers
117k views

I'm trying to create functions inside of a loop: functions = [] for i in range(3): def f(): return i functions.append(f) Alternatively, with lambda: functions = [] for i in range(3):...
sharvey's user avatar
  • 8,225
158 votes
8 answers
136k views

In Objective-C, you can define a block's input and output, store one of those blocks that's passed in to a method, then use that block later: // in .h typedef void (^...
Jay Dub's user avatar
  • 1,682
113 votes
6 answers
85k views

let sortedNumbers = numbers.sort { $0 > $1 } print(sortedNumbers) Can anyone explain what $0 and $1 means in Swift? Another Sample array.forEach { actions.append($0) }
aashish tamsya's user avatar
229 votes
8 answers
333k views

What am I doing wrong here? counter = 0 def increment(): counter += 1 increment() The above code throws an UnboundLocalError.
Randomblue's user avatar
  • 117k
227 votes
12 answers
54k views

What is a closure? Do we have them in .NET? If they do exist in .NET, could you please provide a code snippet (preferably in C#) explaining it?
Developer's user avatar
  • 18.9k

1
2 3 4 5
182