在MATLAB中调试时,我使用匿名函数进行诊断打印.例如,
debug_disp = @(str) disp(str);
debug_disp('Something is up.')
...
debug_disp = @(str) disp([]);
% diagnostics are now hidden
Run Code Online (Sandbox Code Playgroud)
使用disp([])"狼吞虎咽"似乎对我来说有点脏; 有更好的选择吗?明显的(?)方法不起作用:
debug_disp = @(str) ;
Run Code Online (Sandbox Code Playgroud)
我认为这可以用于其他功能语言应用程序,而不仅仅是诊断打印.
是否可以访问被引用的类/对象self,static以及$thisPHP中的匿名回调?像这样:
class Foo {
const BAZ = 5;
public static function bar() {
echo self::BAZ; // it works OK
array_filter(array(1,3,5), function($number) /* use(self) */ {
return $number !== self::BAZ; // I cannot access self from here
});
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法让它像通常的变量一样使用use(self)子句?
我正在读这本书,它有这个代码示例
function getFunction() {
var result = [];
for (var i = 0; i < 10; i++) {
result[i] = function(num) {
return function() {
console.log("this is " + num);
}
}(i);
};
return result;
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,但为什么这里的匿名函数没有包含在这样的括号中(function(...))(i);?在哪些情况下可以在匿名函数中省略括号?
Kotlin有两种声明匿名函数的方法(也就是lambda).两种不同的语法是:
val lambda = { input : String ->
"received ${string}"
}
Run Code Online (Sandbox Code Playgroud)
和
val anonymousFunction = fun (input : String): String {
return "received ${string}"
}
Run Code Online (Sandbox Code Playgroud)
我理解两者之间的区别(如本答案中所述),但我不明白的是为什么语言有两种不同的方式来声明同一件事.
对于另一个经验,是否在工作中有优化?匿名函数版本是否过于冗长?lambda版本的语法不支持返回类型吗?
我正在学习更多关于Scala的知识,而且我在http://www.scala-lang.org/node/135中理解匿名函数的例子时遇到了一些麻烦.我已经复制了下面的整个代码块:
object CurryTest extends Application {
def filter(xs: List[Int], p: Int => Boolean): List[Int] =
if (xs.isEmpty) xs
else if (p(xs.head)) xs.head :: filter(xs.tail, p)
else filter(xs.tail, p)
def modN(n: Int)(x: Int) = ((x % n) == 0)
val nums = List(1, 2, 3, 4, 5, 6, 7, 8)
println(filter(nums, modN(2)))
println(filter(nums, modN(3)))
}
Run Code Online (Sandbox Code Playgroud)
我对modN功能的应用感到困惑
def modN(n: Int)(x: Int) = ((x % n) == 0)
Run Code Online (Sandbox Code Playgroud)
在示例中,使用一个参数调用它
modN(2) and modN(3)
Run Code Online (Sandbox Code Playgroud)
modN(n:Int)(x:Int)的语法是什么意思?
因为它是用一个参数调用的,所以我假设它们不是两个参数,但我无法弄清楚mod函数如何使用nums的值.
当尝试使用时创建类似函数的列表时lapply,我发现列表中的所有函数都是相同的,并且等于最终元素应该是什么.
考虑以下:
pow <- function(x,y) x^y
pl <- lapply(1:3,function(y) function(x) pow(x,y))
pl
[[1]]
function (x)
pow(x, y)
<environment: 0x09ccd5f8>
[[2]]
function (x)
pow(x, y)
<environment: 0x09ccd6bc>
[[3]]
function (x)
pow(x, y)
<environment: 0x09ccd780>
Run Code Online (Sandbox Code Playgroud)
当您尝试评估这些功能时,您会得到相同的结果:
pl[[1]](2)
[1] 8
pl[[2]](2)
[1] 8
pl[[3]](2)
[1] 8
Run Code Online (Sandbox Code Playgroud)
这里发生了什么,我怎样才能得到我想要的结果(列表中的正确功能)?
更新:从PHP7开始,现在可以使用以下语法使用匿名函数解除引用:
$array[] = [
'new' => (function()
{
...
return mt_rand();
})(),
'or' => getClosure()()
]
Run Code Online (Sandbox Code Playgroud)
原帖:我最近在试验一些东西,并想知道是否有办法使用匿名函数的返回值
假设我有一个for循环,它创建了一个数组,数组的每个值都必须有一个数据库调用,我想做的是:
for($i = 0; $i != 10; $i++)
{
$array[] = [
'new' => function(){
// some proccesing here maybe
// lets use mt_rand for this example.
return mt_rand();
},
'old' => function(){
return mt_rand();
}
];
}
Run Code Online (Sandbox Code Playgroud)
或者可能
echo function(){
// again, we'll just use mt_rand
return mt_rand();
};
Run Code Online (Sandbox Code Playgroud)
这两个都返回一个closure类.无论如何实际上将它们的返回值传递回数组或回声,对于上面的例子?
更新:我已经确定这是不可能的,因此,功能请求可以在这里找到:http://bugs.php.net/bug.php?id = 64608
我发现自己一直想通过一个Func在地方的一个回报,没有输入Action,例如
Func<int> DoSomething = ...;
Task.Run(DoSomething);
Run Code Online (Sandbox Code Playgroud)
在哪里,我真的不在乎的回报价值DoSomething.
但是,这些类型并不统一,我最终包装了这个调用
Task.Run(() => { DoSomething(); });
Run Code Online (Sandbox Code Playgroud)
有没有办法让这些类型统一而不包装?另外,有没有很好的设计理由为什么他们不统一?