The Inbox: Collections-EG.908.mcz

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

The Inbox: Collections-EG.908.mcz

commits-2
A new version of Collections was added to project The Inbox:
http://source.squeak.org/inbox/Collections-EG.908.mcz

==================== Summary ====================

Name: Collections-EG.908
Author: EG
Time: 7 August 2020, 8:17:30.343164 pm
UUID: d02855a8-a339-4f1d-bede-89a50f07892e
Ancestors: Collections-eem.907

Changing  behavior of #peekBack to (correctly) return first element of underlying collection when stream position is 1.

=============== Diff against Collections-eem.907 ===============

Item was changed:
  ----- Method: PositionableStream>>peekBack (in category 'accessing') -----
  peekBack
  "Return the element at the previous position, without changing position.  Use indirect messages in case self is a StandardFileStream."
-
  | element |
  self position = 0 ifTrue: [self errorCantGoBack].
+ element := self back.
- self position = 1 ifTrue: [self position: 0.  ^ nil].
- self skip: -2.
- element := self next.
  self skip: 1.
  ^ element!


Reply | Threaded
Open this post in threaded view
|

Re: The Inbox: Collections-EG.908.mcz

David T. Lewis
The new tests and fix for PositionableStream look good, but there is a
failure now in RWBinaryOrTextStreamTest>>testPeekBack.

Dave

On Sat, Aug 08, 2020 at 12:17:32AM +0000, [hidden email] wrote:

> A new version of Collections was added to project The Inbox:
> http://source.squeak.org/inbox/Collections-EG.908.mcz
>
> ==================== Summary ====================
>
> Name: Collections-EG.908
> Author: EG
> Time: 7 August 2020, 8:17:30.343164 pm
> UUID: d02855a8-a339-4f1d-bede-89a50f07892e
> Ancestors: Collections-eem.907
>
> Changing  behavior of #peekBack to (correctly) return first element of underlying collection when stream position is 1.
>
> =============== Diff against Collections-eem.907 ===============
>
> Item was changed:
>   ----- Method: PositionableStream>>peekBack (in category 'accessing') -----
>   peekBack
>   "Return the element at the previous position, without changing position.  Use indirect messages in case self is a StandardFileStream."
> -
>   | element |
>   self position = 0 ifTrue: [self errorCantGoBack].
> + element := self back.
> - self position = 1 ifTrue: [self position: 0.  ^ nil].
> - self skip: -2.
> - element := self next.
>   self skip: 1.
>   ^ element!
>
>

Reply | Threaded
Open this post in threaded view
|

Re: The Inbox: Collections-EG.908.mcz

David T. Lewis
Actually, I think that the new ReadStreamTest>>testPeekBack is not in
agreement with the existing RWBinaryOrTextSteamTest>>testPeekBack.

The peekBack method was not in Squeak 3.6, but it appears in Squeak 3.8.
It is unused in the image, but it presumably is intended to support parsing
things from a stream.

If I understand correctly, peek means "show me the next element that
has not yet been read from the stream", and peekBack means "show me the
element before the one that was most recently read from the stream".
So I expect that peekBack would mean to skip back 2 from the current
position, not skip back 1.

Dave

On Fri, Aug 07, 2020 at 09:32:56PM -0400, David T. Lewis wrote:

> The new tests and fix for PositionableStream look good, but there is a
> failure now in RWBinaryOrTextStreamTest>>testPeekBack.
>
> Dave
>
> On Sat, Aug 08, 2020 at 12:17:32AM +0000, [hidden email] wrote:
> > A new version of Collections was added to project The Inbox:
> > http://source.squeak.org/inbox/Collections-EG.908.mcz
> >
> > ==================== Summary ====================
> >
> > Name: Collections-EG.908
> > Author: EG
> > Time: 7 August 2020, 8:17:30.343164 pm
> > UUID: d02855a8-a339-4f1d-bede-89a50f07892e
> > Ancestors: Collections-eem.907
> >
> > Changing  behavior of #peekBack to (correctly) return first element of underlying collection when stream position is 1.
> >
> > =============== Diff against Collections-eem.907 ===============
> >
> > Item was changed:
> >   ----- Method: PositionableStream>>peekBack (in category 'accessing') -----
> >   peekBack
> >   "Return the element at the previous position, without changing position.  Use indirect messages in case self is a StandardFileStream."
> > -
> >   | element |
> >   self position = 0 ifTrue: [self errorCantGoBack].
> > + element := self back.
> > - self position = 1 ifTrue: [self position: 0.  ^ nil].
> > - self skip: -2.
> > - element := self next.
> >   self skip: 1.
> >   ^ element!
> >
> >
>

Reply | Threaded
Open this post in threaded view
|

Re: The Inbox: Collections-EG.908.mcz

darth-cheney
Hi Dave,

On Fri, Aug 7, 2020 at 10:14 PM David T. Lewis <[hidden email]> wrote:
Actually, I think that the new ReadStreamTest>>testPeekBack is not in
agreement with the existing RWBinaryOrTextSteamTest>>testPeekBack.

Looks like I got too excited and neglected to run the full test suite, apologies!
 
