anonymous-function

Difference between function with a name and function without name in Javascript

邮差的信 提交于 2019-11-29 20:27:01
1. function abc(){ alert("named function"); } v/s 2. function(){ alert("Un-Named function"); } Kindly explain from beginners point. They work exactly the same. It's only in how you are able to run them that they are different. So example #1 you could call again at any point with abc(); . For example 2, you would either have to pass it as a parameter to another function, or set a variable to store it, like this: var someFunction = function() { alert("Un-Named function"); } Here's how to pass it into another function and run it. // define it function iRunOtherFunctions(otherFunction) {

Anonymous recursive function in Scala

隐身守侯 提交于 2019-11-29 19:57:43
Is there a way to write an anonymous function that is recursive in Scala? I'm thinking of something like this: ((t: Tree) => { print(t.value); for (c <- t.children) thisMethod(c) })(root) (Related question: Which languages support *recursive* function literals / anonymous functions? ) Rustem Suniev As described in the link you posted. You can use Y-combinator. Here is example: scala> def fix[A,B](f: (A=>B)=>(A=>B)): A=>B = f(fix(f))(_) fix: [A,B](f: ((A) => B) => (A) => B)(A) => B scala> val fact = fix[Int,Int](f => a => if(a<=0) 1 else f(a-1) * a) fact: (Int) => Int = <function1> scala> fact

How can I write an anonymous function in Java?

老子叫甜甜 提交于 2019-11-29 19:41:25
Is it even possible? chris if you mean an anonymous function, and are using a version of Java before Java 8, then in a word, no. ( Read about lambda expressions if you use Java 8+ ) However, you can implement an interface with a function like so : Comparator<String> c = new Comparator<String>() { int compare(String s, String s2) { ... } }; and you can use this with inner classes to get an almost-anonymous function :) polygenelubricants Here's an example of an anonymous inner class. System.out.println(new Object() { @Override public String toString() { return "Hello world!"; } }); // prints

How does variable scope work within the Mocha test framework?

核能气质少年 提交于 2019-11-29 17:05:25
问题 I am a relative newbie to all things javascript, node.js, mocha etc. In my code I have a Unit object that has a disable() that sets the disabled property to true and a isDisabled() that returns the disabled property. It also has a method nextTurnReset() that resets the unit on the start of the next turn. I have written a test suite to test this behavior. I first disable the object and then try to test to see if it is disabled. However, the unit variable inside my first test - which is within

Why can't I assign my function reference to a matching variable? E2555 is raised

匆匆过客 提交于 2019-11-29 17:04:07
问题 I'm trying to build an custom comparer which allows the assignment of the comparison function to an internal field. In order to ease the creation of the comparer, I tried to add a constructor-like class function Construct which initializes the comparer. Now if I try to compile the following example, the compiler displays [dcc32 Fehler] ConsoleDemo1.dpr(37): E2555 Symbol 'Result' cannot be tracked I have the following example-code: program ConsoleDemo1; {$APPTYPE CONSOLE} {$R *.res} uses

Moq a function with anonymous type

只愿长相守 提交于 2019-11-29 14:41:55
I'm trying to mock this method Task<TResult> GetResultAsync<TResult>(Func<string, TResult> transformFunc) like this iMock.Setup(m => m.GetResultAsync(It.IsAny<Func<string, object>>())).ReturnsAsync(new { isPair = false }); The method to test doing the call passing an anonymous type to the generic parameter like this instance.GetResultAsync(u => new {isPair = u == "something" }) //dont look at the function return because as generic could have diferent implementations in many case Moq never matches my GetResultAsync method with the parameters sent. I'm using Moq 4 The anonymous type is going to

Logical short-circuit inside a function handle

你。 提交于 2019-11-29 13:53:19
I have a function handle that operates on 2d arrays of arbitrary size: R2T = @(DL1,DL2) arrayfun(@(DL1,DL2)... 1/(fzero(@(x)fFitObj1(x)./fFitObj2(x)-... DL1./DL2,[minLim maxLim])) ... ,DL1,DL2) - C1; Here's a bottom-up breakdown of what it does: fzero(@(x)fFitObj1(x)./fFitObj2(x)-DL1./DL2,[minLim maxLim]) - This bit looks for a zero of the considered function on the interval [minLim maxLim] , where fFitObj1 and fFitObj2 are function handles available from before, C1 is some known constant and DL1, DL2 are provided. @(DL1,DL2)1/(fzero(...)) - a wrapper for fzero that allows DL1 and DL2 to be

Are anonymous functions a bad practice in JavaScript?

牧云@^-^@ 提交于 2019-11-29 10:56:36
问题 I was reading that using anonymous functions in javascript is bad practice, because it can make debugging a pain, but I haven't seen this for myself. Are anonymous functions in JavaScript really bad practice and, if so, why? 回答1: Nope, anonymous functions are used all over the place in JavaScript across the web. It may make debugging a little more difficult in spots, but not nearly enough to say that they shouldn't be used. For example, JQuery makes extensive use of them. There are a lot of

setTimeout() on recursive function within a self invoking function

走远了吗. 提交于 2019-11-29 10:48:27
问题 I want to distribute my code as a self-envoking anonymous functions, as I see many do. Also, within my code I have to monitor for another lib loading, so I can use it when it's available. (function(window, document, undefined) { staffHappens(); var initMyLib = function() { if (typeof(myLib) == 'undefined') { setTimeout("initMyLib()", 50); } else { useMyLib(); } } moreStaffHappens(); initMyLib(); //-> initMyLib is undefined })(this, document); How can this error occur? Should initMyLib be

Converting code with Anonymous functions to PHP 5.2

こ雲淡風輕ζ 提交于 2019-11-29 09:34:15
I have some PHP 5.3 code which builds an array to be passed to a view. This is the code I have. # Select all this users links. $data = $this->link_model->select_user_id($this->user->id); if (count($data) > 0) { # Process the data into the table format. $table = array ( 'properties' => array ( 'delete_link_column' => 0, ), 'callbacks' => array ( # Callback for the name link. function($value) { return sprintf('<a href="/links/view/name/%s">%s</a>', $value, $value); }, # Callback for the category link. function($value) { return sprintf('<a href="/category/view/name/%s">%s</a>', $value, $value); }