Andres Valloud schrieb:
> Here's a more permlink to the messages...
>
>
http://groups.google.com/group/comp.lang.smalltalk/browse_thread/thread/3c47ffb41e6433d3/d21a50cbcb3a49de?q=ANSI&lnk=nl&>
> FIrst, things like 'abcd' asSortedCollection includes: 5 would fail if
> includes: used binary search because the argument may not be comparable
> to what is inside the collection. And wrapping the sort block in an
> exception handler may not work either:
>
> " [...] consider the following:
> | coll |
> coll := ((1 to: 10) collect: [:each | 2 raisedTo: each])
> asSortedCollection: [:a :b | a highBit <= b highBit].
> coll allSatisfy: [:each | coll includes: each asFloat]
> This should return true by the ANSI standard. However, it will raise an
> error if you try to look for the floating point value using the sort block.
>
> Also, does [binary search] work for the case where the sort block
> doesn't uniquely
> distinguish items?
> | coll |
> coll := (1 to: 1000) asSortedCollection: [:a :b | true].
> coll allSatisfy: [:each | coll includes: each]
> In this case, [includes:] has to linearly look through the whole
> collection for each element. "
thank you both for the explanation and links.
Slightly related:
Here at work we rarely use #asSortedCollection: [:a :b | ... ]. Instead,
we usually use a custom extension, #sortedBy: [ : a | ... ], which
returns a comparable value. There are two reasons for doing this:
- sorting is usually done by computing a comparable value based on each
item of the collection, so the expression to compute the comparable
value is the same for a and b.
- computing the comparable value is often expensive, so #sortedBy: first
computes the values for all elements and then uses the precomputed
values to sort the collection.
Maybe it would be useful to have a method similar to #sortedBy: in the
base image, in two flavors (with and without precomputation).
_______________________________________________
vwnc mailing list
vwnc@...
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc