I'm sorry, it appears that I failed to explain the question
well enough.�� I thought I'd explained earlier.
successor: target
�� ^(self select: [:each | target < each]) min
is trivial.�� What's wrong with it is that it allocates
an intermediate collection and takes two passes.
FIXING that is is also trivial.
successor: target
�� ^(self virtualSelect: [:each | target < each]) min
���������������� ^^^^^^^
This does allocate something, but it's just a few words,
and a single traversal is one.
In other languages/contexts we'd be talking about
loop fusion/listless transformation/deforestation.
It is my understanding that using Transducers would
get me *this* level of improvement.
The problem is that this is still a linear-time
algorithm.�� If you take advantage of the order in
a SortedCollection or SortedSet,it can take logarithmic
time.�� When SortedSet is implemented as a splay tree
-- as it is in my library -- iterating over all its
elements using #successor: is amortised CONSTANT time
per element.�� So we need THREE algorithms:
��- worst case O(n)�� select+min
��- worst case O(lg n)�� binary search
��- amortised O(1)�� splaying
and we want the algorithm selection to be
���� A U T O M A T I C.
That's the point of using an object-oriented language.
I say what I want done and the receiver decides how to
do it.�� Anything where I have to write different
calling code depending on the structure of the receiver
doesn't count as a solution.
Now we come to the heart of the problem.
The binary search algorithm is NOT a special case
of the linear search algorithm.�� It is not made of
pieces that can be related to the parts of the linear
search algorithm.
The splaying algorithm is NOT a special case of the
linear search algorithm OR the binary search algorithm.
It is not made of pieces that can be related to their
parts.
So *IF* I want automatic selection of an appropriate
algorithm, then I have to rely on inheritance and
overriding, and in order to do that I have to have
a named method that *can* be overridden, and at that
point I'm no longer building a transducer out of
pluggable pieces.
So that's the point of this exercise.
How do we get
(a) composition of transducers out of pluggable parts
AND
(b) automatic selection of appropriate algorithms