closures

How do Valas closures map to Genie?

与世无争的帅哥 提交于 2019-12-30 06:41:25
问题 The Vala Tutorial has an example about DBus using anonymous methods. Bus.own_name (BusType.SESSION, "org.example.DemoService", /* name to register */ BusNameOwnerFlags.NONE, /* flags */ on_bus_aquired, /* callback function on registration succeeded */ () => {}, /* callback on name register succeeded */ () => stderr.printf ("Could not acquire name\n")); /* callback on name lost */ I am trying to rewrite this code in Genie, but could not manage to convert the two last lines. The Genie Tutorial

What is the best known transitive closure algorithm for a directed graph?

左心房为你撑大大i 提交于 2019-12-30 04:04:08
问题 In terms of runtime, what is the best known transitive closure algorithm for directed graphs? I am currently using Warshall's algorithm but its O(n^3). Although, due to the graph representation my implementation does slightly better (instead of checking all edges, it only checks all out going edges). Is there any transitive closure algorithm which is better than this? In particular, is there anything specifically for shared memory multi-threaded architectures? 回答1: This paper discusses the

Javascript Closures

人走茶凉 提交于 2019-12-30 03:13:09
问题 I'm quite still confused with the concept of closure in JavaScript. I get the point that closure is the ability of the inner function to access the variable created within its mother function after the mother function has returned. But I'm still confused why do we have to create inner function to protect the local variable if we could just create a variable inside the function? 回答1: We need to create an inner function so that the variables in the outer function have some existence after the

Finding a function's parameters in Python

时光毁灭记忆、已成空白 提交于 2019-12-30 02:26:08
问题 I want to be able to ask a class's __init__ method what it's parameters are. The straightforward approach is the following: cls.__init__.__func__.__code__.co_varnames[:code.co_argcount] However, that won't work if the class has any decorators. It will give the parameter list for the function returned by the decorator. I want to get down to the original __init__ method and get those original parameters. In the case of a decorator, the decorator function is going to be found in the closure of

How to store local variables in jQuery click functions?

天大地大妈咪最大 提交于 2019-12-30 01:08:47
问题 I'm trying to figure out how to store external variable values in the functions created during jQuery's click() event. Here's a sample of the code I'm working with now. for(var i=0; i<3; i++){ $('#tmpid'+i).click(function(){ var gid = i; alert(gid); }); } <div id="tmpid0">1al</div> <div id="tmpid1">asd</div> <div id="tmpid2">qwe</div> So what's happening is that the events are attaching properly, but the value of 'gid' is always the last incremented value of 'i'. I'm not sure how to setup the

Objective-C Blocks, Recursion Fails

岁酱吖の 提交于 2019-12-30 00:40:10
问题 Sup guys, I'm trying to do a function that calls itself but by putting everything on one block, As you can see, the following function is intended to be called an indefinite amount of times (until arcrandom returns a number lower than 50) and you should expect as an output a variable number of "RUNNING" messages, depending on chance. void (^_test_closure)(void) = ^ { NSLog(@"RUNNING"); if(arc4random() % 100 > 50) { _test_closure(); } }; _test_closure(); However, when running it, I get an EXC

Is there a particular reason LinqKit's expander can't pick up Expressions from fields?

那年仲夏 提交于 2019-12-29 11:34:17
问题 I'm using LinqKit library which allows combining expressions on the fly. This is a pure bliss for writing Entity Framewok data acess layer because several expressions can optionally be reused and combined, which allows both for readable and efficient code. Consider following piece of code: private static readonly Expression<Func<Message, int, MessageView>> _selectMessageViewExpr = ( Message msg, int requestingUserId ) => new MessageView { MessageID = msg.ID, RequestingUserID =

How can I pass several methods (with parameters) AS a parameter?

时光毁灭记忆、已成空白 提交于 2019-12-29 08:15:12
问题 Suppose I have the following WCF code: try { ServiceClient proxy = new ServiceClient(); proxy.ClientCredentials.UserName.UserName = "user"; proxy.ClientCredentials.UserName.Password = "password"; proxy.GetData(2); if (proxy.State = CommunicationState.Opened) { proxy.GetData("data"); } proxy.Close(); } catch (FaultException ex) { // handle the exception } And since I notice that the try...catch and other logic is repetitive, not to mention that setting up a WCF call is expensive, I want to

Lambda capture problem with iterators?

落花浮王杯 提交于 2019-12-29 08:10:10
问题 Apologies if this question has been asked already, but suppose we have this code (I've run it with Mono 2.10.2 and compiled with gmcs 2.10.2.0): using System; public class App { public static void Main(string[] args) { Func<string> f = null; var strs = new string[]{ "foo", "bar", "zar" }; foreach (var str in strs) { if ("foo".Equals(str)) f = () => str; } Console.WriteLine(f()); // [1]: Prints 'zar' foreach (var str in strs) { var localStr = str; if ("foo".Equals(str)) f = () => localStr; }

Recursive lambda callbacks without Y Combinator

梦想与她 提交于 2019-12-29 06:25:15
问题 I wish to create a callback that recursively returns itself as a callback. The suggested method to recurse is for the function to have a reference to itself: std::function<void (int)> recursive_function = [&] (int recurse) { std::cout << recurse << std::endl; if (recurse > 0) { recursive_function(recurse - 1); } }; This fails as soon as you return it from a function: #include <functional> #include <iostream> volatile bool no_optimize = true; std::function<void (int)> get_recursive_function()