closures

What are the pros and cons of using closures instead of locks for shared state?

為{幸葍}努か 提交于 2019-12-08 02:36:12
问题 I'm trying to assess what is the fastest solution for sharing state across a single-writer, single-reader scenario, where the reader just consumes the latest value of a state variable assigned by the writer . The shared state can be of any managed type (i.e. reference or value types). Ideally the synchronization solution would work as fast as the naive non-synchronized solution as possible, since the method will be used for both single-threaded and multi-threaded scenarios potentially

How do I reuse this JavaScript timeout closure?

天涯浪子 提交于 2019-12-08 01:45:04
问题 I found the following piece of JavaScript code (maybe here at Stack Overflow?) for implementing a timeout: var delay = (function() { var timer = 0; return function(callback, ms) { clearTimeout(timer); timer = setTimeout(callback, ms); }; })(); I'm new to JavaScript, so I'm still trying to wrap my head around closures. If I call delay(firstCallback, 200) in one place and then delay(secondCallback, 200) right after, the first timeout callback is cleared, while the second callback executes

Google Maps v3 opens the last infoWindow when a marker is clicked

喜你入骨 提交于 2019-12-08 00:15:39
问题 I have a shared infoWindow for all my markers. It works good if I use jquery's $().each(function(){}) , but if I change it to JavaScrips's native for or while loop, it's not working as expected. Whenever I click a marker, it will open the last populated marker's infoWindow , instead of the clicked marker's infoWindow . This works: $(stores).each(function() { var storeId = $(this).attr('storeId'); var address = $(this).attr('rawAddress'); var city = $(this).attr('city'); var state = $(this)

Usage of closures with multiple arguments in swift

落爺英雄遲暮 提交于 2019-12-08 00:12:26
问题 This question is largely based on this one: Link The main difference being that I want to pass in arguments to the closure as well. Say I have something like this: func someFunctionThatTakesAClosure(completionClosure: (venues: Dictionary<String, AnyObject>, error: NSError) -> ()) { // function body goes here var error: NSError? let responseDictionary: Dictionary<String, AnyObject> = ["test" : "test2"] completionClosure(venues: responseDictionary, error: error!) } No error here. But when I

Removing event listeners with anonymous function calls in JavaScript

时光总嘲笑我的痴心妄想 提交于 2019-12-08 00:10:30
问题 I'm trying to remove an event listener for created span elements where the function called is within a closure. I've tried various methods and none seem to work. var MyClass = function () {} MyClass.prototype.addSpan = function (el) { var span = document.createElement('span'); span.innerHTML = "Text here"; el.appendChild(span); span.addEventListener('click', (function (obj) { return function () { obj.removeSpan(); } })(this), false); } MyClass.prototype.removeSpan = function () { alert(

Why Apple's closure declarations missing argument labels

一世执手 提交于 2019-12-08 00:05:23
问题 As I read through the UIKit , most of the time I see closures (as a function parameter) with missing argument labels like this: func fooClosure(fooClosure: (Bool) -> Swift.Void) In some cases I can guess what it stands for, in others I can't. Example: Take a look at one of UIActivityViewController 's closure (as a type alias): public typealias UIActivityViewControllerCompletionWithItemsHandler = (UIActivityType?, Bool, [Any]?, Error?) -> Swift.Void What does 3rd type stand for? I have to look

Storing an unboxed closure with a reference arg in a HashMap

社会主义新天地 提交于 2019-12-07 22:51:59
问题 I'm trying to store a closure as a HashMap value. If I pass the closure arg by value, everything works great: use std::collections::hash_map::HashMap; fn main() { let mut cmds: HashMap<String, Box<FnMut(String)->()>> = HashMap::new(); cmds.insert("ping".to_string(), Box::new(|&mut:s| { println!("{}", s); })); match cmds.get_mut("ping") { Some(f) => f("pong".to_string()), _ => () } } (playpen) But if I want a closure that takes a reference arg, things go south: use std::collections::hash_map:

Calling objective-C typedef block from swift

主宰稳场 提交于 2019-12-07 18:56:43
问题 I'm trying to call a method from swift. The method is in a singleton written in objective-C the block in the header file: typedef void(^VPersonResultBlock)(Person *person, NSError *error); - (void)askForMe:(VPersonResultBlock)block; and here's the implementation of that method. - (void)askForMe:(VPersonResultBlock)block { if (_me) block(_me,nil); else { [Person getMeWithBlock:^(PFObject *person, NSError *error) { if (!error) { _me = (Person *)person; block(_me,nil); } else if (error) { block

Swagger generating javascript-closure-angular-client

纵然是瞬间 提交于 2019-12-07 18:05:10
问题 I am new to swagger, I have generated a javascript closure angular client from the swagger's online editor, it gave me a DefaultAPI.js and other JS files that match my objects definitions. I've searched in swagger docs and on the net for a way to use the generated files in an angular application but didn't find any explanation, any clue ? 来源: https://stackoverflow.com/questions/40936437/swagger-generating-javascript-closure-angular-client

Do closures break serialization

久未见 提交于 2019-12-07 17:10:38
问题 Today I faced a SerializationException that refered to some anonymous inner class +<>c__DisplayClass10 stating it was not serializable, when IIS tried to store the session in the ASP.NET State Service: Type 'Xyz.GeneralUnderstandingTests+ASerializableType+<>c__DisplayClass10' in Assembly 'Xyz, Version=1.2.5429.24450, Culture=neutral, PublicKeyToken=null' is not marked as serializable. I looked for lambdas in my code and found quite a few, but most of them were not new and did never have any