closures

Problem with LINQ, anonymous types, and closures

余生颓废 提交于 2019-12-01 23:37:26
问题 I have a piece of code that filters a list using LINQ, creates a list of instances of an anonymous type, and assigns an event handler to each instance: // Select every linear expression and create a menu item from it var items = from expr in expressionList.Expressions where expr.Type == ExpressionType.Linear let stdExpr = (StandardExpression)expr select new { Menu = new ToolStripMenuItem(stdExpr.Expression), // string stdExpr.Slot // int }; // Wire a Click event handler to each menu to set

Javascript: Find exactly 10 words in a prefix tree that start with a given prefix

倖福魔咒の 提交于 2019-12-01 23:04:52
I have a trie (also called a prefix tree). Given a prefix, I want to get a list of ten words that start with the prefix. The thing that's unique about this problem is that I only want 10 of the words that start with the given prefix-- not all of them. There are optimizations that can be made, given this. My code below I know works fine. Each node in the trie has a children property and a this_is_the_end_of_a_word property. For instance, when you insert "hi", this is what the trie looks like: . The problem: Given a prefix, I want to get a list of ten words that start with the prefix. My

JavaScript Closures and setTimeout

安稳与你 提交于 2019-12-01 22:43:44
Closures are something I still don't fully grasp in JS. I think this is a closure issue. I'm trying to create a progress bar. Every x seconds I want to increment the width of a DIV. Here's the part that is supposed to do that: for(i=0;i<=counter;i++){ setTimeout(function (){ myDiv.style.width = wIncrement+"px" timeIncrement++; wIncrement++; },timeIncrement*1000); } What I want to happen is every x seconds, increase the size of the bar. If course, that's not what's happening. I'm pretty sure (hope) that this is a closure issue, but the syntax to mix with a setTimout completely flummoxes me. Can

Why may we use “internal argument labels” in type annotations of closures, when they (seemingly) can never be accessed?

女生的网名这么多〃 提交于 2019-12-01 22:37:01
问题 Background This is naturally legal: let closure: (Int, Int) -> () = { print($0 + $1) } closure(1, 2) // 3 Whereas, since the implementation of evolution proposal SE-0111: Remove type system significance of function argument labels in Swift 3, the following is not legal: let closure: (a: Int, b: Int) -> () = { /* ... */ } Error: function types may not have argument label a , use _ instead. Which is expected, as, quoting from SE-0111: Function types may only be defined in terms of the types of

Inline if statement mutating inout parameter in a void return closure, weird error (Error: type 'Int1' does not conform to protocol 'BooleanType')

两盒软妹~` 提交于 2019-12-01 21:51:40
I've run into a somewhat weird (compile time) error that I cannot make any sense out of. The error is given for the following snippet: /* error: type 'Int1' does not conform to protocol 'BooleanType' */ let closure1 : (inout foo: Int) -> () = { foo -> () in (foo<0 ? (foo = -1) : (foo = 1)) } Error: type 'Int1' does not conform to protocol 'BooleanType' Note that Int 1 is not a typo here. Question 1: Why am I not allowed to use a single inline if statement (with result '()' ) as the implicit return type of a void return closure? Question 2: Out of curiosity, what is the Int1 type? (Curiously

How do I declare, create, and use method pointers in Swift?

泪湿孤枕 提交于 2019-12-01 21:21:20
问题 I'm not talking about pointers to C functions, but to a method within a Swift type. struct Test: GeneratorType { var methodPointer: mutating () -> Bool? // Non-working guess var which: Bool init() { which = false methodPointer = which ? &testMethod1 : &testMethod2 // Also non-working guess } //... } The compiler says " mutating " isn't legal as part of a function declaration. (Actually, it just suggests a semi-colon there.) And for the pointer initialization (after I remove mutating ), the

Is LINQifying my code worth accessing a foreach variable in a closure?

浪尽此生 提交于 2019-12-01 21:19:47
Reminiscent of the title of a bootleg live Rolling Stones recording of yesteryear, Resharper is sharper than I'll ever be; as I had it inspect my code, it told me this regarding closures: 1) "Loop: foreach (var item in PlatypiIds) { var query = db.Table<Locations>().Where(l => l.PlatypusId == item). Where(l=> l.SentTimeUTC >= EarliestToShow). Where(l=> l.SentTimeUTC <= LatestToShow). OrderBy(l => l.SentTimeUTC); if (query != null) { foreach (var q in query) { listLocs.Add(q); } } } ...can be converted into LINQ expression: listLocs.AddRange(from item in PlatypiIds select db.Table<Locations>()

Why may we use “internal argument labels” in type annotations of closures, when they (seemingly) can never be accessed?

被刻印的时光 ゝ 提交于 2019-12-01 21:13:22
Background This is naturally legal: let closure: (Int, Int) -> () = { print($0 + $1) } closure(1, 2) // 3 Whereas, since the implementation of evolution proposal SE-0111: Remove type system significance of function argument labels in Swift 3, the following is not legal: let closure: (a: Int, b: Int) -> () = { /* ... */ } Error: function types may not have argument label a , use _ instead. Which is expected, as, quoting from SE-0111: Function types may only be defined in terms of the types of the formal parameters and the return value. Curiously, however (and as prompted by the error message

Primitive variable does not live long enough

谁说我不能喝 提交于 2019-12-01 21:01:35
There is an error in this piece of code: let a: Vec<_> = (1..10).flat_map(|x| (1..x).map(|_| x)).collect(); The error message: error[E0597]: `x` does not live long enough --> src/main.rs:2:57 | 2 | let a: Vec<_> = (1..10).flat_map(|x| (1..x).map(|_| x)).collect(); | --- ^- - borrowed value needs to live until here | | || | | |borrowed value only lives until here | | borrowed value does not live long enough | capture occurs here But why? Is is a primitive type, i.e. it should be cloned anyway. What do I understand wrong? This does not work because you capture x by reference when you do map(|_|

swift maximum consecutive positive numbers

北慕城南 提交于 2019-12-01 20:55:37
问题 How to count maximum consecutive positive numbers using closures? var numbers = [1,3,4,-1,-2,5,2,-2,-3,-4,5] //in this case it should be 3 print(numbers.reduce(0, { $1 > 0 ? $0 + 1 : $0 } ))//this counts total positive numbers 回答1: Update: Simpler solution: Split the array into slices of positive elements, and determine the maximal slice length: let numbers = [1,3,4,-1,-2,5,2,-2,-3,-4,5] let maxConsecutive = numbers.split(whereSeparator: { $0 <= 0 }).map { $0.count }.max()! print