closures

Differences between action and methods in Grails controllers

为君一笑 提交于 2019-12-02 06:40:35
问题 As far as I know, if I want to create an action in a controller then I can do it by: class My Controller { def myAction = { println "in my action " } } or I can create it by: class My Controller { def myAction(){ println "in my action " } } Can somebody please tell the difference between the two methodology , or if I have anything wrong with my concept or perception 回答1: The first implementation was defining public closures in the controller, the second is to use public methods. The second

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

不想你离开。 提交于 2019-12-02 06:23:15
问题 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:

Reproduce Capturing iteration variable issue

醉酒当歌 提交于 2019-12-02 06:09:53
问题 I'm rereading a part from c# 5.0 in Nutshell about the capturing iteration variables (Page 138) and I have tried to reproduce the code bellow on c# 4.0 and c# 5.0 but with no hope to catch the difference until now using System; class Test { static void Main() { Action[] actions = new Action[3]; int i = 0; foreach (char c in "abc") actions[i++] = () => Console.Write(c); for (int j = 0; j < 3; j++) { actions[j](); } foreach (Action a in actions) a(); Console.ReadLine(); } } Note I have Visual

Convert anonymous function in PHP 5.3 into PHP 5.2 equivalent

馋奶兔 提交于 2019-12-02 05:19:47
I have error in line 2 and 13 in PHP 5.2, I have no idea to make the correction, I tried using create_function but not working, can anyone help with this? function _process_special_keyword($str){ $callback = function($match){ $ret = $match[1] . '[' . $match[2] . ']'; if(!empty($match[3])){ $ret .= '.[' . $match[3] . ']'; } $ret .= $match[4]; return $ret; }; $strSQL = preg_replace_callback('/([\s\(\.,])(' . SPECIAL_KEYWORDS . ')(?:\.(' . SPECIAL_KEYWORDS . '))?([\s\)\.,])/i', $callback, $str); $callback = function($match){ return 'CASE WHEN ' . $match[1] . ' THEN ' . $match[2] . ' ELSE ' .

How to use closures to create event listeners in a Javascript for loop?

荒凉一梦 提交于 2019-12-02 05:06:22
HTML <span class="char" id="0">?</span> <span class="char" id="1">!</span> <span class="char" id="2">"</span> <span class="char" id="3">/</span> <span class="char" id="4">%</span> <span class="char" id="5">$</span> ... JavaScript var charElems = document.getElementsByClassName('char'); for (var i=0; i < charElems.length; i++) { charElems[i].addEventListener('mouseover',function() { (function(j) {mouseoverCheck(j);}(i)); }); } I have a bunch (hundreds) of span elements with numbers as IDs (starting from 0 and incrementing by 1). What this loop is supposed to do is create mouseover event

What are the correct semantics of a closure over a loop variable? [closed]

こ雲淡風輕ζ 提交于 2019-12-02 04:28:05
Consider the following lua code: f = {} for i = 1, 10 do f[i] = function() print(i .. " ") end end for k = 1, 10 do f[k]() end This prints the numbers from 1 to 10. In this case, i is closed over the value for each iteration of the outer loop. This is how I had always understood closures, and I was very happy... ...until I was porting some lua code into c#, and I tried to do the same thing: var f = new Action[10]; for (int i = 0; i < 10; i++) { f[i] = (new Action(delegate() { Console.Write(i + " "); })); } for (int k = 0; k < 10; k++) { f[k](); } And now I get the number 10 printed 10 times

Differences between action and methods in Grails controllers

痴心易碎 提交于 2019-12-02 04:01:35
As far as I know, if I want to create an action in a controller then I can do it by: class My Controller { def myAction = { println "in my action " } } or I can create it by: class My Controller { def myAction(){ println "in my action " } } Can somebody please tell the difference between the two methodology , or if I have anything wrong with my concept or perception The first implementation was defining public closures in the controller, the second is to use public methods. The second way was introduced in grails 2, and is widely considered to be the best way. A couple of reasons i can think

JavaScript - referencing 'this' in an inner function

霸气de小男生 提交于 2019-12-02 03:58:23
Consider the following code: MyClass.prototype.my_func = function () { this.x = 10; $.ajax({ // ... success: function (data) { alert(this.x); } }); } It doesn't work, since apparently this is not bound into the closure's execution context. I've been able to work it around by introducing another variable: var _this = this; And this works inside the anonymous function. But looks quite ugly to me. Is there some nice way to handle this? This may look like the ugly solution for you and there are some walkarounds (such as using bind() method to change the context), but this is the best solution I

What does it mean to call an escaping closure after the function returns? [duplicate]

寵の児 提交于 2019-12-02 03:56:46
This question already has an answer here: Escaping Closures in Swift 5 answers I was reading the apple developer documentation's definition of escaping closures. It says "a closure is said to escape a function when the closure is passed as an argument to the function, but is called after the function returns" I am not sure what the last part is supposed to mean, what does it mean by "after the function returns"? Does it mean "after the function returns a value"? Take for example a call to an API. This call is going to take time, but we are going to want to do something after the call is

How to use my own version of jQuery with browserified modules

橙三吉。 提交于 2019-12-02 03:51:12
(I should clarify up front: My question is about closures and client module patterns in Javascript. It's not about how to use jQuery.noConflict().) I've got a bit of Javascript that people can add to their websites. I want my own code to have access to a $ variable which resolves to a specific version of jQuery that's independent of whatever the page has loaded. This is easy if all my code is in a single file, inside a closure that I define. But I'm struggling to find a clean way to do this using a module pattern where my code is in separate closures. Background (i.e. the obvious thing which