Expand/Shrink

flatten

Definition: sequence res = flatten(sequence s, res="")
Description: Remove all nesting from a sequence.

Returns a string or a sequence of atoms.

By default, if all (nested) elements of s are string or integers in the range 0..255, a [binary] string will be returned.
The optional res parameter allows that behaviour to be overriden, ie flatten(s,{}) returns a dword-sequence.
It is also possible to provide a starting prefix, eg flatten({"a",{"b",{"c"}}},"flattened:") returns "flattened:abc".
Comments: Empty sub-sequences are stripped out entirely.

Unlike the (broken) Euphoria version of flatten, there is no optional delimiter: for something like {"this","that"} -> "this\nthat", use join(), which is usually much more appropriate for string processing.
Example: You can use flatten() on a tree-like structure, e.g.
?flatten({10,{ 13, {45}}, {84.1, {}, 33.2}})    -- {10,13,45,84.1,33.2}
?flatten({10,{ 13, {45}}, {84, {}, 33}})        -- "\n\r-T!"
?flatten({10,{ 13, {45}}, {84, {}, 33}},{})     -- {10,13,45,84,33}
Implementation: See builtins\pflatten.e (an autoinclude) for details of the actual implementation.
See Also: join