closures

Using Expression to 'Cast' Func<object, object> to Func<T, TRet>

淺唱寂寞╮ 提交于 2019-12-24 02:14:57
问题 I've written a little function that attempts to do the following dynamically: Func<object, object> fa = i => Convert.ChangeType(i, typeof (string)); Func<int, string> fb = o => (string) fa((int)o); The func is as follows: /// <summary> /// Converts <see cref="Func{object, object}" /> to <see cref="Func{T, TResult}" />. /// </summary> public static Delegate Convert(Func<object, object> func, Type argType, Type resultType) { Contract.Requires(func != null); Contract.Requires(resultType != null)

Dynamically calling nested functions based on arguments

跟風遠走 提交于 2019-12-24 01:26:10
问题 If I have the following Python class: class Test(object): funcs = { "me" : "action", "action": "action", "say" : "say", "shout" : "say" } def dispatch(self, cmd): def say: print "Nested Say" def action: print "Nested Action" # The line below gets the function name as a string, # How can I call the nested function based on the string? Test.funcs.get(cmd, "say") I would like to be able to do the following: >>> Test().dispatch("me") Nested Action >>> Test().dispatch("say") Nested Say Any

How to create closure in loop and store it in variable for later execution

≡放荡痞女 提交于 2019-12-24 00:52:50
问题 See code below. I've tried to strip it to its bare bones. I have a _queue array. I want to iterate 10 times. On each iteration, I want to create a function that has a properly scoped reference for j (i.e. j=0 on the first iteration, j=1 on the second iteration, etc.) I want to store that function in variable f, and then add f to the _queue array so I can call it later. The problem of course is that on each iteration of the first loop, instead of storing the closure in f, it immediately

Is it possible in Javascript to create an external closure?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 00:49:08
问题 Normally, to create a closure, you create it inside another function, and it gets the scope of its parent: var parent = function(){ var a = "works!"; var subfunction(){ console.log(a); // "works!" } subfunction(); } I'm trying to figure out a way to emulate this closure behavior with a function that is defined outside of the parent function. I know this is possible using parameters: var parent = function(){ var a = "hello"; subfunction(a); } var subfunction(a){ console.log(a); // works, but

How to return indexedDB query result out of event handler?

大憨熊 提交于 2019-12-24 00:33:29
问题 I have to return query result from indexedDB, but the result is only available in onsuccess event handler. 1 function listPeople(){ ... 4 var open = indexedDB.open("AccordionDatabase",1), 5 res; 6 7 open.onsuccess = function(){ 8 var db = open.result; 9 var transaction = db.transaction("PeopleStore", "readwrite"); 10 var store = transaction.objectStore("PeopleStore"); 11 var request = store.getAll(); 12 request.onsuccess = function(event){ 13 res = event.target.result; 14 console.log(res); 15

Why won't my subVC from UIPageViewController pass data to my View Controller effectively using a closure instead of delegates?

倖福魔咒の 提交于 2019-12-24 00:24:16
问题 My homework project has a single RootVC with the top half of the page being a scrollable UIPageviewController object. The UIPageViewController has a sub-View Controller called SubVCOne. Pressing a greeting button placed on SubVCOne presents a greeting View on the RootVC. I need too find a way to pass this information from the SubVCOne of the UIPageViewController to the RootVC w/o using protocols or delegates or caches; hence closures. My attempts have been a little futile--- Root VC: class

Closure in groovy cannot use private field when called from extending class

僤鯓⒐⒋嵵緔 提交于 2019-12-23 20:37:13
问题 I have a class in groovy, where I have a private field, and a method. In the method, I call http service, and pass a closure there to handle the response. Something like this: class WebUiRestRequestSender { private String jSessionIdCookie def login(String username, String password) { //... httpClient.post( path: login, body: parameters, requestContentType : URLENC ) { resp, reader -> jSessionIdCookie = getSessionCookie(resp) } } } Everything works fine when I create object of this class and

Swift Cast AnyObject to Block

南楼画角 提交于 2019-12-23 19:04:11
问题 So I am using the Salesforce SDK and built bridging headers for the entire SDK. They provide a block syntax which hasn't translated into the most usable code. For instance, func sendRESTRequest(request: SFRestRequest!, failBlock: SFRestFailBlock!, completeBlock: AnyObject!) The complete block is AnyObject!. I was able to get around this with var block : @objc_block (dataResponse :AnyObject!) -> Void = { dataResponse in //I handle the response} restService.sendRESTRequest(request, failBlock: {

this, owner, delegate in Groovy closure

筅森魡賤 提交于 2019-12-23 17:56:56
问题 Here is my code: class SpecialMeanings{ String prop1 = "prop1" def closure = { String prop1 = "inner_prop1" println this.class.name //Prints the class name println this.prop1 println owner.prop1 println delegate.prop1 } } def closure = new SpecialMeanings().closure closure() output is prop1 prop1 prop1 I would expect the first line to be prop1 as this refers to the object in which the closure is defined. However, owner (and be default delegate) should refer to the actual closure. So the next

Closure may outlive the current function

五迷三道 提交于 2019-12-23 16:34:33
问题 I am just starting to learn Rust. For this purpose I am rewriting my C++ project in Rust, but the biggest problems are lifetimes of closures and such. I created a absolute minimal scenario of my problem seen here and below: use std::sync::Arc; use std::cell::{RefCell, Cell}; struct Context { handler: RefCell<Option<Arc<Handler>>>, } impl Context { pub fn new() -> Arc<Context> { let context = Arc::new(Context{ handler: RefCell::new(None), }); let handler = Handler::new(context.clone()); (