anonymous-function

In Scala, can you make an anonymous function have a default argument?

陌路散爱 提交于 2019-12-28 13:53:32
问题 This works: scala> def test(name: String = "joe"): Boolean = true test: (name: String)Boolean I expected this to work in the same way: scala> val test: String => Boolean = { (name: String = "joe") => true } <console>:1: error: ')' expected but '=' found. 回答1: The boring, correct answer is no, you can't, but actually you kind of can, with the experimental single abstract method (SAM) synthesis in 2.11. First you need to define your own SAM type with the default value for the apply method's

javascript: Using the current for-loop counter-value inside a function() { }?

笑着哭i 提交于 2019-12-28 06:36:10
问题 on a website i want to do this: (simplified) myHandlers = new Array(); for(var i = 0; i < 7; i++) { myHandlers.push(new Handler({ handlerName: 'myHandler'+i, // works, e.g. ->myHandler1, 2, 3 etc. handlerFunc: function(bla) { /*...*/ alert(i); } // doesn't work,all return 7 } } I could set the counter as another attribute of my Handler (which would copy the current value) and use it inside my function, but I guess, there is also a way to actually copy this value, no? 回答1: When handlerFunc is

Equivalent of C# anonymous methods in Java?

霸气de小男生 提交于 2019-12-27 22:09:24
问题 In C# you can define delegates anonymously (even though they are nothing more than syntactic sugar). For example, I can do this: public string DoSomething(Func<string, string> someDelegate) { // Do something involving someDelegate(string s) } DoSomething(delegate(string s){ return s += "asd"; }); DoSomething(delegate(string s){ return s.Reverse(); }); Is it possible to pass code like this in Java? I'm using the processing framework, which has a quite old version of Java (it doesn't have

Convert input of an anonymous function

邮差的信 提交于 2019-12-25 12:11:51
问题 I have an anonymous function A taking two arguments. I need to convert this function so it takes one argument, by changing the other argument to a constant. For example having a function: A = @(X, Y) X + Y; I would like now to have: B = @(Y) 3 + Y; This seems to be a normal thing to do in mathematics, so I guess there is a way to do such thing in MATLAB. I cannot find the solution though. The reason I need to do something like this is that I have a function that does some calculations on A ,

Share variable between two lambdas

青春壹個敷衍的年華 提交于 2019-12-25 04:42:47
问题 I want to be able to share a variable in the containing scope between two lambda functions. I have the following: void holdAdd(const Rect& rectangle, Hold anonymousHeld, Hold anonymousFinish) { std::map<int,bool> identifierCollection; HoldFinish holdFinish = [=](const int& identifier) mutable { if (identifierCollection.count(identifier) == 0) return; identifierCollection.erase(identifier); anonymousFinish(); }; holdCollisionCollection.push_back([=](const int& identifier, const Vec2& point)

How to get unique id of lambda function for “do once” pattern?

余生长醉 提交于 2019-12-25 03:33:04
问题 I want to implement "do once" pattern that allows me to avoid writing 3 things: declaring var first = true if(first) Do(...) statement inside repeated block of code first = false assignment inside repeated block of code I also want to avoid workarounds like these: manually maintaining and passing unique identification into Do function defining once context variable multiple times So my code should look as simple as this: using(var once = new Once()) foreach(var it in new[]{1,2,3}){ once.Do(()

Bot framework - unexpected error when call turnContext.sendActivity method inside Cron callback function

痴心易碎 提交于 2019-12-25 01:35:48
问题 I have some trouble to make scheduled skype notifications. Error: (node:3720) UnhandledPromiseRejectionWarning: TypeError: Cannot perform 'get' on a proxy that has been revoked at CronJob.<anonymous> (C:\bots\clean\bot.js:101:43) at CronJob.fireOnTick (C:\bots\clean\node_modules\cron\lib\cron.js:554:23) at Timeout.callbackWrapper [as _onTimeout] (C:\bots\clean\node_modules\cron\lib\cron.js:621:10) at ontimeout (timers.js:498:11) at tryOnTimeout (timers.js:323:5) at Timer.listOnTimeout (timers

PHP - Passing functions with arguments as arguments

社会主义新天地 提交于 2019-12-24 19:33:57
问题 I have several interchangeable functions with different numbers of arguments, for example: function doSomething1($arg1) { … } function doSomething2($arg1, $arg2) { … } I would like to pass a certain number of these functions, complete with arguments, to another handling function, such as: function doTwoThings($thing1, $thing2) { $thing1(); $thing2(); } Obviously this syntax is not correct but I think it gets my point across. The handling function would be called something like this:

How to use anonymous functions before PHP 5.3? [duplicate]

别说谁变了你拦得住时间么 提交于 2019-12-24 18:09:48
问题 This question already has answers here : PHP modify code to avoid anonymous functions (4 answers) Closed 6 years ago . I have a class that uses 5.3 feature anonymous function (https://github.com/JeffreyWay/Easy-WordPress-Custom-Post-Types/blob/master/jw_custom_posts.php), but there is a problem, on the server I can not upgrade to 5.3. Is there any simple workaround to get rid of anonymous functions or do I need to rewrite whole class? 回答1: You have two options: Change the hosting with PHP

php - Access outer class from an anonymous callback

喜你入骨 提交于 2019-12-24 15:59:58
问题 I have a code like this: class Server { private $stopper; public function setStopper() { $this->stopper = TRUE; } public function startServer() { $consumer = new Consumer(); $consumer->onConsume(function($data) { global $consumer; // some processing if( ?? ) { // how to access stopper here?? $consumer->stop(); // also how to access stopServer() here?? } }); $consumer->consume(); } public function stopServer() { ... } } This code is in a file that should run forever unless the setStopper() is