anonymous-function

Argument type of anonymous function

﹥>﹥吖頭↗ 提交于 2019-12-08 17:09:58
问题 I'm having some trouble with this code. It's supposed to be an OperationTree with Elements BinaryOperations and UnaryOperations. The method eval does the evaluation and looks up the variables in a map. Here's the code 1 import collection.immutable.HashMap 2 sealed abstract class OpTree[T]{ 3 4 def eval(v:HashMap[Char,T]):T = { 5 case Elem(x) => x 6 case UnOp(f,c) => { 7 f(c.eval(v)) 8 } 9 case BinOp(f,l,r) => { 10 f(l.eval(v),r.eval(v)) 11 } 12 case Var(c) => { 13 v.get(c) 14 } 15 } 16 } 17 /

How do I make an “empty” anonymous function in MATLAB?

半腔热情 提交于 2019-12-08 14:50:29
问题 I use anonymous functions for diagnostic printing when debugging in MATLAB. E.g., debug_disp = @(str) disp(str); debug_disp('Something is up.') ... debug_disp = @(str) disp([]); % diagnostics are now hidden Using disp([]) as a "gobble" seems a bit dirty to me; is there a better option? The obvious (?) method doesn't work: debug_disp = @(str) ; This could, I think, be useful for other functional language applications, not just diagnostic printing. 回答1: You could add a regular do-nothing

PHP anonymous function variable as reference

核能气质少年 提交于 2019-12-08 08:15:30
问题 While working with Laravel framework, more specific - Form macros, I stumbled upon a weird error. At first, I thought it's something wrong with Laravel, but then I took everything out of context: <?php // placeholder function that takes variable as reference $function = function(&$reference) { // append to variable $reference = $reference . ':' . __METHOD__; }; // test with straight call $variable = 'something'; $function($variable); echo $variable; // test with call_user_func(), that gets

Anonymous function with no curly braces and no argument labels?

只愿长相守 提交于 2019-12-08 02:56:24
问题 I saw some code on another question that seems to create an anonymous function (closure expression) with some unusual syntax: let plus: (Int, Int) -> Int = (+) I understand the left side—that it's declaring a constant of type (Int, Int) -> Int (a function that takes two Integers and returns an Integer). But what is (+) ? How can it declare a function without curly brackets, and how does it refer to the two arguments when there are no argument labels of any kind? The function takes two

Why the execution context (“this”) of prototypical function is wrong in this example?

≡放荡痞女 提交于 2019-12-08 02:20:20
问题 Prototypical function bar is executed elsewhere, in a Node.js environment (where bind should be available). I want this inside bar() function to be the instance of my object : var Foo = function (arg) { this.arg = arg; Foo.prototype.bar.bind(this); }; Foo.prototype.bar = function () { console.log(this); // Not my object! console.log(this.arg); // ... thus this is undefined } var foo = new Foo(); module.execute('action', foo.bar); // foo.bar is the callback ... why bar() logs undefined and

jQuery toggle class with delay works only once

孤街醉人 提交于 2019-12-07 18:04:44
问题 I am clearly missing something fundamental when it comes to jQuery, anonymous functions, and delays. The following code works only ONCE per page load (it will add the class, then remove it after 1 second, and if i click again, it will add the class, but will NEVER remove the class for the duration of the page, unless I reload the page): var jElement = $(currElem); jElement.addClass("highlight") .delay(1000) .queue(function(){ $(this).removeClass("highlight"); }); HOWEVER, if I add the (non

Reference Instance Variables in Javascript Constructor

戏子无情 提交于 2019-12-07 12:44:59
问题 I'm trying to maintain state on an object by doing something like this: obj = function() { this.foo = undefined; this.changeState = function () { (function () { this.foo = "bar" })(); // This is contrived, but same idea. }; }; I want to set the instance variable foo to "bar" when I call the changeState method. For instance: o = new obj(); o.changeState(); alert(o.foo); // This should say "bar" As far as I can tell, what is happening is that "this" in the inner anonymous function is pointing

Multiline anonymous function in Matlab? [duplicate]

送分小仙女□ 提交于 2019-12-07 09:28:18
问题 This question already has answers here : How to execute multiple statements in a MATLAB anonymous function? (6 answers) Closed 4 years ago . Is it possible to create multiline anonymous function in Matlab? There are no appropriate examples in documentation, but also no direct denials. In web discussions I found some derisions of askers as if it silly wish. Nowadays, when most languages introducing lambda expressions with multiline capability this looks strange. 回答1: No, unfortunately this is

Executing a method inside a method

Deadly 提交于 2019-12-07 07:06:54
问题 I'm currently working on a JavaScript exercise in FreeCodeCamp, and one of the test-cases my code should work with is a function call that looks like this: addTogether(2)(3); here is the bare-bones function I'm given: function addTogether() { return; } When I run the code below: function addTogether() { return arguments; } In the console, that the editor provides, I get: TypeError: addTogether(...) is not a function The instructions hint at using the arguments object, and it works well with

Passing anonymous function as callback in Javascript

て烟熏妆下的殇ゞ 提交于 2019-12-07 05:17:41
问题 Is there a way to pass anonymous function as a custom callback function in Javascript? I have this code: function notifyme(msg){ console.log(msg) } notifyme("msg", function(){ //do some custom redirect logic }); I am trying the above code and it's executing notifyme function, but not going further with redirect code. I know I can pass a function name as a callback, but I don't have a specific function that I can pass. This is why they invented the anonymous function I guess. It'd be really