简体   繁体   English

吱吱作响的小话,为什么有时简化的方法不起作用?

[英]Squeak Smalltalk, why sometimes the reduced method doesn't work?

(2332 / 2332) reduced 
(2332 / 2) reduced 
(2332 / 322) reduced  (1166/161)
(2332 / 3) reduced  (2332/3)
(2332 / 2432423) reduced  (2332/2432423)

Look at the above codes. 看上面的代码。 The first and second, when printed, do not work. 第一个和第二个在打印时不起作用。 The MessageNotUnderstood window pops up. 弹出MessageNotUnderstood窗口。 And the 3rd, 4th, 5th code are okay. 而且第3,第4,第5代码都可以。 Results come out right. 结果正确。

Why does the reduced method not work? 为什么reduced方法不起作用?

Is it because the reduced method fails to handle final results which are integers like Uko guesses ? 是否因为归约方法无法处理最终结果(如Uko猜测的整数)?

Fractions are reduced automatically in the / method. /方法中的分数会自动减少。 There is no need to send the reduced message. 无需发送reduced消息。

Eg if you print the result of 例如,如果您打印的结果

2 / 4

you get the reduced (1/2) automatically. 您会自动获得缩小的(1/2)

If you print the result of 如果打印结果

2332 / 2332

it is reduced to 1 which is not a Fraction, but an Integer, and Integers do not understand the reduced message. 它减少为1不是分数,而是整数,并且整数不理解reduced消息。 That's why you get an error. 这就是为什么您会得到一个错误。

The only case when a Fraction is not automatically reduced is when you create it manually, as in 分数不会自动减少的唯一情况是手动创建分数,如

Fraction numerator: 2 denominator: 4

which will answer the non-reduced (2/4) . 这将回答未归约(2/4) But in normal arithmetic expressions you never need to send reduced . 但是在普通的算术表达式中,您无需发送reduced

The error occurs because by default, the Integer class does not understand the message reduced in Squeak. 发生错误是因为默认情况下, Integer类不理解Squeak中reduced的消息。 This despite members of Squeak's Integer class being fractions. 尽管Squeak的Integer类的成员都是分数。

5 isFraction "returns True"

The wonderful thing about Smalltalk is that if something does not work the way you want, you can change it. Smalltalk的妙处在于,如果某些事情无法按您想要的方式工作,则可以对其进行更改。 So if an Integer does not respond to the message reduced and you want it to, then you can add a reduced method to Integer with the expected behavior: 因此,如果Integer不响应reduced的消息而您希望它响应,则可以使用预期的行为向Integer添加一个reduced方法:

reduced
    "treat an integer like a fraction"
    ^ self

Adding methods to Classes is the way Smalltalk makes it easy to write expressive programs. 将方法添加到类是Smalltalk简化编写表达性程序的方式。 For example, Fractions in GNU Smalltalk understand the message reduce but not the message reduced available in Squeak. 例如,GNU Smalltalk中的Fractions理解消息reduce但Squeak中不可用消息reduced Rather than trying to remember a meaningless difference, the programmer can simply make reduced available to fractions in GNU Smalltalk: 程序员不必试图记住无意义的区别,而可以简单地将reduced提供给GNU Smalltalk中的分数:

Fraction extend [
  "I am a synonym for reduce" 
  reduced [
    ^ self reduce
  ]
]

Likewise one can extend Fraction in Squeak to have a reduce method: 同样,可以将Squeak中的Fraction扩展为reduce方法:

reduce
  "I am a synonym for reduced"
   ^ self reduced

The designers of Smalltalk made a language that let's programmers express themselves in the way that they think about the problem. Smalltalk的设计师设计了一种语言,使程序员可以以思考问题的方式来表达自己。

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

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