If I understand correctly, peek means "show me the next element that
has not yet been read from the stream", and peekBack means "show me the
element before the one that was most recently read from the stream".
So I expect that peekBack would mean to skip back 2 from the current
position, not skip back 1.

I see what you are saying and that makes sense to me. My thinking was that stream positions are always "between" the elements, and that a "peek forward" just means "grab the element but don't advance the position. In that case, peeking backward would do the same in the reverse direction: look one element back but not decrement the position.

Because there are no senders I guess this isn't a big deal and we can trash the changes. It came up because I'm thinking of porting my refactor of GIFReadWriter and animated image parsing that I did for Pharo about a year ago. I use peekBack in a couple of places there but can definitely figure out another way to achieve the same result. (I'll save the GIF discussion for a later email because it's ... complicated).

Thanks!
 

Dave
--
Eric


Reply | Threaded
Open this post in threaded view
|

Re: The Inbox: Collections-EG.908.mcz

Levente Uzonyi
In reply to this post by David T. Lewis
Hi Dave,

On Fri, 7 Aug 2020, David T. Lewis wrote:

> Actually, I think that the new ReadStreamTest>>testPeekBack is not in
> agreement with the existing RWBinaryOrTextSteamTest>>testPeekBack.
>
> The peekBack method was not in Squeak 3.6, but it appears in Squeak 3.8.
> It is unused in the image, but it presumably is intended to support parsing
> things from a stream.
>
> If I understand correctly, peek means "show me the next element that
> has not yet been read from the stream", and peekBack means "show me the
> element before the one that was most recently read from the stream".
> So I expect that peekBack would mean to skip back 2 from the current
> position, not skip back 1.

Another interpretation of peek in the context of streams is to look
around the current position without consuming any elements.
#peek returns what #next would return without consuming any elements,
and following the same logic, #peekBack is expected to return what #back
would without affecting position.

That interpretation is what Eric implemented.

If we decide to stick with the previous implementation based on #oldBack,
which does not make any practical sense to me, then we'll need another
method to get the last read element, because #peekBack would return the
penultimate one.


Levente

>
> Dave
>
> On Fri, Aug 07, 2020 at 09:32:56PM -0400, David T. Lewis wrote:
>> The new tests and fix for PositionableStream look good, but there is a
>> failure now in RWBinaryOrTextStreamTest>>testPeekBack.
>>
>> Dave
>>
>> On Sat, Aug 08, 2020 at 12:17:32AM +0000, [hidden email] wrote:
>> > A new version of Collections was added to project The Inbox:
>> > http://source.squeak.org/inbox/Collections-EG.908.mcz
>> >
>> > ==================== Summary ====================
>> >
>> > Name: Collections-EG.908
>> > Author: EG
>> > Time: 7 August 2020, 8:17:30.343164 pm
>> > UUID: d02855a8-a339-4f1d-bede-89a50f07892e
>> > Ancestors: Collections-eem.907
>> >
>> > Changing  behavior of #peekBack to (correctly) return first element of underlying collection when stream position is 1.
>> >
>> > =============== Diff against Collections-eem.907 ===============
>> >
>> > Item was changed:
>> >   ----- Method: PositionableStream>>peekBack (in category 'accessing') -----
>> >   peekBack
>> >   "Return the element at the previous position, without changing position.  Use indirect messages in case self is a StandardFileStream."
>> > -
>> >   | element |
>> >   self position = 0 ifTrue: [self errorCantGoBack].
>> > + element := self back.
>> > - self position = 1 ifTrue: [self position: 0.  ^ nil].
>> > - self skip: -2.
>> > - element := self next.
>> >   self skip: 1.
>> >   ^ element!
>> >
>> >
>>

Reply | Threaded
Open this post in threaded view
|

Re: The Inbox: Collections-EG.908.mcz

Levente Uzonyi
In reply to this post by darth-cheney
Hi Eric,

On Fri, 7 Aug 2020, Eric Gade wrote:

