closures

Can't pass closures in groovy

ぃ、小莉子 提交于 2019-12-10 15:26:39
问题 I am trying to run a basic example for the Geb library (http://www.gebish.org/manual/current/intro.html#introduction). Here is the code: import geb.Browser Browser.drive { go "http://google.com/ncr" // make sure we actually got to the page assert title == "Google" // enter wikipedia into the search field $("input", name: "q").value("wikipedia") // wait for the change to results page to happen // (google updates the page dynamically without a new request) waitFor { title.endsWith("Google

Swift try inside Objective-C block

不问归期 提交于 2019-12-10 14:57:06
问题 I need to create a function foo that takes a throwing closure as a parameter. I can implement it with either Swift or ObjC but I need to be able to call it from both. Like this: // Swift func bar() throws func foo(_ block: () throws -> void) foo { try bar() } and // Objc [self foo:^( [other barBar]; )]; I tried to implement it with both Swift and ObjC without succes. With Swift: @objc func foo(block: () throws -> Void) I get this error: Method cannot be marked @objc because the type of the

Handling of closures in data.table

给你一囗甜甜゛ 提交于 2019-12-10 14:18:48
问题 I am using the data.table package to return a list of function closures in a j expression as output by the approxfun function from the stats package. Basically, on each Date, I would like a closure that allows me to calculate an arbitrary yval based on an arbitrary xval as determined by approxfun . However, approxfun is only valid when there are at least two unique values of x passed to the function. In the case where there is only one unique value of x, I would like to return a function that

Can someone explain me the flow of this JavaScript function? (Closure concept)

北城以北 提交于 2019-12-10 14:16:42
问题 I'm reading "Eloquent JavaScript". Chapter 3 introduces "Closure" concept and gives you a couple of examples. One of these is next one: function multiplier(factor) { return function(number) { return number * factor; }; } var twice = multiplier(2); console.log(twice(5)); // → 10 I think I understood the concept. If first I execute console.log(twice) , since variable number is undefined, what I get is [Function] . What I don't understand is how twice(5) works. Why local variable number is

Repeated evaluation of pure expression in IO action

本小妞迷上赌 提交于 2019-12-10 13:53:49
问题 I have a procedure that (a) does some IO, (b) constructs a lookup table, and (c) returns an IO action that uses the lookup table. But when compiled with -O , GHC (version 6.12.1) inlines the construction the lookup table, so that it is reevaluated for every call of the IO action. Example: module Main where import Data.Array import Data.IORef import Control.Monad makeAction getX getY sumRef = do x <- getX let a = listArray (0, 1000) [x ..] return $ do y <- getY modifyIORef sumRef (\sum -> sum

Closure captured variable modifies the original as well

妖精的绣舞 提交于 2019-12-10 13:46:26
问题 I have the following simple code: static void Main(string[] args) { int j = 0; Func<int> f = () => { for (int i = 0; i < 3; i++) { j += i; } return j; }; int myStr = f(); Console.WriteLine(myStr); Console.WriteLine(j); Console.Read(); } From what I read when closures are involved, a new type is created by the compiler so it can store the captured variable and maintain a reference to it. However, when I run the following code, both printed lines show 3. I was expecting 0 and 3, because the

Does this Resharper fix for disposed closure warning make any sense?

好久不见. 提交于 2019-12-10 13:32:56
问题 I'm working on getting rid of some warnings from a static code analysis. In one specific case there was no disposing being done on a ManualResetEvent . The code in question executes a Func on the main thread and blocks the calling thread for a certain number of milliseconds. I realize this sounds like a weird thing to do, but it's outside the scope of this question, so bear with me. Suppose I add a using statement like so: object result = null; using (var completedEvent = new ManualResetEvent

Destructuring tuple of tuple in closure

柔情痞子 提交于 2019-12-10 13:28:00
问题 I can destructure a tuple of tuple easily: let tt = (2, (3, 4)) let (a, (b, c)) = tt b // => 3 I'd like to do the same when declaring a closure, for example I thought I could write: [tt].map { (a, (b, c)) in // Use b } Xcode complains with "Unnamed parameters must be written with the empty name". Only way I got it to "work" was: [tt].map { (a, tuple: (b: Int, c: Int)) in // Use tuple.b } This has two drawbacks I'd like to avoid: I need to use tuple.b instead of b I need to specify the types

Is there a simple way to register static closures with Castle Windsor?

别等时光非礼了梦想. 提交于 2019-12-10 13:22:35
问题 I've been experimenting with using named delegates instead of single-method interfaces. This has some advantages for code size, as we can go from (some linebreaks removed so as not to overstate the case): public interface IProductSource { IEnumerable<Product> GetProducts(); } public class DataContextProductSource : IProductSource { private readonly DataContext _DataContext; public DataContextProductSource(DataContext dataContext) { if (dataContext == null) throw new ArgumentNullException(

Understanding crash report (Partial apply…) in Swift

我怕爱的太早我们不能终老 提交于 2019-12-10 13:22:31
问题 I'm new to Swift programming, Recently I have implemented spotlight search for my app with Swift. Everything is working well and good however I"m getting few crash reports nowadays and I have no clue what is "Partial apply" and why it is crashing. As I'm unable to simulate it locally I"m trying to solve it by using crash logs. Following is the crash report... Thread : Crashed: com.apple.root.default-qos 0 AppName 0x1076d4 partial apply for static spotLightSearchManager.(indexAllItem in