简体   繁体   English

使用keysAndValueDo(smalltalk)将SortedCollection打印到屏幕上

[英]Print a SortedCollection to screen using keysAndValueDo (smalltalk)

您好,我正在学习并且是Smalltalk的新手,我正在尝试将我的SortedCollection打印到屏幕上以尝试使用keysAndValueDo,但我不确定这样做如何,如果有人可以给我一个通用的例子,那会很好

The following example works with Pharo Smalltalk, other Smalltalk implementation might work similar. 以下示例可用于Pharo Smalltalk,其他Smalltalk实现可能也类似。

  1. First, look at existing print methods as examples. 首先,以现有的打印方法为例。 In case of SortedCollection , you find them in the printing protocol of its superclass Collection . 如果是SortedCollection ,则可以在其超类Collectionprinting协议中找到它们。

  2. You will find that the printing of elements is defined in printElementsOn: . 您会发现元素的打印是在printElementsOn:定义的。 So you could overwrite this method in SortedCollection . 因此,您可以在SortedCollection覆盖此方法。

Here is a printElementsOn: method that will use keysAndValuesDo: , as you were asking for: 这是一个printElementsOn:方法,该方法将按照您的要求使用keysAndValuesDo: ::

printElementsOn: aStream
    aStream nextPut: $(.
    self keysAndValuesDo: [:key :value | 
        aStream
            nextPut: $(;
            print: key;
            nextPut: $:;
            space;
            print: value;
            nextPut: $)].
    aStream nextPut: $)

Now a collection that before printed: 现在打印之前的收藏:

"a SortedCollection(1 2 3 3 5 10)" “一个SortedCollection(1 2 3 3 5 10)”

will print: 将打印:

"a SortedCollection((1: 1)(2: 2)(3: 3)(4: 3)(5: 5)(6: 10))" “一个SortedCollection((1:1)(2:2)(3:3)(4:3)(5:5)(6:10))”

Part 1 - Displaying to the screen 第1部分-在屏幕上显示
The most straight forward way to print to the screen in a GUI-based Smalltalk is to use the message: 在基于GUI的Smalltalk中打印到屏幕的最直接的方法是使用以下消息:
Transcript show: 'some text'
(The Transcript is a system object that displays into a scrollable window). (“成绩单”是显示在可滚动窗口中的系统对象)。

To make sure there is a newline before each line of display text, we send the message cr to the Transcript 为了确保每行显示文本之前都有换行符,我们将消息cr发送到“文字记录”

Transcript cr.
Transcript show: 'some text'.

A shorthand method, that saves us re-typing Transcript over and over, is to send Transcript a series of messages one after another. 一种速记方法可以使我们一次又一次地向Transcript发送一系列消息,从而省去了一遍又一遍地重新键入Transcript This is called a message cascade . 这称为消息级联 Each time we end a message in ; 每次我们在留言结束; it means send to the same receiver as the last message was sent to . 这意味着发送到与最后一条消息发送到相同的接收者

We can then shorten this again, as Smalltalk pays no attention to newlines in expressions. 然后,我们可以再次缩短它,因为Smalltalk不会关注表达式中的换行符。

The final display message cascade becomes: 最终的显示消息级联变为:
Transcript cr; show: 'some text'.

Part 2: Enumerating aSortedCollection using keysAndValuesDo: This keyword message is SequencableCollectionand its method header is: 第2部分:使用keysAndValuesDo枚举aSortedCollection:此关键字消息为SequencableCollection,其方法标头为:
keysAndValuesDo: aBlock "Enumerate the receiver with all the keys (aka indices) and values."

(It works the same way in Dolphin, and in Squeak and its derivatives, Pharo and Cuis). (它在Dolphin,Squeak及其衍生物Pharo和Cuis中的工作方式相同)。

The keyword message keysAndValuesDo: takes a block argument. 关键字message keysAndValuesDo:带有一个块参数。
A block is an anonymous object, with one method. 块是具有一种方法的匿名对象。 Its method is defined between a matched pair of square brackets - a [ ... ] pair. 它的方法定义在一对匹配的方括号- [ ... ]对之间。

In this case, we need a local variable in the block for the key of each element of the collection, and another local variable for the value of each element. 在这种情况下,我们需要在块中使用一个局部变量作为集合中每个元素的键,并使用另一个局部变量作为每个元素的值。

We can call them anything we like, and in this case, it is the order that they appear in that is important. 我们可以将它们命名为我们喜欢的任何东西,在这种情况下,它们出现的顺序很重要。 keysAndValuesDo: will put the element's key into the first local variable in the block, and will put the element's value into the second local variable in the block. keysAndValuesDo:将元素的键放入块中的第一个局部变量,并将元素的值放入块中的第二个局部变量。

Local variables in a block are declared at the start of the block, and each variable name is identified by prefixing it with : . 块中的局部变量在该块的开始处声明,并且每个变量名都以:作为前缀来标识。 The local variable declarations are ended with a | 局部变量声明以|结尾| .

The block then looks like 然后,该块看起来像
[:local1 :local2 | "do something for each element, with the key in local1 and the value in local2" ]

I prefer meaningful local variable names, so I'll use eachKey and eachValue . 我更喜欢有意义的局部变量名称,因此我将使用eachKeyeachValue

Part 3: Putting it all together 第3部分:将所有内容放在一起
To enumerate through mySortedCollection 通过mySortedCollection枚举

"Declare the mySortedCollection variable"
|mySortedCollection| 

"Initialise the collection"
mySortedCollection := SortedCollection new.  

"add in some data to the collection"
mySortedCollection add: ('First') ;
                   add: ('Second') ;
                   add: ('Third').

"Enumerate through the collection, displaying to the Transcript window"
mySortedCollection keysAndValuesDo: 
   [:eachKey :eachValue | 
    Transcript cr; show: eachKey; show: ' '; show: eachValue
] .

Paste the code into a Workspace (known as a Playground in Pharo, from version 4.0 onwards). 将代码粘贴到工作区(从4.0版开始,在Pharo中称为Playground)。 Select the text. 选择文字。 Once selected, right-click (on a two or three button mouse) and select "Do it" from the menu. 选择后,右键单击(用两三个鼠标单击),然后从菜单中选择“执行”。 Or use Ctrl-d as a keyboard shortcut. 或使用Ctrl-d作为键盘快捷键。 (The exact chording key may vary on your platform) (确切的和弦键可能会因您的平台而异)

Final notes 最后的笔记
In a SortedCollection or an OrderedCollection, the key is the index. 在SortedCollection或OrderedCollection中,键是索引。 The value is what is stored at element[index]. 该值是存储在element [index]处的值。

In a Dictionary, the key of the element is the key, and the value of the element is the value. 在字典中,元素的键是键,元素的值是值。

SortedCollections are sorted in order of the element values, according to the definition of the collections sort block . 根据集合sort块的定义,SortedCollections按元素值的顺序排序 In the absence of a custom sort block, they will be added in ascending order. 在没有自定义排序块的情况下,它们将以升序添加。 'First' , 'Second' and 'Third' are, coincidentally, in alphabetical order. 巧合'Third'是, 'First''Second''Third'按字母顺序排列。 It happens to work out nicely in this example. 在此示例中,它工作得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM