在Scala中,如何定义一个带有可变数量参数的匿名函数?
scala> def foo = (blah:Int*) => 3
<console>:1: error: ')' expected but identifier found.
def foo = (blah:Int*) => 3
^
Run Code Online (Sandbox Code Playgroud) 是否可以将匿名函数作为参数传递,并让它立即执行,从而传递函数的return值?
function myFunction(Array $data){
print_r($data);
}
myFunction(function(){
$data = array(
'fruit' => 'apple',
'vegetable' => 'broccoli',
'other' => 'canned soup');
return $data;
});
Run Code Online (Sandbox Code Playgroud)
这会因Array类型提示而引发错误,抱怨传递的对象.好吧,如果我删除类型提示,它当然会吐出Closure Object,而不是我想要的结果.我明白,我在技术上合格的对象实例Closure来myFunction,不过,我一定附近,我已经看到了这个在其他地方完成.这可能吗?如果是这样,我做错了什么?
为了便于讨论,我无法修改我传递闭包的函数.
tl; dr:如何将匿名函数声明作为参数传递,从而导致返回值作为参数传递.
PS:如果不清楚,所需的输出是:
Array
(
[fruit] => apple
[vegetable] => broccoli
[other] => canned soup
)
Run Code Online (Sandbox Code Playgroud) 优化我的MATLAB代码,我偶然发现了一个关于匿名函数的奇怪问题.
就像在这个线程中我意识到的那样,有时匿名函数的运行速度非常慢.但是对函数的更改很少,它的运行速度与子函数或嵌套函数一样快.
我使用这个(简单的)测试文件来重现Windows 7 64位下Matlab R2010b的行为:
clear all; close all; clc;
% functions
fn1 = @(x) x^2;
fn2 = @(x) double(x^2);
% variables
x = linspace(-100,100,100000);
N = length(x);
%% anonymous function
y = zeros(1,N);
t = tic;
for i=1:N
y(i) = fn1(x(i));
end
tm.anonymous_1 = toc(t);
%% anonymous function (modified)
y = zeros(1,N);
t = tic;
for i=1:N
y(i) = fn2(x(i));
end
tm.anonymous_2 = toc(t);
%% print
tm
Run Code Online (Sandbox Code Playgroud)
我得到的结果是:
tm =
anonymous_1: 1.0605
anonymous_2: 0.1217
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,第一种方法慢了大约10倍.我不知道是什么触发了这种加速/减速.我尝试了不同的东西,得到了几乎相同(快速)的时间:
fn2 …Run Code Online (Sandbox Code Playgroud) 我可以创建一个接受可变数量参数的匿名函数吗?
我有一个S带有某个字段的struct数组,比方说,bar我想将所有bar值传递给我的匿名函数foo.由于struct中的元素数量S未知,因此foo必须能够接受可变数量的参数.
我能够提出的最接近的事情是传递一个单元格数组作为输入参数列表:
foo({arg1, arg2, arg3, ...})
Run Code Online (Sandbox Code Playgroud)
我正在调用它foo({S.bar}),但它看起来很尴尬.
为此创建一个特殊的m文件似乎有点矫枉过正.还有其他想法吗?
我试图弄清楚C#的匿名函数的语法,对我来说没有意义.为什么这是有效的
Func<string, string> f = x => { return "Hello, world!"; };
Run Code Online (Sandbox Code Playgroud)
但这不是?
Func<string> g = { return "Hello, world!"; };
Run Code Online (Sandbox Code Playgroud) 什么是这个JavaScript的TypeScript等价物?
(function() {
/* code here */
})();
Run Code Online (Sandbox Code Playgroud)
我试过这个
() => {
/* code here */
}
Run Code Online (Sandbox Code Playgroud)
但这会产生
(function() {
/* code here */
});
Run Code Online (Sandbox Code Playgroud)
我需要在末尾添加额外的括号来执行匿名函数.
所以我无法弄清楚为什么变量this.tasks在我的目标对象内部的add事件监听器中变得未定义.我觉得它可能与异步编程有关(我仍然不完全理解).对不起,我有点像一个JS菜鸟,但是如果你们能解释一下我做错了什么,什么可能是更好的解决方案呢!谢谢.
function Goal(name) {
this.gDiv = document.createElement('div');
this.name = name || "goal";
this.tasks = document.createElement('ul');
//Sets the styling and content and adds it to the parent element
this.initialize = function() {
this.gDiv.className = "default";
this.gDiv.setAttribute("id", this.name);
this.gDiv.innerHTML = this.name;
elem.appendChild(this.gDiv);
this.gDiv.parentNode.insertBefore(this.tasks, this.gDiv.nextSibling);
this.tasks.style.display = "none";
};
//Creates a list underneath the a dive associated with the Goal object
this.addTask = function(task) {
var newLi = document.createElement('li');
newLi.innerHTML = task;
this.tasks.appendChild(newLi);
};
this.gDiv.addEventListener('click', function(){
alert(this.tasks);
});
} …Run Code Online (Sandbox Code Playgroud) 我试图使用version_compare在一个文件中支持一些PHP代码的两个版本,但我仍然收到错误.
码:
if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
$alias = preg_replace_callback('/&#x([0-9a-f]{1,7});/i', function($matches) { return chr(hexdec($matches[1])); }, $alias);
$alias = preg_replace_callback('/&#([0-9]{1,7});/', function($matches) { return chr($matches[1]); }, $alias);
} else {
$alias = preg_replace('/&#x([0-9a-f]{1,7});/ei', 'chr(hexdec("\\1"))', $alias);
$alias = preg_replace('/&#([0-9]{1,7});/e', 'chr("\\1")', $alias);
}
Run Code Online (Sandbox Code Playgroud)
但我得到:
PHP Parse错误:语法错误,意外T_FUNCTION
在preg_replace_callback()调用上,可能是因为匿名函数.
我正在使用代码:
var x = function() {return true;};
Run Code Online (Sandbox Code Playgroud)
试图将x设置为true,该函数的返回值,而是x被定义为函数本身.如何将x设置为函数的返回值?我可以通过使用非内联函数或其他类似的东西轻松编码这个问题,但它让我感到困惑,因为我确信必须有一个简单的解决方案.
谢谢.
我假设lambda functions,delegates并且anonymous functions使用相同的主体将具有相同的"速度",但是,运行以下简单程序:
static void Main(string[] args)
{
List<int> items = new List<int>();
Random random = new Random();
for (int i = 0; i < 10000000; i++)
{
items.Add(random.Next());
}
Stopwatch watch;
IEnumerable<int> result;
Func<int, bool> @delegate = delegate(int i)
{
return i < 500;
};
watch = Stopwatch.StartNew();
result = items.Where(@delegate);
watch.Stop();
Console.WriteLine("Delegate: {0}", watch.Elapsed.TotalMilliseconds);
Func<int, bool> lambda = i => i < 500;
watch = Stopwatch.StartNew();
result = items.Where(lambda);
watch.Stop();
Console.WriteLine("Lambda: {0}", …Run Code Online (Sandbox Code Playgroud) javascript ×3
c# ×2
matlab ×2
php ×2
closures ×1
delegates ×1
lambda ×1
performance ×1
scala ×1
typescript ×1
undefined ×1