anonymous-function

How to call a closure that is a class variable?

邮差的信 提交于 2019-11-26 23:41:14
class MyClass { var $lambda; function __construct() { $this->lambda = function() {echo 'hello world';}; // no errors here, so I assume that this is legal } } $myInstance = new MyClass(); $myInstance->lambda(); //Fatal error: Call to undefined method MyClass::lambda() So what is the correct syntax for reaching class variables ? In PHP, methods and properties are in a separate namespace (you can have a method and a property with the same name), and whether you are accessing a property or a method depends of the syntax you are using to do so. $expr->something() is a method call, so PHP will

How can I write a generic anonymous method?

﹥>﹥吖頭↗ 提交于 2019-11-26 22:18:34
问题 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? 回答1: 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 =

Recursive Anonymous Function Matlab

主宰稳场 提交于 2019-11-26 21:01:40
I know that this is not what anonymous functions are made for, but just as a puzzle I tried to make a recursive function via anonymous functions. The prototype of recursive functions obviously is the factorial function. The problem is that it is difficult to make a case distinction within the anonymous functions. What I managed to do so far is following: f=@(cn,n,f)eval('if n>1; f(cn*n,n-1,f);else;ans=cn;end'); f=@(n)f(1,n,f); Or alternatively: f=@(cn,n,f)eval('if n>1; f(cn*n,n-1,f);else;disp(cn);end'); f=@(n)f(1,n,f); What is not very satisfactory is that you still cannot use this function

Scala underscore minimal function

不打扰是莪最后的温柔 提交于 2019-11-26 21:00:31
Let's create a value for the sake of this question: val a = 1 :: Nil now, I can demonstrate that the anonymous functions can be written in shorthand form like this: a.map(_*2) is it possible to write a shorthand of this function?: a.map((x) => x) my solution doesn't work: a.map(_) Your first shorthand form can also be written point-free a map (2*) Thanks to multiplication being commutative. As for (x) => x , you want the identity function. This is defined in Predef and is generic, so you can be sure that it's type-safe. Daniel C. Sobral For the record, a.map(_) does not work because it stands

Why and how do you use anonymous functions in PHP?

廉价感情. 提交于 2019-11-26 20:15:56
Anonymous functions are available from PHP 5.3. Should I use them or avoid them? If so, how? Edited ; just found some nice trick with php anonymous functions... $container = new DependencyInjectionContainer(); $container->mail = function($container) {}; $conteiner->db = function($container) {}; $container->memcache = function($container) {}; Anonymous functions are useful when using functions that require a callback function like array_filter or array_map do: $arr = range(0, 10); $arr_even = array_filter($arr, function($val) { return $val % 2 == 0; }); $arr_square = array_map(function($val) {

How do you explain this structure in JavaScript?

江枫思渺然 提交于 2019-11-26 20:00:58
问题 (function() { //codehere } )(); What is special about this kind of syntax? What does ()(); imply? 回答1: The creates an anonymous function, closure and all, and the final () tells it to execute itself. It is basically the same as: function name (){...} name(); So basically there is nothing special about this code, it just a 'shortcut' to creating a method and invoking it without having to name it. This also implies that the function is a one off, or an internal function on an object, and is

javascript: recursive anonymous function?

大憨熊 提交于 2019-11-26 19:30:58
Let's say I have a basic recursive function: function recur(data) { data = data+1; var nothing = function() { recur(data); } nothing(); } How could I do this if I have an anonymous function such as... (function(data){ data = data+1; var nothing = function() { //Something here that calls the function? } nothing(); })(); I'd like a way to call the function that called this function... I've seen scripts somewhere (I can't remember where) that can tell you the name of a function called, but I can't recall any of that information right now. You can give the function a name, even when you're

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

老子叫甜甜 提交于 2019-11-26 18:53:37
问题 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,

removeEventListener on anonymous functions in JavaScript

 ̄綄美尐妖づ 提交于 2019-11-26 17:30:45
I have an object that has methods in it. These methods are put into the object inside an anonymous function. It looks like this: var t = {}; window.document.addEventListener("keydown", function(e) { t.scroll = function(x, y) { window.scrollBy(x, y); }; t.scrollTo = function(x, y) { window.scrollTo(x, y); }; }); (there is a lot more code, but this is enough to show the problem) Now I want to stop the event listener in some cases. Therefore I am trying to do a removeEventListener but I can't figure out how to do this. I have read in other questions that it is not possible to call

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

寵の児 提交于 2019-11-26 15:56:46
问题 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? 回答1: It will fail when you try to call a