anonymous-function

Anonymous partial function in early initializer requires “premature access to class”

别来无恙 提交于 2019-12-02 01:27:06
Why does this fail to compile: trait Item trait StringItem extends Item { def makeString: String } trait SomeOtherItem extends Item trait DummyTrait case class Marquee(items: Seq[Item]) extends { val strings: Seq[String] = items.collect { case si: StringItem => si.makeString // <-- partial function inside braces } } with DummyTrait with the error message <$anon: Item => String> requires premature access to class Marquee ? It seems to me that the partial function makes no use of Marquee . Yet this compiles: val pf: PartialFunction[Item, String] = { case si: StringItem => si.makeString } case

need more explanation on w3schools javascript closure example

被刻印的时光 ゝ 提交于 2019-12-02 01:12:18
I'm trying to understand closures and am looking at the W3Schools javascript tutorial. This is one example they give by making a counter. <body> <p>Counting with a local variable.</p> <button type="button" onclick="myFunction()">Count!</button> <p id="demo">0</p> <script> var add = (function () { var counter = 0; return function () {return counter += 1;} })(); function myFunction(){ document.getElementById("demo").innerHTML = add(); } </script> </body> Example Explained The variable add is assigned the return value of a self-invoking function. The self-invoking function only runs once. It sets

How to rewrite an example without the use of create_function?

安稳与你 提交于 2019-12-02 00:35:46
问题 When looking at PHP's create_function it says: If you are using PHP 5.3.0 or newer a native anonymous function should be used instead. I want to recreate same functionality of create_function but using an anonymous function . I do not see how, or if I am approaching it correctly. In essence, how do I change the following so that I no longer use create_function but still can enter free-form formula that I want to be evaluated with my own parameters? $newfunc = create_function( '$a,$b', 'return

How to rewrite an example without the use of create_function?

丶灬走出姿态 提交于 2019-12-01 20:42:53
When looking at PHP's create_function it says: If you are using PHP 5.3.0 or newer a native anonymous function should be used instead. I want to recreate same functionality of create_function but using an anonymous function . I do not see how, or if I am approaching it correctly. In essence, how do I change the following so that I no longer use create_function but still can enter free-form formula that I want to be evaluated with my own parameters? $newfunc = create_function( '$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);' ); echo $newfunc(2, M_E) . "\n"; Example taken from PHP's create

Scala method types and methods as parameters

旧城冷巷雨未停 提交于 2019-12-01 18:09:06
In the following code example, I do not understand why the function fun can be passed as an argument to the method addAction . The method fun is of type Unit , while the method addAction expects a function of type () => Unit . If fun is of type () => Unit , then why does the compiler complain that fun is of type Unit , when I try to add fun to the actions list: actions = fun :: actions ? package myscala object MyScala { def fun() { println("fun1 executed.") } def addAction(a: () => Unit) { actions = a :: actions } var actions: List[() => Unit] = List() def main(args: Array[String]) { // the

Scala method types and methods as parameters

妖精的绣舞 提交于 2019-12-01 17:06:50
问题 In the following code example, I do not understand why the function fun can be passed as an argument to the method addAction . The method fun is of type Unit , while the method addAction expects a function of type () => Unit . If fun is of type () => Unit , then why does the compiler complain that fun is of type Unit , when I try to add fun to the actions list: actions = fun :: actions ? package myscala object MyScala { def fun() { println("fun1 executed.") } def addAction(a: () => Unit) {

Is there a way in Delphi to assign an anonymous method to a button event?

核能气质少年 提交于 2019-12-01 15:23:48
问题 I was wondering if there is a way in Delphi to assign an anonymous method to a form control event. For example: Button1.OnClick := procedure (Sender: TObject) begin ShowMessage('') end; Of course this gives me an error [dcc32 Error] Control.Controller.pas(51): E2009 Incompatible types: 'method pointer and regular procedure' This is because the method must belong to an object, but then it would not be anonymous any more. Perhaps there is some work around for this 回答1: This is not possible. You

anonymous function performance in PHP [closed]

点点圈 提交于 2019-12-01 15:13:42
I'm starting to use functional programming paradigms in php and was wondering what the performance impacts are. Some googling just seems to say that there are some. To be specific, I would like to know: Is there actually a performance impact or is it an urban legend? What is the performance impact (hopefully someone out that has done benchmarks)? What causes this impact (if one exists)? Is it fixed cost, or per execution? Any resources you guys have would be greatly appreciated :) Thanks in advance Jory Geerts I did some testing with array_map(), calling it with: The name of a function ( array

Break array_walk from anonymous function

南笙酒味 提交于 2019-12-01 15:06:50
Is there a way to stop an array_walk from inside the anonymous function ? Here is some sample code (that works) to show what I mean, that checks if an array has only numeric values. $valid = true; array_walk($parent, function ($value) use (&$valid) { if (!is_numeric($value)) { $valid = false; } }); return $valid ? 'Valid' : 'Invalid'; If I have a big enough array, and the first entry is invalid, the rest of the (redundant) checks are still done, so I would like to stop the execution. Using break / continue doesn't work (error: Fatal error: Cannot break/continue 1 level in ... ). Note: I don't

anonymous function performance in PHP [closed]

倾然丶 夕夏残阳落幕 提交于 2019-12-01 14:07:09
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . I'm starting to use functional programming paradigms in php and was wondering what the performance impacts are. Some googling just seems to say that there are some. To be specific, I would like to know: Is there