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