anonymous-function

Recursive Anonymous Function Matlab

痴心易碎 提交于 2019-12-17 05:12:08
问题 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

Why do arrow functions not have the arguments array? [duplicate]

纵然是瞬间 提交于 2019-12-17 04:01:31
问题 This question already has answers here : Official information on `arguments` in ES6 Arrow functions? (2 answers) Closed 2 years ago . function foo(x) { console.log(arguments) } //foo(1) prints [1] but var bar = x => console.log(arguments) gives the following error when invoked in the same way: Uncaught ReferenceError: arguments is not defined 回答1: Arrow functions don't have this since the arguments array-like object was a workaround to begin with, which ES6 has solved with a rest parameter:

(…()) vs. (…)() in javascript closures [duplicate]

戏子无情 提交于 2019-12-17 02:09:29
问题 This question already has answers here : Location of parenthesis for auto-executing anonymous JavaScript functions? (4 answers) Closed 4 years ago . I know this is silly, but there's any difference between this: (function() { var foo = 'bar'; })(); and this? (function() { var foo = 'bar'; }()); JSLint tells us to Move the invocation into the parens that contain the function , but I see no need to. Edit: The answers are too cool. ~function , the JSHint alternative along with jQuery's

Why use named function expressions?

青春壹個敷衍的年華 提交于 2019-12-16 19:32:20
问题 We have two different way for doing function expression in JavaScript: Named function expression (NFE) : var boo = function boo () { alert(1); }; Anonymous function expression : var boo = function () { alert(1); }; And both of them can be called with boo(); . I really can't see why/when I should use anonymous functions and when I should use Named Function Expressions. What difference is there between them? 回答1: In the case of the anonymous function expression, the function is anonymous —

Matlab: How to specify input in matlabFunction?

泄露秘密 提交于 2019-12-14 02:37:16
问题 matlabFunction() is a function that can convert symbolic to anonymous function. But how to specify what input arguments to be appeared on the anonymous function? For example, x = sym('x', [3, 1]) func = matlabFunction(x) It returns a handle with: func = function_handle with value: @(x1,x2,x3)[x1;x2;x3] But how to make this to be returned:? @(x) [x(1); x(2); x(3)] that the whole x is the input arguments, not every element of it. This could be extremely useful when x has very long colums. 回答1:

How to get outer loop index inside anonymous function call?

一曲冷凌霜 提交于 2019-12-14 01:25:31
问题 I have the following javascript code : var Person = [['John', 0, 0, 0],['Chris', 1, 0, 0]]; for (i = 0; i < Person.length; i++ ) { someObj.myMethod(Person[i][0], function (object) { console.log(i); //this prints 2, I want 0 and 1 as per the loop //here I want to access other members of Person[i] array, like Person[i][1], Person[i][2] and Person[i][3] //but these console.log() print 'undefined' because i = 2 !! console.log(Person[i][1]); console.log(Person[i][2]); console.log(Person[i][3]); })

Why is context inside non-interactive Function object different in node.js?

自作多情 提交于 2019-12-13 17:22:39
问题 I'd like to create a function from string that requires another module (don't ask). When I try to do that in node interactive shell, everything is fine and dandy: > f = new Function("return require('crypto')"); [Function] > f.call() { Credentials: [Function: Credentials], (...) prng: [Function] } However, when I put the exact same code in file, I am told that require function is not avaliable: israfel:apiary almad$ node test.coffee undefined:2 return require('crypto') ^ ReferenceError:

Why cant I define a variable inside a MATLAB anonymous function?

戏子无情 提交于 2019-12-13 16:35:57
问题 I must be missing something really simple because this doesnt seem like it should be this hard. This code is correct: clear all whatever = @(x) deal(max(x), size(x)); input = randn(1,1000); [a b] = whatever(input) However, what I really want to do is something like this: clear all whatever = @(x) deal(q = 3; q*max(x), size(x)); input = randn(1,1000); [a b] = whatever(input) Why does this break? I cant define q inside the function?? The whole reason I want to use anonymous functions is so that

Usage of anonymous functions in arrayfun with GPU acceleration (Matlab)

懵懂的女人 提交于 2019-12-13 14:23:26
问题 I am new to the Parallel toolbox from Matlab R2012b and was wondering what the best way is to overcome the following problem. I am analyzing the neighbourhood of every pixel in an image. Which is extremely good case for parallelization. However, I can't seem to get it working. The main catch in the problem is that some "constant" arguments should be passed to the function. So the function should be called for every pixel, however, it also needs to access the surrounding pixels. (Preferable by

Javascript Sandbox unit testing

穿精又带淫゛_ 提交于 2019-12-13 13:08:16
问题 I am using QUnit, which is excellent. I have enclosed my JS app in the (function () {})(); sandbox. This hides a lot of code that I don't want public, but I also need to test that code. Here is an example of how this works: (function () { var PublicAPI = window.PublicAPI = {}; PublicAPI.publicFunction = function (foo) { PrivateAPI.privateFunction(foo); return 'bar'; }; var PrivateAPI = {}; PrivateAPI.privateFunction: function (foo) { // Make secret stuff that never gets returned to the public