anonymous-function

Guards and concatiating to lists in an anonymous function

心已入冬 提交于 2019-12-11 05:13:38
问题 I am trying to wrap my head around the syntax of Haskell. This problem is very simple to solve logically. I have to break up a list of positive and negative integers and group them such that [1,2,3,-1,-2,-3,1,2,3] becomes [[1,2,3],[-1,-2,-3], [1,2,3]] I would like to use a higher order function, foldr to be able to do that with an anonymous function taking in two arguements. This is what I have so far. split = foldr (\ x y -> if (x > 0) then if (head (head y)) < 0 then [x] : y else x : head y

Are functions passed as parameters always callbacks? JavaScript

人盡茶涼 提交于 2019-12-11 03:22:07
问题 If I have the code below, where I pass two functions as a parameters into the function sayHi , is this an example of a callback? I notice there are two ways of running these 'parameter functions': either as below, we I call the functions where they are defined (as arguments), or alternatively where I call the parameter in the sayHi function. Would this be the difference between a callback and an anonymous function? function sayHi(name, testForTrue) { if (testForTrue == true) { console.log

Asynchronous method 'anonymous' should not return void

巧了我就是萌 提交于 2019-12-11 01:36:09
问题 Can someone help me resolve this problem I tried everything. I usually know how to resolve that problem but not with anonymous method. DelegateCommand has 2 constructors. 1) public DelegateCommand (Action executeMethod) 2) public DelegateCommand (Action executeMethod, Func canExecute). I wanna know is it possible some how to remove that warning. Async and await are needed otherwise my method: enterButtonClicked(); would be called synchronously. ... public DelegateCommand EnterButton { get;

PHP Anonymous Function as Default Argument?

偶尔善良 提交于 2019-12-11 01:17:37
问题 Is there a way to do this in php? //in a class public static function myFunc($x = function($arg) { return 42+$arg; }) { return $x(8); //return 50 if default func is passed in } 回答1: PHP Default function arguments can only be of scalar or array types: The default value must be a constant expression, not (for example) a variable, a class member or a function call. From: PHP Manual / Function Arguments / Default argument values How about: public static function myFunc($x = null) { if (null ===

What is the benefit of assigning a self executing anonymous function to a variable in javascript?

走远了吗. 提交于 2019-12-11 00:52:41
问题 I was reading a post about how to fire a function after a window resize was complete and came across some examples that assigned self executing anonymous functions to variables: var delay = (function(){ var timer = 0; return function(callback, ms){ clearTimeout (timer); timer = setTimeout(callback, ms); }; })(); $(window).resize(function() { delay(function(){ alert('Resize...'); //... }, 500); }); What is the difference/benefit of making the function operand self executing as opposed to it's

Extract Data from Anonymous Function Scope

旧城冷巷雨未停 提交于 2019-12-10 20:15:50
问题 Because of the complexity of this application, I have a need to wrap Facebook API calls, like so. //In main file, read is always undefined var read = fb_connect.readStream(); // In fb_wrapper.js function readStream () { var stream; FB.api('/me/feed', {limit:10000}, function (response) { stream = response.data; }); return stream; } I know that due to the asynchronous nature of the call, the rest of the readStream() function will return stream (which has no value). I am having trouble finding a

Can't pass delegate

旧巷老猫 提交于 2019-12-10 20:07:58
问题 I've created a function that gets console input from the user as-long as it fits a filter so to speak. public delegate TResult OutFunc<in T, TValue, out TResult>(T arg1, out TValue arg2); public static T PromptForInput<T>(string prompt, OutFunc<string, T, bool> filter) { T value; do { Console.Write(prompt); } while (!filter(Console.ReadLine(), out value)); return value; } This works great when I call the method as I do below. which gets a number from the user as long as it parses to an int

Converting Cell array of function handle into a single array of function handle

牧云@^-^@ 提交于 2019-12-10 18:15:02
问题 I need to build up a vector of non-linear equations to be used in fsolve to solve it. But I should make each element of the vector in each loop iteration. How can I make up such a vector? In fact, I can not use cell array. How can I convert a cell array like {@(x) x(1)+x(2)^2; @(x) x(1)-2*(x(2))} into an array like @(x) [ x(1)+x(2)^2 ; x(1)-2*(x(2))] ? Because I want to use fsolve to solve the system of non-linear equations. 回答1: Use func2str to get the function definitions in string and use

How can I dynamically check the number of arguments expected of an anonymous function in PHP?

三世轮回 提交于 2019-12-10 17:55:56
问题 Is it possible to get the number of arguments expected of an anonymous function in PHP? I'm aware of ReflectionMethod, but that seems to only work if the method is defined on a class. In my case, the anonymous function is either going to have 1 or two arguments. I'd prefer to do the checking correctly, rather than wrapping the first call in a try/catch, and trying again with 2 parameters if the first has failed. 回答1: Try this: // returns the arity of the given closure function arity($lambda)

How do I optimize constrained integral expressions in MATLAB using anonymous functions?

倾然丶 夕夏残阳落幕 提交于 2019-12-10 17:03:10
问题 I have an integrated error expression E = int[ abs(x- p )^2 ]dx with limits x|0 to x| L . The variable p is a polynomial of the form 2*(a*sin(x)+b(a)*sin(2*x)+c(a)*sin(3*x)) . In other words, both coefficients b and c are known expressions of a . An additional equation is given as dE/da = 0 . If the upper limit L is defined, the system of equations is closed and I can solve for a , giving the three coefficients. I managed to get an optimization routine to solve for a purely based on