> Hi Dave,
>
> On Fri, Aug 7, 2020 at 10:14 PM David T. Lewis <[hidden email]> wrote:
>       Actually, I think that the new ReadStreamTest>>testPeekBack is not in
>       agreement with the existing RWBinaryOrTextSteamTest>>testPeekBack.
>
>
> Looks like I got too excited and neglected to run the full test suite, apologies!
>  
>       If I understand correctly, peek means "show me the next element that
>       has not yet been read from the stream", and peekBack means "show me the
>       element before the one that was most recently read from the stream".
>       So I expect that peekBack would mean to skip back 2 from the current
>       position, not skip back 1.
>
>
> I see what you are saying and that makes sense to me. My thinking was that stream positions are always "between" the elements, and that a "peek forward" just means "grab the element but don't advance the position. In that
> case, peeking backward would do the same in the reverse direction: look one element back but not decrement the position.
>
> Because there are no senders I guess this isn't a big deal and we can trash the changes. It came up because I'm thinking of porting my refactor of GIFReadWriter and animated image parsing that I did for Pharo about a year
> ago. I use peekBack in a couple of places there but can definitely figure out another way to achieve the same result. (I'll save the GIF discussion for a later email because it's ... complicated).
That sounds really cool. I hope it fixes the bug Karl reported early this
year[1] (see the attached gif if you don't know). When I had a look at the
code, I felt like rewriting it would be easier than fixing it.

Now that I know the context of where you intend to use #peekBack, I
suggest you shouldn't use it but rather store what #next returned in a
variable.
That should make a significant difference in performance if used in a
tight loop.


Levente

[1] http://forum.world.st/BUG-GIFReadWriter-td5109760.html

>
> Thanks!
>  
>
>       Dave
>
> --
> Eric
>
>

Reply | Threaded
Open this post in threaded view
|

Re: The Inbox: Collections-EG.908.mcz

David T. Lewis
In reply to this post by Levente Uzonyi
Hi Levente,

On Sat, Aug 08, 2020 at 06:32:56PM +0200, Levente Uzonyi wrote:

> Hi Dave,
>
> On Fri, 7 Aug 2020, David T. Lewis wrote:
>
> >Actually, I think that the new ReadStreamTest>>testPeekBack is not in
> >agreement with the existing RWBinaryOrTextSteamTest>>testPeekBack.
> >
> >The peekBack method was not in Squeak 3.6, but it appears in Squeak 3.8.
> >It is unused in the image, but it presumably is intended to support parsing
> >things from a stream.
> >
> >If I understand correctly, peek means "show me the next element that
> >has not yet been read from the stream", and peekBack means "show me the
> >element before the one that was most recently read from the stream".
> >So I expect that peekBack would mean to skip back 2 from the current
> >position, not skip back 1.
>
> Another interpretation of peek in the context of streams is to look
> around the current position without consuming any elements.
> #peek returns what #next would return without consuming any elements,
> and following the same logic, #peekBack is expected to return what #back
> would without affecting position.
>
> That interpretation is what Eric implemented.

When I first looked at it, I had the same interpretation as Eric and you.

>
> If we decide to stick with the previous implementation based on #oldBack,
> which does not make any practical sense to me, then we'll need another
> method to get the last read element, because #peekBack would return the
> penultimate one.
>

You are right, it is inconsistent and confusing. But I wonder if there is
any use case for a method that just answers the most recently read item.

Sometimes I just look at Cuis when I want to find out how to do things right.
Here is what Cuis does:

  'ABCDEF' readStream next: 2; peekBack. ==> $B
  'ABCDEF' readStream next: 2; back. ==> $B
  'ABCDEF' readStream next: 2; oldPeekBack. ==> $A
 
  'ABCDEF' readStream next: 1; peekBack. ==> $A
  'ABCDEF' readStream next: 1; back. $A
  'ABCDEF' readStream next: 1; oldPeekBack. ==> nil
 
  'ABCDEF' readStream next: 0; peekBack. ==> Error: CantGoBack
  'ABCDEF' readStream next: 0; back. ==> Error: CantGoBack
  'ABCDEF' readStream next: 0; oldPeekBack. ==> Error: CantGoBack

In Cuis, there are no senders of #peekBack, and one sender of #oldPeekBack
(PositionableStream>>#backChunk).

I think this is consistent with what you are recommending, so maybe we
should just adopt the Cuis implmentations and update our tests accordingly.

Note that the #oldPeekBack in Cuis is like our current #peekBack, and is
still inconsistent with respect to whether it answers nil or raises an error,
which still should be fixed.

Dave

>
> Levente
>
> >
> >Dave
> >
> >On Fri, Aug 07, 2020 at 09:32:56PM -0400, David T. Lewis wrote:
> >>The new tests and fix for PositionableStream look good, but there is a
> >>failure now in RWBinaryOrTextStreamTest>>testPeekBack.
> >>
> >>Dave
> >>
> >>On Sat, Aug 08, 2020 at 12:17:32AM +0000, [hidden email] wrote:
> >>> A new version of Collections was added to project The Inbox:
> >>> http://source.squeak.org/inbox/Collections-EG.908.mcz
> >>>
> >>> ==================== Summary ====================
> >>>
> >>> Name: Collections-EG.908
> >>> Author: EG
> >>> Time: 7 August 2020, 8:17:30.343164 pm
> >>> UUID: d02855a8-a339-4f1d-bede-89a50f07892e
> >>> Ancestors: Collections-eem.907
> >>>
> >>> Changing  behavior of #peekBack to (correctly) return first element of
> >>underlying collection when stream position is 1.
> >>>
> >>> =============== Diff against Collections-eem.907 ===============
> >>>
> >>> Item was changed:
> >>>   ----- Method: PositionableStream>>peekBack (in category 'accessing')
> >>-----
> >>>   peekBack
> >>>   "Return the element at the previous position, without changing
> >>position.  Use indirect messages in case self is a StandardFileStream."
> >>> -
> >>>   | element |
> >>>   self position = 0 ifTrue: [self errorCantGoBack].
> >>> + element := self back.
> >>> - self position = 1 ifTrue: [self position: 0.  ^ nil].
> >>> - self skip: -2.
> >>> - element := self next.
> >>>   self skip: 1.
> >>>   ^ element!
> >>>
> >>>
> >>
>