[Pharo-project] Fwd: trying to understand loops in pharo

Nicolas Cellier nicolas.cellier.aka.nice at gmail.com
Sun Oct 16 16:33:04 EDT 2011


Ah, I see, this sound appealing.
But I would object this: (I'm speaking of do:, not only to:do:)
1) what the last value mean for unordered collections?
2) for a SequenceableCollection, the behavior is already
(someCollection inject: nil into: [:void :each | aBlock value: each])
3) returning the last value in all implementors of #do: would cost
complexification of code and possibly slow down (non clean blocks)

Is it really useful?

Nicolas

2011/10/16 Stéphane Ducasse <stephane.ducasse at inria.fr>:
> for me the last expression of the block.
>
> Stef
>>>> Which return value would be useful?
>>>
>>> That's not the right question.  IMO the compiler should answer the same
>>> result as the non-inlined one when the return value is asked for.  Arguably
>>> the non-inline version needs to be commented to specify that it returns the
>>> value it does (in this case self), and that the compiler's optimiser
>>> reflects this.
>>>
>>
>> Agree, but I was wondering what useful object this common answer could be?
>>
>>>>
>>>> Nicolas
>>>>
>>>> 2011/10/16 Stéphane Ducasse <stephane.ducasse at inria.fr>:
>>>>> A friend of mine ask me the following
>>>>>
>>>>> |result|
>>>>> result := String new.
>>>>> 1 to: 10 do: [:n | result := result, n printString, ' '].
>>>>> I am trying to do or print this code. It prints 'nil' instead of a
>>>>> collection. Its an example from PBE book.
>>>>> do you know the reason?
>>>>> although this one's working:
>>>>> result := String new.
>>>>> (1 to: 10) do: [:n | result := result, n printString, ' '].
>>>>>
>>>>> I checked the implementation
>>>>> Interval>>do: aBlock
>>>>> "Evaluate aBlock for each value of the interval.
>>>>> Implementation note: instead of repeatedly incrementing the value
>>>>> aValue := aValue + step.
>>>>> until stop is reached,
>>>>> We prefer to recompute value from start
>>>>> aValue := start + (index * step).
>>>>> This is better for floating points accuracy, while not degrading Integer
>>>>> and
>>>>> Fraction speed too much.
>>>>> Moreover, this is consistent with methods #at: and #size"
>>>>> | aValue index size |
>>>>> index := 0.
>>>>> size := self size.
>>>>> [index < size]
>>>>> whileTrue: [aValue := start + (index * step).
>>>>> index := index + 1.
>>>>> aBlock value: aValue]
>>>>>
>>>>> Number>>to: stop do: aBlock
>>>>> "Normally compiled in-line, and therefore not overridable.
>>>>> Evaluate aBlock for each element of the interval (self to: stop by: 1)."
>>>>> | nextValue |
>>>>> nextValue := self.
>>>>> [nextValue <= stop]
>>>>> whileTrue:
>>>>> [aBlock value: nextValue.
>>>>> nextValue := nextValue + 1]
>>>>>
>>>>> any further idea than := value is different from value:
>>>>> Stef
>>>>
>>>
>>>
>>>
>>> --
>>> best,
>>> Eliot
>>>
>>
>
>
>




More information about the Pharo-dev mailing list