Google 網路論壇不再支援新的 Usenet 貼文或訂閱項目,但過往內容仍可供查看。

Block reversed

瀏覽次數:0 次
跳到第一則未讀訊息

Howard Oh

未讀,
2006年6月9日 下午4:58:212006/6/9
收件者:
Block is one of the reasons that makes Smalltalk so lovable. But
sometimes, block can make expressions very hard to read, because we
human read parameter object first and then read inside the block just
like Smalltalk runtime would do. This makes our reading cursor jump to
right end and then back to left.

A guick example that can depict this point can be...

[:x| x sqrt ] value: ([:x| x + 5 ] value: ([:x| x * x ] value: 2))

An idea came up to me a few minutes ago,

Define a method for Object

------------------------------------------------------------------------------------
Object>>--> aBlock

^aBlock value: self
------------------------------------------------------------------------------------

This new method can transform the example expression to...

2 --> [:x| x * x ] --> [:x| x + 5 ] --> [:x| x sqrt ]

Have a good one
Howard

Chris Uppal

未讀,
2006年6月10日 清晨7:00:552006/6/10
收件者:
Howard,

> [:x| x sqrt ] value: ([:x| x + 5 ] value: ([:x| x * x ] value: 2))
>

> This new method can transform the example expression to...
>
> 2 --> [:x| x * x ] --> [:x| x + 5 ] --> [:x| x sqrt ]

I like that. Very nice :)

-- chris


Wolfgang Eder

未讀,
2006年7月5日 上午9:44:172006/7/5
收件者:
Howard,
I agree that the --> is easier to read.
However, I am not sure why you want to use
blocks this way in the first place?

| x y z |
x := 2.
y := x * x.
z := y + 5.
z sqrt

or even:

| x |
x := 2.
x := x * x.
x := x + 5.
x sqrt

Cheers,
Wolfgang

0 則新訊息