anonymous-function

Can I store RegExp and Function in JSON?

≯℡__Kan透↙ 提交于 2019-11-27 20:32:48
Given a block like this: var foo = {"regexp":/^http:\/\//, "fun":function(){}, } What is a proper way to store it in JSON? You have to store the RegExp as a string in the JSON object. You can then construct a RegExp object from the string: // JSON Object (can be an imported file, of course) // Store RegExp pattern as a string // Double backslashes are required to put literal \ characters in the string var jsonObject = { "regex": "^http:\\/\\/" }; function fun(url) { var regexp = new RegExp(jsonObject.regex, 'i'); var match; // You can do either: match = url.match(regexp); // Or (useful for

What are the benefits to using anonymous functions instead of named functions for callbacks and parameters in JavaScript event code?

前提是你 提交于 2019-11-27 17:15:17
I'm new-ish to JavaScript. I understand many of the concepts of the language, I've been reading up on the prototype inheritance model, and I'm whetting my whistle with more and more interactive front-end stuff. It's an interesting language, but I'm always a bit turned off by the callback spaghetti that is typical of many non-trivial interaction models. Something that has always seemed strange to me is that in spite of the readability nightmare that is a nest of JavaScript nested callbacks, the one thing that I very rarely see in many examples and tutorials is the use of predefined named

Slow performance using anonymous functions in MATLAB… have others noticed this?

我与影子孤独终老i 提交于 2019-11-27 15:17:44
问题 In order to refactor my MATLAB code, I thought I'd pass around functions as arguments (what MATLAB calls anonymous functions), inspired by functional programming. However, it seems performance is hit quite severely. In the examples below, I compare different approaches. (The code snippet is wrapped in a function in order to be able to use subfunctions) The result I get is 0 seconds for direct, almost 0 seconds using a subfunction, and 5 seconds using anonymous functions. I'm running MATLAB 7

How many arguments does an anonymous function expect in clojure?

萝らか妹 提交于 2019-11-27 14:37:51
How does Clojure determine how many arguments an anonymous function (created with the #... notation) expect? user=> (#(identity [2]) 14) java.lang.IllegalArgumentException: Wrong number of args (1) passed to: user$eval3745$fn (NO_SOURCE_FILE:0) #(println "Hello, world!") -> no arguments #(println (str "Hello, " % "!")) -> 1 argument ( % is a synonym for %1 ) #(println (str %1 ", " %2 "!")) -> 2 arguments and so on. Note that you do not have to use all %n s, the number of arguments expected is defined by the highest n. So #(println (str "Hello, " %2)) still expects two arguments. You can also

Anonymous function vs normal function

十年热恋 提交于 2019-11-27 13:40:00
问题 Just out of interest, are there any speed/functionality differences between function foo(bar) { alert("foo" + bar); } and var foo = function(bar) { alert("foo" + bar); }; 回答1: There are no significant speed differences. (Test) There are functionality differences. Function declarations (like your first) and function expressions (like your second) are processed at different times. They have different effects on the scope in which they occur. Your first function has a true name , your second

Clojure: returning a vector from an anonymous function

孤街醉人 提交于 2019-11-27 13:39:44
问题 I wrote a small anonymous function to be used with a map call. The function returns a vector containing a column name and column value from a SQL result set query. Here is the function (input is the column name): (fn [name] [(keyword name) (.getObject resultset name)]) This works fine, however when I tried to use a "simplified" version of the anonymous function, I got an error: #([(keyword %) (.getObject resultset %)]) java.lang.IllegalArgumentException: Wrong number of args (0) passed to:

Javascript arguments.callee what is it for

假如想象 提交于 2019-11-27 12:16:16
问题 I haven't found any complete cross-browser doc of this variable. What is arguments.callee for? how does it work? Which arguments does it have? 回答1: arguments.callee is a reference to the function that is currently being called. First things first: don't use it: if you're in a strict context, it'll just spew errors. However, personally -and I'm not alone in this- I'll miss this property. Before I get to explain why, I'll give you a pseudo-example of when you might use this: var looper =

Using `$this` in an anonymous function in PHP pre 5.4.0

旧街凉风 提交于 2019-11-27 12:15:51
The PHP manual states It is not possible to use $this from anonymous function before PHP 5.4.0 on the anonymous functions page . But I have found I can make it work by assigning $this to a variable and passing the variable to a use statement at the function definition. $CI = $this; $callback = function () use ($CI) { $CI->public_method(); }; Is this a good practice? Is there a better way to access $this inside an anonymous function using PHP 5.3? It will fail when you try to call a protected or private method on it, because using it that way counts as calling from the outside. There is no way

How can I pass a reference to a function, with parameters? [duplicate]

依然范特西╮ 提交于 2019-11-27 10:12:22
Possible Duplicate: How can I pre-set arguments in JavaScript function call? (Partial Function Application) I need to able to pass a reference to a function with a given set of parameters . Here is an example of passing a reference without parameters: var f = function () { //Some logic here... }; var fr = f; //Here I am passing a reference to function 'f', without parameters fr(); //the 'f' function is invoked, without parameters Now what I need to do is pass the same f function, but this time I would need to pass parameters to the reference. Now, I can do it with an anonymous function and

How can I write a generic anonymous method?

浪尽此生 提交于 2019-11-27 09:40:37
Specifically, I want to write this: public Func<IList<T>, T> SelectElement = list => list.First(); But I get a syntax error at T . Can't I have a generic anonymous method? Nope, sorry. That would require generic fields or generic properties, which are not features that C# supports. The best you can do is make a generic method that introduces T: public Func<IList<T>, T> SelectionMethod<T>() { return list => list.First(); } And now you can say: Func<IList<int>, int> selectInts = SelectionMethod<int>(); Of course you can, but T must be known: class Foo<T> { public Func<IList<T>, T>