[squeak-dev] Review Request: Bag printString
christoph.thiede at student.hpi.uni-potsdam.de
christoph.thiede at student.hpi.uni-potsdam.de
Fri Mar 25 19:29:35 UTC 2022
Hi David, hi Marcel,
these are very good points. Please find the next iteration of the patch attached. Instead of ' (n)', I decided on '^n' (common mathm. multiset notation) as I believe that even more brackets would be too confusing. Many printStrings already use them to display a hash or something else.
What do you think? :-)
Best,
Christoph
=============== Summary ===============
Change Set: Bag printString
Date: 26 February 2022
Author: Christoph Thiede
This changeset customizes the Bag printString to no longer emit every occurence of each element repeatedly.
Example:
{10 @ 10. 10 @ 10. 20 @ 20. 20 @ 20. 20 @ 20. 30 @ 30. 30 @ 30. 40 @ 40} asBag. 'a Bag(10 at 10^2 20 at 20^3 30 at 30^2 40 at 40)'.
Thanks to Robert (rhi) for the hint!
Revision: Do not print the number for items with 1 occurence. Instead of '->', denote counts by '^', following the mathematical notiation of multisets.
=============== Diff ===============
Bag>>printElementsOn: {printing} · ct 3/25/2022 20:21
+ printElementsOn: aStream
+
+ aStream nextPut: $(.
+ contents size > 100
+ ifTrue: [aStream nextPutAll: 'size '.
+ self size printOn: aStream]
+ ifFalse: [contents keysInOrder
+ do: [:key | | count |
+ aStream print: key.
+ (count := contents at: key) > 1 ifTrue: [
+ aStream
+ nextPut: $^;
+ print: count]]
+ separatedBy: [aStream space]].
+ aStream nextPut: $)
BagTest>>testPrintString {tests} · ct 3/25/2022 20:24
+ testPrintString
+
+ | bag |
+ bag := Bag new.
+ bag add: $1 withOccurrences: 5.
+ bag add: '2' withOccurrences: 1.
+ bag add: self withOccurrences: 3.
+
+ self assert: 'a Bag(BagTest>>#testPrintString^3 ''2'' $1^5)' equals: bag printString.
Dictionary>>printElementsOn: {printing} · ct 2/26/2022 20:28 (changed)
printElementsOn: aStream
aStream nextPut: $(.
self size > 100
ifTrue: [aStream nextPutAll: 'size '.
self size printOn: aStream]
ifFalse: [self keysInOrder
do: [:key | aStream print: key;
nextPutAll: '->';
- print: (self at: key);
- space]].
+ print: (self at: key)]
+ separatedBy: [aStream space]].
aStream nextPut: $)
---
Sent from Squeak Inbox Talk
On 2022-02-28T09:17:04+01:00, marcel.taeumel at hpi.de wrote:
> Hi Christoph --
>
> I have to agree with Dave. Can we find a threshold when it makes sense to show the counts of each element? Maybe drop the "1" entirely and only show "->n" for n > 1? Maybe don't use "->" but parentheses? And maybe check with objects that have a more complex printString on their own. Hmm...
>
> aBag(10 at 10 (2) 20 at 20 (3) ...)
>
> (Also make sure that the printString is kind of as fast as before. Thanks.)
>
> Best,
> Marcel
> Am 26.02.2022 23:08:06 schrieb David T. Lewis <lewis at mail.msen.com>:
> This seems easier to read for the case of a Bag containing
> many copies of the same entry, and harder to read for the
> case of a Bag containing mostly different entries. I am not
> sure which is the more common scenario.
>
> The original printString hides the implementation, which seems
> like a good thing.
>
> Either approach would be fine for me, although I think I slightly
> prefer the original version.
>
> Dave
>
> On Sat, Feb 26, 2022 at 08:49:48PM +0100, christoph.thiede at student.hpi.uni-potsdam.de wrote:
> > =============== Summary ===============
> >
> > Change Set:????????Bag printString
> > Date:????????????26 February 2022
> > Author:????????????Christoph Thiede
> >
> > This changeset customizes the Bag printString to no longer emit every occurence of each element repeatedly.
> >
> > Example:
> > ????{10 @ 10. 10 @ 10. 20 @ 20. 20 @ 20. 20 @ 20. 30 @ 30. 30 @ 30. 40 @ 40} asBag. 'a Bag(10 at 10->2 20 at 20->3 30 at 30->2 40 at 40->1)'.
> >
> > Thanks to Robert (rhi) for the hint!
> >
> > =============== Diff ===============
> >
> > Bag>>printElementsOn: {printing} ? ct 2/26/2022 20:26
> > + printElementsOn: aStream
> > +
> > + ????contents printElementsOn: aStream.
> >
> > BagTest>>testPrintString {tests} ? ct 2/26/2022 20:25
> > + testPrintString
> > +
> > + ????| bag |
> > + ????bag := Bag new.
> > + ????bag add: '1' withOccurrences: 5.
> > + ????bag add: '2' withOccurrences: 1.
> > + ????bag add: '3' withOccurrences: 3.
> > + ????
> > + ????self assert: 'a Bag(''1''->5 ''2''->1 ''3''->3)' equals: bag printString.
> >
> > Dictionary>>printElementsOn: {printing} ? ct 2/26/2022 20:28 (changed)
> > printElementsOn: aStream
> > ????aStream nextPut: $(.
> > ????self size > 100
> > ????????ifTrue: [aStream nextPutAll: 'size '.
> > ????????????self size printOn: aStream]
> > ????????ifFalse: [self keysInOrder
> > ????????????????do: [:key | aStream print: key;
> > ???????????????????????? nextPutAll: '->';????????????????
> > - ???????????????????????? print: (self at: key);
> > - ???????????????????????? space]].
> > + ???????????????????????? print: (self at: key)]
> > + ????????????????separatedBy: [aStream space]].
> > ????aStream nextPut: $)
> >
> > ---
> > Sent from Squeak Inbox Talk
> > ["Bag printString.1.cs"]
>
> >
>
>
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20220228/5e79ee82/attachment.html>
>
>
["Bag printString.2.cs"]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20220325/abaafc6b/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Bag printString.2.cs
Type: application/octet-stream
Size: 1736 bytes
Desc: not available
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20220325/abaafc6b/attachment.obj>
More information about the Squeak-dev
mailing list
|