好吧,请耐心等待我,我知道这看起来会非常令人费解,但请帮助我了解发生了什么.
from functools import partial
class Cage(object):
def __init__(self, animal):
self.animal = animal
def gotimes(do_the_petting):
do_the_petting()
def get_petters():
for animal in ['cow', 'dog', 'cat']:
cage = Cage(animal)
def pet_function():
print "Mary pets the " + cage.animal + "."
yield (animal, partial(gotimes, pet_function))
funs = list(get_petters())
for name, f in funs:
print name + ":",
f()
Run Code Online (Sandbox Code Playgroud)
得到:
cow: Mary pets the cat.
dog: Mary pets the cat.
cat: Mary pets the cat.
Run Code Online (Sandbox Code Playgroud)
所以基本上,为什么我没有得到三种不同的动物?是不是cage'打包'进入嵌套函数的局部范围?如果没有,对嵌套函数的调用如何查找局部变量?
我知道遇到这些问题通常意味着一个人"做错了",但我想了解会发生什么.
我无法让这些块在Swift上工作.这是一个有效的例子(没有完成块):
UIView.animateWithDuration(0.07) {
self.someButton.alpha = 1
}
Run Code Online (Sandbox Code Playgroud)
或者没有尾随封闭:
UIView.animateWithDuration(0.2, animations: {
self.someButton.alpha = 1
})
Run Code Online (Sandbox Code Playgroud)
但是一旦我尝试添加完成块,它就无法工作:
UIView.animateWithDuration(0.2, animations: {
self.blurBg.alpha = 1
}, completion: {
self.blurBg.hidden = true
})
Run Code Online (Sandbox Code Playgroud)
自动完成功能给了我completion: ((Bool) -> Void)?但不确定如何使它工作.也尝试使用尾随闭包,但得到了同样的错误:
! Could not find an overload for 'animateWithDuration that accepts the supplied arguments
// This is how I do regular animation blocks
UIView.animate(withDuration: 0.2) {
<#code#>
}
// Or with a completion block
UIView.animate(withDuration: 0.2, animations: {
<#code#>
}, completion: { _ in …Run Code Online (Sandbox Code Playgroud) 这是Access到Modified Closure的问题扩展.我只是想验证以下内容是否足够安全,适合生产使用.
List<string> lists = new List<string>();
//Code to retrieve lists from DB
foreach (string list in lists)
{
Button btn = new Button();
btn.Click += new EventHandler(delegate { MessageBox.Show(list); });
}
Run Code Online (Sandbox Code Playgroud)
我每次启动时都只运行一次.现在似乎工作正常.正如Jon在某些情况下提到的反直觉结果一样.那么我需要注意什么呢?如果列表不止一次运行会没问题吗?
我希望能够直接调用我分配给对象属性的闭包,而无需将闭包重新分配给变量然后调用它.这可能吗?
下面的代码不起作用和原因Fatal error: Call to undefined method stdClass::callback().
$obj = new stdClass();
$obj->callback = function() {
print "HelloWorld!";
};
$obj->callback();
Run Code Online (Sandbox Code Playgroud) 我正在研究THREE.js并注意到一个模式,其中函数的定义如下:
var foo = ( function () {
var bar = new Bar();
return function ( ) {
//actual logic using bar from above.
//return result;
};
}());
Run Code Online (Sandbox Code Playgroud)
(示例请参见此处的 raycast方法).
这种方法的正常变化如下所示:
var foo = function () {
var bar = new Bar();
//actual logic.
//return result;
};
Run Code Online (Sandbox Code Playgroud)
将第一个版本与正常变体进行比较,第一个版本似乎有不同之处:
因此,主要区别在于,在第一个变体中,条形图在初始化时仅分配一次,而第二个变体在每次调用时都创建此临时变量.
我最好猜测为什么使用它是为了限制bar的实例数(只有一个),从而节省了内存管理开销.
我的问题:
什么是关闭?它应该包含在Java 7中.(已经讨论了封装包含在Java 7中,但最终没有包括在内.-ed)任何人都可以向我提供一些可靠的参考资料,我可以从中学习有关闭包的内容吗?
哪些具体条件为闭合来实现Fn,FnMut和FnOnce特质?
那是:
FnOnce特性?FnMut特性?Fn特性?例如,改变它的主体上的闭包状态会使编译器无法实现Fn它.
对于Fibers,我们有一个典型的例子:生成Fibonacci数
fib = Fiber.new do
x, y = 0, 1
loop do
Fiber.yield y
x,y = y,x+y
end
end
Run Code Online (Sandbox Code Playgroud)
为什么我们需要光纤呢?我可以用相同的Proc重写这个(实际上是闭包)
def clsr
x, y = 0, 1
Proc.new do
x, y = y, x + y
x
end
end
Run Code Online (Sandbox Code Playgroud)
所以
10.times { puts fib.resume }
Run Code Online (Sandbox Code Playgroud)
和
prc = clsr
10.times { puts prc.call }
Run Code Online (Sandbox Code Playgroud)
将返回相同的结果.
那么纤维有什么优点呢?我可以用Fibers写什么样的东西我不能用lambdas和其他很酷的Ruby功能?
基本上我使用这个方便的函数来处理数据库行(关注PDO和/或其他东西)
function fetch($query,$func) {
$query = mysql_query($query);
while($r = mysql_fetch_assoc($query)) {
$func($r);
}
}
Run Code Online (Sandbox Code Playgroud)
有了这个功能,我可以简单地做到:
fetch("SELECT title FROM tbl", function($r){
//> $r['title'] contains the title
});
Run Code Online (Sandbox Code Playgroud)
现在假设我需要$r['title']在var中连接所有内容(这只是一个例子).
我怎么能这样做?我在想这样的东西,但它不是很优雅:
$result = '';
fetch("SELECT title FROM tbl", function($r){
global $result;
$result .= $r['title'];
});
echo $result;
Run Code Online (Sandbox Code Playgroud) 我刚才正在学习函数指针,当我正在阅读关于这个主题的K&R章节时,第一件让我感到惊讶的是,"嘿,这有点像一个闭包." 我知道这种假设在某种程度上是根本错误的,在网上搜索后我没有找到任何对这种比较的分析.
那么为什么C风格的函数指针与闭包或lambdas根本不同呢?据我所知,它与函数指针仍然指向已定义(命名)函数的事实有关,这与匿名定义函数的做法相反.
为什么将函数传递给在第二种情况下看起来更强大的函数,它是未命名的,而不是第一种传递的正常日常函数?
请告诉我如何以及为什么我错误地比较这两者.
谢谢.