论坛首页 编程语言技术论坛

Closure & Block

浏览 10325 次
锁定老帖子 主题:Closure & Block
精华帖 (0) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2006-11-15  
问题:Block & closure的分别是什么,差别是什么,分别适应什么样的情况!

在我概念中一直觉得Ruby的Block和C/C++中的函数指针是类似,只是他写法更简单。对于Python和lisp来说function是一等公民,所以也可以实现和Ruby的Block一样的功能。其实对于Java程序员来说,我认为Block更像匿名内部类。今天找到一篇权威的文章http://www.artima.com/intv/closures.html
该文中提到Closure可以和Block互相转换,而他们的差别就是Closure具有context。不是很理解Closure的含义。目前也暂时想不到什么情况下使用Closure强于使用Block,希望大家share一下。
引用

Bill Venners: Ruby supports blocks and closures. What are blocks and closures, and how are they used?

Yukihiro Matsumoto: Blocks are basically nameless functions. You may be familiar with the lambda from other languages like Lisp or Python. Basically, you can pass a nameless function to another function, and then that function can invoke the passed-in nameless function. For example, a function could perform iteration by passing one item at a time to the nameless function. This is a common style, called higher order function style, among languages that can handle functions as first class objects. Lisp does it. Python does it .Even C does it with function pointers. Many other languages do this style of programming. In Ruby, the difference is mainly a different kind of syntax for higher order functions. In other languages, you have to specify explicitly that a function can accept another function as an argument. But in Ruby, any method can be called with a block as an implicit argument. Inside the method, you can call the block using the yield keyword with a value.

   发表时间:2006-11-15  
geradle 写道

该文中提到Closure可以和Block互相转换,而他们的差别就是Closure具有context。不是很理解Closure的含义。目前也暂时想不到什么情况下使用Closure强于使用Block,希望大家share一下。

这个解释是正确的。前几天在编写xruby的builtin时发现编译器的一个bug,把block当成closure来实现了,结果block中的return没有跳出函数作用域,比如下面这段代码:
def test
  3.times do
    return
  end
end

在block中,这个return是直接跳出函数的,当时是结束当前循环,所以最终的结果是3次循环都执行了,函数也执行到最后。
0 请登录后投票
   发表时间:2006-11-15  
按照SICP的说法,Closure有两个含义,第一个指的就是离散数学中的定义,大概意思是:

一个定义域和函数集合,使得函数的值域仍然落进定义域。

后来不知道为什么这个词用来形容包含自由变量的函数,这个自由变量就是上面所说的Context。

其实这两个定义没有任何关系。

0 请登录后投票
   发表时间:2006-11-15  
唉,Ruby的Closure,Proc,Block这三个概念太相似了,难于区别,虽然不理解也可以写代码,但是还是理解的好。
0 请登录后投票
   发表时间:2006-11-16  
Here is a MF article about closure
www.martinfowler.com/bliki/Closure.html
0 请登录后投票
   发表时间:2006-11-16  
roseanne 写道
Here is a MF article about closure
www.martinfowler.com/bliki/Closure.html


roseanne为什么老用英文写呀?计算机输入不了中文么?
0 请登录后投票
   发表时间:2006-11-16  
roseanne 写道
Here is a MF article about closure
www.martinfowler.com/bliki/Closure.html

这个文章没有讲三者的差别,只是讲了Closure的作用,Closure的作用我已经理解,其在Python中也有对应的实现。
我现在的疑问是,Ruby中这三个东西到底有什么细微的差别,说实话我觉得有点重复了。所以有点疑惑!
0 请登录后投票
   发表时间:2006-11-17  
qiezi was correct.

I read and tested a lot, finally understand the similarity and difference now.

Put together an example code, try to run it, you would understand too.

The key is return!


def test_closure_or_not(closure_or_not)
  puts
  puts "before calling closure_or_not"
  result = closure_or_not.call
  puts "after calling closure_or_not"
  puts "result from closure_or_not: #{result}"
  "test_closure_or_not result"
end

def test_block(&b)
  puts
  puts "before calling block"
  result = b.call
  puts "after calling block"
  puts "result from block: #{result}"
  "test_block result"
end

puts "======w/o return======"
# all three are fine without return
puts "w/o return:" + test_closure_or_not(lambda {"from lambda"})
puts "w/o return:" + test_closure_or_not(Proc.new {"from Proc.new"})
puts "w/o return:" + test_block{"from block"}

puts "======w/ return======"
# Only lambda is fine with return, 
# lambda is closure, block and Proc.new are not
puts "w/ return:" + test_closure_or_not(lambda {return "from lambda"})
puts "w/ return:" + test_closure_or_not(Proc.new {return "from Proc.new"})
puts "w/ return:" + test_block{return "from block"}


0 请登录后投票
   发表时间:2006-11-17  
Pay attention to the comments in code.

Thanks!
0 请登录后投票
   发表时间:2006-11-17  
Thanks roseanne. Now, I know the key is return, but I want to ask why?
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics