Hi Roelof,
Polymorphism is the answer.
Object >> flattenInto: result
result nextPut: anObject
UndefinedObject >> flattenInto: result
^self
Collection >> flattenInto: result
self do: [ :item | item flattenInto: result ]
flatten: anObject
^ (OrderedCollection streamContents: [ :stream | anObject flattenInto:
stream ]) asArray
Noury
> On 13 Sep 2020, at 11:26, Roelof Wobben via Pharo-users
> <[email protected]> wrote:
>
> Hello,
>
> I know that OOP is not asking a object what it is but I have to flatten a
> array so I did this :
>
> flattenArray: aCollection
> ^ (OrderedCollection
> streamContents: [ :stream | self flatten: aCollection into: stream ])
> asArray
>
> flatten: anObject into: result
> ^ anObject isCollection
> ifTrue: [ anObject do: [ :item | self flatten: item into: result ] ]
> ifFalse: [ anObject ifNotNil: [ result nextPut: anObject ] ]
>
> The name flattenArray is given bij exercism.
>
> Now I wonder how I can make this more a OOP solution.
>
> Can someone give me some hints or some examples ?
>
> Roelof
>