closures

Rewriting an anonymous function in php 7.4

半城伤御伤魂 提交于 2020-01-31 15:07:22
问题 There is the following anonymous recursive function: $f = function($n) use (&$f) { return ($n == 1) ? 1 : $n * $f($n - 1); }; echo $f(5); // 120 I try to rewrite to version 7.4, but there is an error, please tell me what I'm missing? $f = fn($n) => ($n == 1) ? 1 : $n * $f($n - 1); echo $f(5); Notice: Undefined variable: f Fatal error: Uncaught Error: Function name must be a string 回答1: Just like Barmar said, you can't use $f from the outside scope, because when the implicit binding takes

Closures in C# - initating a thread in a function call with a value type

我怕爱的太早我们不能终老 提交于 2020-01-30 12:27:12
问题 I have this code, which works as I wanted but I don't understand exactly why. Thinking about a stack in C, C++, I'd guess that the p variable will be on the stack on each call, and then erased when the method returns. How does the closure of the thread captures it and more over, captures the correct value every time? The output is what I wanted - files are "_a", "_b", "_c". public enum enumTest { a = 1, b =2, c=3 } private void Form1_Load(object sender, EventArgs e) { callme(enumTest.a);

can swift functions and closures conform to Hashable?

非 Y 不嫁゛ 提交于 2020-01-30 05:18:37
问题 Suppose I want to have a Set of functions or closures. Here's how I would go about it: typealias HandlerX = () -> () static var handlersX = Set<HandlerX>() This produces the following compiler error: Type 'HandlerX' (aka '() -> ()') does not conform to protocol 'Hashable' Is this a dead end? 回答1: Yes, this is a dead end. Hashable isn't really your problem; there's no way to decide whether two closures are Equal (which is a base requirement of Hashable). 来源: https://stackoverflow.com/questions

Swift: Does closure have references to constants or variables?

会有一股神秘感。 提交于 2020-01-29 05:08:58
问题 I know there are several related question and moreover I can find many posts in the Internet. However, I can't understand the fact that closures can hold references. In case of a reference type, it is totally usual and very reasonable, but how about a value type, including struct and enum ? See this code. let counter: () -> Int var count = 0 do { counter = { count += 1 return count } } count += 1 // 1 counter() // 2 counter() // 3 We can access the value type count through two ways. One is by

Is it possible to make a recursive closure in Rust?

巧了我就是萌 提交于 2020-01-26 17:10:51
问题 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 easily done with iteration, but I'm wondering if it's possible to make a recursive function in Rust for more complicated things (such as traversing trees) or if I'm required to use my own stack instead. 回答1: There are a few ways to do this. You can put closures into a struct and pass this struct to the closure. You

Is it possible to make a recursive closure in Rust?

别等时光非礼了梦想. 提交于 2020-01-26 17:08:54
问题 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 easily done with iteration, but I'm wondering if it's possible to make a recursive function in Rust for more complicated things (such as traversing trees) or if I'm required to use my own stack instead. 回答1: There are a few ways to do this. You can put closures into a struct and pass this struct to the closure. You

Hide particular marker google maps api

 ̄綄美尐妖づ 提交于 2020-01-26 04:11:05
问题 Using current code for google maps api i found on stackoverflow, i need to hide all markers except one clicked how can i do it? Not even sure how to approach it? I think it has to do with JS closures however not an expert on that. <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> <title>Google Maps Multiple Markers</title> <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script> </head> <body> <div id="map

Why does this simple closure fail while the other two functions succeed?

送分小仙女□ 提交于 2020-01-25 00:19:45
问题 I've constructed a closure example that I can't get to work, nor can I find any reason why it shouldn't work. Why does it fail to compile on the last closure? Playground struct S {} fn filter<P>(predicate: P) where P: Fn(&S) -> bool, { predicate(&S {}); } fn main() { // this works filter(|_s| true); // this also works fn cb1(_s: &S) -> bool { true } filter(cb1); // but this doesn't work let cb2 = |_s| true; filter(cb2); } Output: error[E0631]: type mismatch in closure arguments --> /tmp

How can I specify the type of a closure argument?

天涯浪子 提交于 2020-01-24 19:25:28
问题 I'm trying to do something like this, but it fails to compile on inspect 's closure arguments: fn main() { vec!(1, 2, 3, 4) .windows(2) .inspect(|&&a[]| println!("{} {}", a[0], a[1])) .count(); } I've tried moving the slice name a around, but couldn't find the sweet spot of the compiler's understanding. 回答1: The direct answer is to use a colon, just like everywhere else you define an arguments type: fn main() { vec!(1, 2, 3, 4) .windows(2) .inspect(|a: &&[u8]| println!("{} {}", a[0], a[1]))

Using Object.DefineProperty and accessing a variable in private scope

▼魔方 西西 提交于 2020-01-24 06:23:13
问题 The following doesn't work, from my getter, I can't see _nickname defined in the 'class' Person. var Person = function (args) { var _nickname = ''; if (args === undefined || args === null) { return; } if (args.nickname !== undefined && args.nickname !== null) { _nickname = args.nickname; } } Object.defineProperty(Person.prototype, "nickname", { get : function () { return _nickname; } }); var x = new Person({ nickname : 'bob' }); console.log(x.nickname); How should one go about accomplishing