Hi Konrad

Sorry, but which version of Pharo are we talking about. I just tried with Pharo 
10, 
which has no flatten method for arrays at all, somewhere along the version I 
believe
the method was renamed to flattened.

#(1 #(2 #(a b) 3) 4) flattened >>> #(1 2 #a #b 3 4) as one would expect.

flatCollect: does not flatten the collection it is called on, but the result 
produced by the block
#(1 3) flatCollect: [ :e | {e. e * 10} ] >>> #(1 10 3 30), and
#(1 #(2 3 #(4 5)) 6) flatCollect: [ :e | {e. e * 10} ]  >>>  #(1 10 #(2 3 #(4 
5)) #(20 30 #(40 50)) 6 60)

I am not going to comment on naming of such core api methods - they tend to 
have 
a 40 year history of forgotten arguments and random “clean ups”.

Best,

Kasper

> On 12 Apr 2022, at 11.51, Konrad Hinsen <konrad.hin...@fastmail.net> wrote:
> 
> Hi everyone,
> 
> I am getting surprising results trying to flatten nested arrays. So I
> read the docstrings. And now I understand even less how flatten and
> flatCollect are supposed to work.
> 
> flatten says:
> 
>    "Recursively collect each non-collection element of the receiver and
>     its descendant collections.  Please note, this implementation
>     assumes that strings are to be treated as objects rather than as
>     collection."
> 
> That suggests that
> 
>   #(1 2 3) flatten
> 
> should yield #(1 2 3). But it raises an exception, trying to send do: to
> an integer.
> 
> A quick look at the code shows that flatten is just flatCollect: [
> :each | each ]. flatCollect: says:
> 
>   "Evaluate aBlock for each of the receiver's elements and answer the
>    list of all resulting values flatten one level. Assumes that aBlock
>    returns some kind of collection for each element. Equivalent to the
>    lisp's mapcan"
> 
> "Flatten one level" is different from flatten's "recursively". The
> assumption that aBlock returns a collection is also incompatible with
> what flatten is supposed to do (and explains my exception).
> 
> I don't find the reference to a Lisp function very helpful. Assuming
> "Lisp" means "Common Lisp", this confirms the docstring of flatCollect:,
> this confirms the assumption of aBlock returning a collection, and that
> only one level of nesting is flattened.
> 
> Since flatCollect does what it says, the problem seems to be in
> flatten. Either its docstring or its implementation are wrong.
> 
> Or is it me misunderstanding something?
> 
> Cheers,
>  Konrad

Reply via email to