closures Questions

2

Solved

I am trying to test if a binary search tree is valid: use std::{cell::RefCell, rc::Rc}; pub struct TreeNode { val: i32, left: Option<Rc<RefCell<TreeNode>>>, right: Option<...
Susi asked 10/2, 2019 at 6:34

9

Solved

I'm new to Swift and I was reading the manual when I came across escaping closures. I didn't get the manual's description at all. Could someone please explain to me what escaping closures are in Sw...
Walburga asked 15/9, 2016 at 6:13

6

Solved

Is there a construct in java that does something like this(here implemented in python): [] = [item for item in oldList if item.getInt() > 5] Today I'm using something like: ItemType newList ...
Tubulate asked 21/9, 2010 at 12:10

3

Solved

I cant find any documentation on the Closure type in PHPDoc. So my question is how do I define the parameter of the parameters sent to the closure and its return value ? Example: How do i describ...
Foresheet asked 16/5, 2013 at 14:17

8

I would like to understand what kind of code causes memory leaks in JavaScript and created the script below. However, when I run the script in Safari 6.0.4 on OS X the memory consumption shown in t...
Fermin asked 27/4, 2013 at 20:16

3

Solved

What are the specific conditions for a closure to implement the Fn, FnMut and FnOnce traits? That is: When does a closure not implement the FnOnce trait? When does a closure not implement the Fn...
Abamp asked 11/5, 2015 at 20:49

6

Solved

In Javascript, I would like to define a class with an inner (or nested) class. Within the inner class I'd like to be able to get access to the parent instance. How can I do this efficiently? Some...
Archivolt asked 22/1, 2011 at 15:10

3

Solved

While I was hanging out in the Python chatroom, someone dropped in and reported the following exception: NameError: free variable 'var' referenced before assignment in enclosing scope I'd never se...
Nymphalid asked 11/7, 2014 at 22:12

8

Solved

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]: ...
Neither asked 19/2, 2010 at 9:46

2

What's the difference between doing these two solutions to the same problem? Closure Method const numberIncrementer = startValue => () => startValue++ const getNextNumber = numberIncremen...
Syracuse asked 8/9, 2017 at 15:22

5

Solved

This is a very simple example, but how would I do something similar to: let fact = |x: u32| { match x { 0 => 1, _ => x * fact(x - 1), } }; I know that this specific example can be easi...
Roxannroxanna asked 5/6, 2013 at 18:5

45

Solved

var funcs = []; // let's create 3 functions for (var i = 0; i < 3; i++) { // and store them in funcs funcs[i] = function() { // each should log its value. console.log("My value:", i); }...
Naman asked 15/4, 2009 at 6:6

3

Solved

When using anonymous functions in PHP, you can easily use variables from right outside of its scope by using the use() keyword. In my case the anonymous functions are already defined somewhere, bu...
Wismar asked 3/1, 2013 at 12:24

3

Solved

I have a protocol: enum DataFetchResult { case success(data: Data) case failure } protocol DataServiceType { func fetchData(location: String, completion: (DataFetchResult) -> (Void)) func ...
Inquisitor asked 17/8, 2016 at 7:46

4

Solved

I thought that I improve performance when I replace this code: def f(a, b): return math.sqrt(a) * b result = [] a = 100 for b in range(1000000): result.append(f(a, b)) with: def g(a): def f(...
Wellestablished asked 2/4, 2012 at 0:2

1

Solved

I am a newby Rustacean (completing the rustling course, and so far enjoying it a lot). I am having trouble with some examples of closures in the Rust book. In particular, this one: fn main() { l...
Attenuator asked 8/3, 2023 at 4:55

1

I'm writing a function that accepts different trait implementors. One of them is a closure. Some closures need an argument type annotation and some don't, depending on their bodies. Example (playg...
Winnah asked 28/2, 2019 at 15:34

1

I just found out that the following code compiles in Rust 21 (used to not compile in 18) fn get_func (i: &mut i32) -> impl Fn() -> i32 + '_ { || *i } Is there an implicit move of i inv...
Anthe asked 7/11, 2021 at 6:27

4

Solved

How to wait to return a value after a closure completion. Example: func testmethod() -> String { var abc = "" /* some asynchronous service call block that sets abc to some other value */ { ...
Nathanielnathanil asked 4/7, 2015 at 14:51

7

Apologies if this question has already been raised and answered. What I need to do is very simple in concept, but unfortunately I have not been able to find an answer for it online. I need to crea...

2

Solved

I have the following code (EDIT: Updated the code so everyone can compile it and see): import UIKit struct Action { let text: String let handler: (() -> Void)? } class AlertView : UIView { ...
Faeroese asked 26/2, 2018 at 20:4

10

Solved

This is bound to beg design questions, but I want to serialize or hash a closure in PHP such that I have a unique identifier for that closure. I don't need to be able to call the closure from tha...
Indispensable asked 21/12, 2012 at 2:59

3

Solved

I sometimes use embed at a certain point in a script to quickly flesh out some local functionality. Minimal example: #!/usr/bin/env python # ... import IPython IPython.embed() Developing a loc...
Czardom asked 2/2, 2016 at 18:37

2

Solved

Assuming I have lots of functions or methods with receiver, each of which has different type of parameters. I want to use table-driven method to dispatch function or method call. So I'll construct ...
Baker asked 13/7, 2019 at 8:25

10

Solved

What does nonlocal do in Python 3.x? To close debugging questions where OP needs nonlocal and doesn't realize it, please use Is it possible to modify variable in python that is in outer, but not g...
Shana asked 11/8, 2009 at 17:36

© 2022 - 2025 — McMap. All rights reserved.