The Inbox: Collections-ct.947.mcz

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

The Inbox: Collections-ct.947.mcz

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

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

Name: Collections-ct.947
Author: ct
Time: 10 June 2021, 8:40:58.780588 pm
UUID: cb2cdf00-2a3a-5d46-bf15-60255480c1fa
Ancestors: Collections-eem.944

Proposal: Adds sort function that uses a boolean compare block such as [:a :b | a <= b].

Usage examples:

        squotVersions sorted: [:a :b | historyWalker shouldVisit: a before: b] asCompareSortFunction.
        #(true nil 42) sorted: #compareSafely: asCompareSortFunction.

=============== Diff against Collections-eem.944 ===============

Item was added:
+ ----- Method: BlockClosure>>asCompareSortFunction (in category '*Collections-SortFunctions-converting') -----
+ asCompareSortFunction
+
+ ^ CompareBlockFunction usingBlock: self!

Item was changed:
  SortFunction subclass: #CollatorBlockFunction
  instanceVariableNames: 'collatorBlock'
  classVariableNames: ''
  poolDictionaries: ''
  category: 'Collections-SortFunctions'!
 
+ !CollatorBlockFunction commentStamp: 'ct 6/10/2021 20:38' prior: 0!
- !CollatorBlockFunction commentStamp: 'nice 11/5/2017 22:57' prior: 0!
  A CollatorBlockFunction is a special SortFunction using a dyadic block to collate objects.
 
  Instance Variables
 
+ collatorBlock <Block> a dyadic block that must return a -1, 0, or 1.!
- collator <Block> a dyadic block that must return a -1, 0, or 1.!

Item was added:
+ SortFunction subclass: #CompareBlockFunction
+ instanceVariableNames: 'compareBlock'
+ classVariableNames: ''
+ poolDictionaries: ''
+ category: 'Collections-SortFunctions'!
+
+ !CompareBlockFunction commentStamp: 'ct 6/10/2021 20:38' prior: 0!
+ A CompareBlockFunction is a special SortFunction using a dyadic block to compare objects. The default compare logic is the lower-equals function (#<=).
+
+ Instance Variables
+
+ compareBlock <Block> a dyadic block that must return true iff the first argument should not appear after the second , otherwise false.!

Item was added:
+ ----- Method: CompareBlockFunction class>>usingBlock: (in category 'instance creation') -----
+ usingBlock: twoArgsBlock
+
+ ^ self new compareBlock: twoArgsBlock!

Item was added:
+ ----- Method: CompareBlockFunction>>collate:with: (in category 'evaluating') -----
+ collate: anObject with: anotherObject
+
+ ^ (compareBlock value: anObject value: anotherObject)
+ ifTrue: [-1]
+ ifFalse: [1]!

Item was added:
+ ----- Method: CompareBlockFunction>>compareBlock (in category 'accessing') -----
+ compareBlock
+
+ ^ compareBlock!

Item was added:
+ ----- Method: CompareBlockFunction>>compareBlock: (in category 'accessing') -----
+ compareBlock: aBlock
+
+ compareBlock := aBlock.!

Item was added:
+ ----- Method: Symbol>>asCompareSortFunction (in category '*Collections-SortFunctions-converting') -----
+ asCompareSortFunction
+
+ ^ CompareBlockFunction usingBlock: self!


Reply | Threaded
Open this post in threaded view
|

Re: The Inbox: Collections-ct.947.mcz

Levente Uzonyi
Hi Christoph,

On Thu, 10 Jun 2021, [hidden email] wrote:

> A new version of Collections was added to project The Inbox:
> http://source.squeak.org/inbox/Collections-ct.947.mcz
>
> ==================== Summary ====================
>
> Name: Collections-ct.947
> Author: ct
> Time: 10 June 2021, 8:40:58.780588 pm
> UUID: cb2cdf00-2a3a-5d46-bf15-60255480c1fa
> Ancestors: Collections-eem.944
>
> Proposal: Adds sort function that uses a boolean compare block such as [:a :b | a <= b].
>
> Usage examples:
>
> squotVersions sorted: [:a :b | historyWalker shouldVisit: a before: b] asCompareSortFunction.
> #(true nil 42) sorted: #compareSafely: asCompareSortFunction.

#sort: and #sorted: expect an argument which responds to #value:value:
with a boolean, so both of those examples work if you don't send
#asCompareSortFunction to the block.

However, #asCompareSortFunction may be useful if you want to create
chained sort functions. I'm not sure about the name though.


Levente

Reply | Threaded
Open this post in threaded view
|

Re: The Inbox: Collections-ct.947.mcz

Christoph Thiede
Hi Levente,

> #sort: and #sorted: expect an argument which responds to #value:value: with a boolean, so both of those examples work if you don't send #asCompareSortFunction to the block.
>
> However, #asCompareSortFunction may be useful if you want to create chained sort functions. I'm not sure about the name though.

Ouch! I did not see the wood for the trees. :-) But yes, if you would like to, for example, invert the order of a compare sort block, the protocols would not match ... So I guess this would still be relevant.

Best,
Christoph

Carpe Squeak!
Reply | Threaded
Open this post in threaded view
|

Re: The Inbox: Collections-ct.947.mcz

Levente Uzonyi
On Thu, 10 Jun 2021, [hidden email] wrote:

> Hi Levente,
>
>> #sort: and #sorted: expect an argument which responds to #value:value: with a boolean, so both of those examples work if you don't send #asCompareSortFunction to the block.
>>
>> However, #asCompareSortFunction may be useful if you want to create chained sort functions. I'm not sure about the name though.
>
> Ouch! I did not see the wood for the trees. :-) But yes, if you would like to, for example, invert the order of a compare sort block, the protocols would not match ... So I guess this would still be relevant.

It's not clear to me what you mean by "invert the order of a compare sort
block.".
However, I see one problem with the implementation. It evaluates the block
but lies about equality. It only returns -1 or 1, never 0. Which means,
chaining is impossible.
Here's an example:

#((1 a) (1 b)) sorted: [ :a | a first ] ascending, [ :a | a second ] descending
"==>  #(#(1 #b) #(1 #a)) which is fine"

but

#((1 a) (1 b)) sorted: [ :a :b | a first <= b first ] asCompareSortFunction, [ :a | a second ] descending.
"==>  #(#(1 #a) #(1 #b)) which is not what I would expect"


Levente