closures

How to accept an async function as an argument?

陌路散爱 提交于 2020-07-21 11:13:34
问题 I would like to replicate the behavior and ergonomics of taking a closure/function as an argument much like map does: iterator.map(|x| ...) . I've noticed that some library code allows passing in async functionality, but this method doesn't allow me to pass in arguments: pub fn spawn<F, T>(future: F) -> JoinHandle<T> where F: Future<Output = T> + Send + 'static, T: Send + 'static, spawn(async { foo().await }); I'm hoping to do one of the following: iterator.map(async |x| {...}); async fn a(x:

How to accept an async function as an argument?

六眼飞鱼酱① 提交于 2020-07-21 11:13:01
问题 I would like to replicate the behavior and ergonomics of taking a closure/function as an argument much like map does: iterator.map(|x| ...) . I've noticed that some library code allows passing in async functionality, but this method doesn't allow me to pass in arguments: pub fn spawn<F, T>(future: F) -> JoinHandle<T> where F: Future<Output = T> + Send + 'static, T: Send + 'static, spawn(async { foo().await }); I'm hoping to do one of the following: iterator.map(async |x| {...}); async fn a(x:

Referencing self in super.init

亡梦爱人 提交于 2020-07-06 13:03:35
问题 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 { init(actions: [Action]) { super.init(frame: .zero) for action in actions { // let actionButton = ActionButton(type: .custom) // actionButton.title = action.title // actionButton.handler = action.handler // addSubview(actionButton) } } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has

Remove initiallyInactive queue

谁说我不能喝 提交于 2020-06-28 05:12:55
问题 I have array of photos that needs to be downloaded, but download function can download only one photo at a time. I neeed to make sure that download function is completed before I call it for other photo. I create .initialyInactive DispatchQueue and queue is activated in completion block of download function. This is woking, and photos are downloaded, but my question is how to cancel download process? Can I somehow to remove not activated queues? My code logic looks something like this.. func

How to return the captured variable from `FnMut` closure, which is a captor at the same time

≯℡__Kan透↙ 提交于 2020-06-27 17:27:25
问题 I have a function, collect_n , that returns a Future that repeatedly polls a futures::sync::mpsc::Receiver and collects the results into a vector, and resolves with that vector. The problem is that it consumes the Receiver , so that Receiver can't be used again. I'm trying to write a version that takes ownership of the Receiver but then returns ownership back to the caller when the returned Future resolves. Here's what I wrote: /// Like collect_n but returns the mpsc::Receiver it consumes so

let vs var in javascript [duplicate]

扶醉桌前 提交于 2020-06-25 13:07:39
问题 This question already has answers here : Why let and var bindings behave differently using setTimeout function? [duplicate] (2 answers) Closed 2 years ago . I understand that let has block scope and var has functional scope. But I do not understand in this case, how using let will solve the problem const arr = [1,2,3,4]; for (var i = 0; i < arr.length; i++) { setTimeout(function() { console.log(arr[i]) }, 1000); } // Prints undefined 5 times const arr = [1,2,3,4]; for (let i = 0; i < arr

Call completion Handler function using “Result”

Deadly 提交于 2020-06-17 22:55:51
问题 How to use call getEmployee function while using "Result"? struct Employee { let name: String let designation: String } func getEmployee(name: String, completion: @escaping(Result<Employee?, Error>) -> Void) { } 回答1: First you need to make your structure conform to Codable struct Employee: Codable { let name, designation: String } Than you need to fetch your data from your server and call completion if decoding was successful with your employee .success(employee) or if it fails pass failure

JSHint error : Functions declared within loops referencing an outer scoped variable may lead to confusing semantics

末鹿安然 提交于 2020-06-01 06:45:46
问题 Is it possible to explain why this code snippet throws such an error ECMA 6 is not an option as of now and I have also tried putting the inner $.each function in a closure IIFE that saves maps the value of i to an inner variable within the closure. Please help ! for(var i = 0; i < cityArray.length; i++) { $.each(_cityCards, function(index, item) { var cityName = $(this).attr('data-city'); if(cityName == cityArray[i]) { $(this).css('transform','scale(1)').delay(500).show(); } }); } 回答1: Sound

How many ways there to write a closure in swift?

限于喜欢 提交于 2020-05-24 07:37:02
问题 Question: What are the basic rules and boundaries (in terms of syntax) that need to be considered when writing any closure in swift? 回答1: Closure Rules: If the closure block solely returns value after the 'in' keyword (or the closure block just returns a value) then using 'return' keyword is optional. Defining closure argument type is optional if the closure type is explicitly defined. Defining closure return type is optional, if closure arguments are defined with it's type and there is no

How many ways there to write a closure in swift?

自闭症网瘾萝莉.ら 提交于 2020-05-24 07:34:09
问题 Question: What are the basic rules and boundaries (in terms of syntax) that need to be considered when writing any closure in swift? 回答1: Closure Rules: If the closure block solely returns value after the 'in' keyword (or the closure block just returns a value) then using 'return' keyword is optional. Defining closure argument type is optional if the closure type is explicitly defined. Defining closure return type is optional, if closure arguments are defined with it's type and there is no