It would help to know which version of Pharo you are using. #(1 2 3) flatten raises an exception in Pharo 9 because there is no #flatten in Array's protocol. Collection has #flatCollect: -- #gather: was not a terribly helpful name, -- but #flatCollect: is horribly misleading. #flatCollect:as: -- A misplaced method, should be -- Collection class >> withAll: aCollection gather: aBlock #flatCollectAsSet: -- a trivial special case of #flatCollect:as:, -- not clear why this special case exists. #flattenOn: -- aStream nextPutAll: self flattened -- but without creating the intermediate data structure #flattened -- a curiously twisted "flatten" that does not work -- at all for non-collections.
#(1 2 3) flattened => #(1 2 3) 1 flattened => BOOM! By the way, we cannot blame the cruftiness of #flattened on 40 years of history. #flatten/#flattened only entered Squeak about 7 years ago and it was BORN crufty. I mistakenly thought Squeak and Pharo agreed on what it meant. They do not. Squeak: puts it on SequenceableCollection and Stream. Pharo : puts it on Collection (more general) and not Stream (less so). Squeak: treats stream elements like nested collections. Pharo : doesn't. So my earlier claim bears repeating: of the Smalltalk systems that have some kind of flattening, no two agree on what it means. The one use I have ever had for flattening is to build up a large string by accumulating characters, strings, symbols, and sequences of the same in a sequence and then flattening the tree into a single string, which is something that Squeak and Pharo flatly refuse to do. One improvement I would like to see in Pharo 11 is for these methods to be deleted.