Tag: closures

在PHP中closures……究竟是什么,他们什么时候需要使用它们?

所以我正在编写一个很好的,最新的,面向对象的时尚。 我经常使用PHP实现的OOP的各个方面,但是我想知道什么时候需要使用闭包。 那里的任何专家都可以揭示什么时候实施closures是有益的?

Pythonclosures:写入父范围的variables

我有一个函数内的以下代码: stored_blocks = {} def replace_blocks(m): block = m.group(0) block_hash = sha1(block) stored_blocks[block_hash] = block return '{{{%s}}}' % block_hash num_converted = 0 def convert_variables(m): name = m.group(1) num_converted += 1 return '<%%= %s %%>' % name fixed = MATCH_DECLARE_NEW.sub('', template) fixed = MATCH_PYTHON_BLOCK.sub(replace_blocks, fixed) fixed = MATCH_FORMAT.sub(convert_variables, fixed) 将要素添加到stored_blocks工作正常,但我不能增加num_converted在第二num_convertedfunction: UnboundLocalError:分配之前引用的局部variables“num_converted” 我可以使用globalvariables但是全局variables很难看,我真的不需要这个variables是全局的。 所以我很好奇我如何写入父函数作用域中的variables。 nonlocal num_converted可能会完成这项工作,但是我需要一个可以与Python 2.x […]

lambda函数的范围及其参数?

我需要一个callback函数,对于一系列gui事件几乎完全相同。 根据哪个事件调用它,函数的行为会稍有不同。 看起来像一个简单的例子,但我无法弄清这个lambda函数的怪异行为。 所以我有下面的简化代码: def callback(msg): print msg #creating a list of function handles with an iterator funcList=[] for m in ('do', 're', 'mi'): funcList.append(lambda: callback(m)) for f in funcList: f() #create one at a time funcList=[] funcList.append(lambda: callback('do')) funcList.append(lambda: callback('re')) funcList.append(lambda: callback('mi')) for f in funcList: f() 这个代码的输出是: mi mi mi do re mi […]

在Groovy闭包中模拟“继续”的最佳模式

Groovy似乎并不支持中断,而是从closures中继续。 什么是模拟这个最好的方法? revs.eachLine { line -> if (line ==~ /-{28}/) { // continue to next line… } }

什么“闭包”在JavaScript中引用?

我明白什么是封闭的,但是在closure这个术语的意义上,我有一些麻烦。 我曾经在很多网站上看过这个词,但是很less就这个词的实际定义达成一致。 它是保留在堆栈帧的variables吗? 它是被返回的函数吗? 它是外部函数的范围吗? 它是内部(返回)函数的范围吗? 这可能是在返回函数后将variables保留在栈帧上的概念吗? 有人能告诉我究竟closure是指什么?

在JDK中默认是Java中的多重inheritance吗?

JDK 8中提供的新function允许您添加到现有界面,同时保持二进制兼容性。 语法就像 public interface SomeInterface() { void existingInterface(); void newInterface() default SomeClass.defaultImplementation; } 这种方式对于所有现有的SomeInterface实现,当他们升级到这个新版本时,他们并不都突然在newInterface()周围编译错误。 虽然这很整齐,但是当你实现两个接口时,会发生什么事情,这两个接口都添加了一个你没有实现的新的默认方法? 让我以一个例子来解释。 public interface Attendance { boolean present() default DefaultAttendance.present; } public interface Timeline { boolean present() default DefaultTimeline.present; } public class TimeTravelingStudent implements Attendance, Timeline { } // which code gets called? new TimeTravelingStudent().present(); 这是否被定义为JDK 8的一部分呢? 我发现Java神在这里谈论类似的东西http://cs.oswego.edu/pipermail/lambda-lib/2011-February/000068.html ,但它的私人邮件列表的一部分,我不能直接问他们。 有关如何在JDK […]

在函数中使用关键字 – PHP

可能重复: 在Php 5.3.0什么是function“使用”标识符? 一个理智的程序员应该使用它吗? 我一直在检查PHP中的闭包,这是我注意到的: public function getTotal($tax) { $total = 0.00; $callback = function ($quantity, $product) use ($tax, &$total) { $pricePerItem = constant(__CLASS__ . "::PRICE_" . strtoupper($product)); $total += ($pricePerItem * $quantity) * ($tax + 1.0); }; array_walk($this->products, $callback); return round($total, 2); } 有人请给我一个关于在这个代码中use的解释。 function ($quantity, $product) use ($tax, &$total) 当我在PHP中search时,它use关键字在命名空间中use它,但在这里看起来不同。 谢谢。

closures使用非转义参数可能会让它逃脱

我有一个协议: enum DataFetchResult { case success(data: Data) case failure } protocol DataServiceType { func fetchData(location: String, completion: (DataFetchResult) -> (Void)) func cachedData(location: String) -> Data? } 通过一个示例实现: /// An implementation of DataServiceType protocol returning predefined results using arbitrary queue for asynchronyous mechanisms. /// Dedicated to be used in various tests (Unit Tests). class DataMockService: DataServiceType […]

更新closures到Swift 3 – @escaping

我已经更新了我的代码到Xcode 8.0 beta 6,但是我被卡住了似乎是关于新的非转义闭包默认。 在下面的代码中,Xcodebuild议在completion:添加@escaping completion:在下面的代码的第一行中,但是仍然不能编译并进行@escaping 。 * ( 编辑 :事实上,应该在 completion: 之后添加@escaping,正如Xcode所build议的那样,警告仍然可以显示,但清理和编译将会删除它。)*这个代码应该如何重写/修改Swift 3? 我已经看了一下新手册,但是找不到合适的代码示例。 func doSomething(withParameter parameter: Int, completion: () -> ()) { // Does something callSomeOtherFunc(withCompletion: completion) } // Calling the method and execute closure doSomething(withParameter: 2) { // do things in closure } 任何帮助非常感谢!

Swift 1.2中的@noescape属性

在Swift 1.2中有一个新的属性,在函数中有闭包参数,正如文档中所说: 这表明该参数只能被调用(或者在一个调用中作为@ noescapeparameter passing),这意味着它不能超过调用的生命周期。 根据我的理解,在此之前,我们可以使用[weak self]不让闭包有强烈的引用,比如它的类,而self可以是nil或闭包被执行时的实例,但是现在@noescape意味着如果这个阶级失败了,封闭将永远不会被执行。 我理解正确吗? 如果我是正确的,为什么我会使用一个普通函数的@noescape闭包,当他们的行为非常相似?