https://forum.world.st/How-can-I-make-this-more-OOP-tp5121729p5121888.html
I take Sean's point. However, while I agree that practising using
polymorphism instead of 'if' is a good idea, I don't think that
*this* exercise is a good one for getting such practice.
Roelof is going through the Pharo track of Exercism.
This particular one is called 'flatten-array'.
The one concession to "hey, this is Pharo!" in the description
of the exercise is a veiled warning against using Pharo's
#flattened/#flattenOn:, which
(a) does not add anything to Object or UndefinedObject
(just Collection>>flattened and Collection>>flattenOn:)
(b) does not make a special case of nil
(c) DOES make a special case of strings, treating them as atomic.
Possibly the most challenging part for me in this exercise was
trying to figure out "what does <list> mean for Smalltalk here?"
In Pharo's existing #flattened, why does a String count as atomic
but a Semphore is just another collection?
This is a Lisp programming exercise from the early 1960s.
It had unpleasant ambiguities back then and it has not got
better since.
If this were rewritten as
"Make a Composite implementation of binary search trees
with a class for empty trees, a class for non-empty ones,
and perhaps a class for singleton ones.
Implement #do:, traversing the elements in increasing order.
Use this to implement #asOrderedCollection."
then that *would* be a good exercise for using polymorphism
instead of conditionals.
BST ()
asOrderedCollection
^OrderedCollection new in: [:coll |
self do: [:each | coll addLast: each].
coll]
EmptyBST ()
do: aBlock
"Nothing to do."
NonemptyBST (less key greater)
do: aBlock
less do: aBlock.
aBlock value: key.
greater do: aBlock.