[Eiffel Translation Artima.com Interview] Ruby Language Blocks and Closure Structure

zhaozj2021-02-16  62

Note: Matz is the founder of the famous programming language Ruby (Japanese)

This dialogue translated the third part of the artima.com website to Matz's interview.

English article Orine: http://www.artima.com/intv/closures.html

Don't reprint this translation article without my consent

(First published in my website http://www.eiffelqiu.com).

Use blocks to do loop abstraction

Bill Venners:

Ruby supports Blocks and Closure structures. What is Blocks and Closure, how do they use?

YUKIHIRO MATSUMOTO:

Blocks is basically an anonymous function. You may be familiar with other languages ​​such as Lisp or Python.

Lambda function. You can pass an anonymous function to another function, this function can

Call this anonymous function passed. For example, the function can pass through one pass to anonymous functions

Elements perform loop iterations. This is a programming language that can be used as a first type of programming.

Usually, it is called high sort function styles. Lisp can be like this, the same, even

Even C can also achieve this through the function pointer. Many other languages ​​can also do such programming.

In Ruby, the difference is only different in the high sort function syntax style. In other languages, you

It must be displayed indicating that a function can accept another function as a parameter. But in Ruby, any

Methods can be called as a hidden parameter. In the method, you can use Yield keywords

Call the block with a value.

Bill Venners:

What is the benefits of block?

YUKIHIRO MATSUMOTO:

Basically, Block is designed to do loop iteration abstraction. Block's most basic use is to let you

Define how to loop itself in its own way.

For example, if you have a list, sequence, vector group or array, you can use the standard library

The way the way is provided to achieve forward loop, but if you want to achieve loop iteration from the time? Such as

If you use the C language, you have to set up four things: an index, a start value, an end condition

And an incremental variable. This way is not good because it exposes the internal implementation method of the list, we want

I hope to hide internal logic. By using Block, we can hide the internal loop iteration.

A method or function. For example, call list.reverse_each, you can realize a list

A reverse cycle iteration does not need to know how the list is implemented.

Bill Venners:

That is, I passed a Block structure, and the code in this block can be in the loop iteration.

Elements do anything, as for how to reverse traversal depends on the List itself. In other words, I will

The code originally written in the C language loop cycle is passed as a block.

YUKIHIRO MATSUMOTO:

Yes, this means you can define many iterations. You can provide a party that iterates forward

, A way to cycle iteration, and so on. This depends on you. C # also has an iterator, but

It has only one iterator for each class. In Ruby you can have any number of iterators. example

For example, if you have a TREE class, you can travers in a depth priority or breadth priority.

You can provide two traversal methods by providing two different ways.

Bill Venners: Let me think about whether I understand this, in Java, they are realized through the Iterator interface

ITs, for example, the calling program allows Collection to implement Iterator. But call

The order must use loop to traverse the elements returned by Iterator. In the For loop, my code is

The process of the element of each loop iteration, such a loop statement will always be displayed in the calling program. Make

With Block, I don't call a method to get an iterator, I just call a method, the same

When I want me to process the process code for each element to be processed in the loop iteration as a Block block

Configure to this function. Is the benefits of Block not to loop from the For For For For For For For For the callout

The extraction is extracted.

YUKIHIRO MATSUMOTO:

The specific details of the realization cyclic iteration should belong to the class that provides this feature. Calling programs should be as few as possible

I know this. This is the original purpose of the Block structure. In fact, in the earlier version of Ruby,

The method using Block is referred to as an iterator because they are designed to achieve loop iteration. but

In the process of Ruby development, Block's use has been greatly enhanced, from the initial cycle.

Abstract to anything.

Bill Venners:

E.g. . . .

YUKIHIRO MATSUMOTO:

We can create a Closure object from the Block, a Closure object is like LISP

The anonymous function implemented in. You can pass an anonymous function (ie closure) to any way.

The behavior of the custom method. Also give an example if you have a sorted method for sort arrays or

List, you can define a block to define how to compare between elements, which is not a loop.

This is not a loop, but it uses Block.

Bill Venners:

What makes Block become a closure?

YUKIHIRO MATSUMOTO:

The Closure object contains the code that can be run, is executable, the code contains state, performs scope.

That is to say, you capture the operating environment in Closure, that is, local variables. So you can be in one

The local variable is cited in Closure, that is, after the function has returned, his execution range has been destroyed.

The local variable is still in a part in the Closure object, when there is no object to reference it,

The garbage collection device will handle it, and local variables will disappear.

Bill Venners:

So, is the local variables are basically shared by methods and closure objects? If the Closure object

Updated the variable, the method can be seen, if the method updates the variable, the Cosure object can also be seen.

YUKIHIRO MATSUMOTO:

Yes, local variables are shared between Closure and Methods, which is true closure, it is not just

copy.

Bill Venners:

What is the benefit of a real closure? Once I turn a block into a closure, I

What can I do with it?

YUKIHIRO MATSUMOTO:

You can convert a closure to a block, so Closure can be used in any block.

Where you can use. Typically, Closure is used to save a block state in an instance variable.

Because once you convert a block to a Closure, it is a pair that can be referenced by variables.

Icon. Of course, Closure can also be used like other languages, such as passing to the object to implement the method.

Definition of behavior. If you want to pass some code from defining a method, you can of course pass to one

BLOCK. But if you want to pass the same code to two methods (of course this is very rare), but

If you really want to do this, you can convert a block to a Closure, will be the same closure

Pass to multiple methods.

Bill Venners:

It turns out, but what is the benefit of getting the context environment? Let Ruby's closure is different.

It captures the context environment, local variables, etc. of the runtime. So what is good to have a context environment?

Is it what we can't be obtained by passing to an object a code block?

YUKIHIRO MATSUMOTO:

In fact, the reason is that the most important reason is to express trust in Lisp language, and LISP provides real

The Closure structure, so I hope to continue to provide this feature.

Bill Venners:

A difference I have seen is: Data Sharing between Closure objects and methods. I think I can be in

Annual non-Closure structure of the block is put in any required environmental data as a parameter

Hand, but block is just a copy of environmental data, not true closure. It doesn't

Share environmental data. Sharing is a different place in closure and ordinary traditional functions.

YUKIHIRO MATSUMOTO:

Yes, sharing allows you to do some interesting code demonstrations, but I think it works for programmers and

It is useless to be useless. This is not too big, such as ordinary complexes like Java's internal classes.

System, in many occasions. But through Ruby's clousure structure, I hope to express my intention to LISP culture.

Qiu Haifeng (EIFFELQIU)

Mail: Eiffel_Q@163.comwww: http://www.eiffelqiu.comWeblog: http://mulder.5d.cn

转载请注明原文地址:https://www.9cbs.com/read-23497.html

New Post(0)