Skip to content

interpreter: Add a flatten() method to arrays #14586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/markdown/snippets/array-flatten.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Array `.flatten()` method

Arrays now have a `.flatten()` method, which turns nested arrays into a single
flat array. This provides the same effect that Meson often does to arrays
internally, such as when passed to most function arguments.
5 changes: 5 additions & 0 deletions docs/yaml/elementary/list.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,8 @@ methods:
- name: length
returns: int
description: Returns the current size of the array / list.

- name: flatten
returns: array[any]
since: 1.9.0
description: Returns a flattened copy of the array, with all nested arrays removed.
14 changes: 14 additions & 0 deletions mesonbuild/interpreter/primitives/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def __init__(self, obj: T.List[TYPE_var], interpreter: 'Interpreter') -> None:
'contains': self.contains_method,
'length': self.length_method,
'get': self.get_method,
'flatten': self.flatten_method,
})

self.trivial_operators.update({
Expand Down Expand Up @@ -106,3 +107,16 @@ def op_index(self, other: int) -> TYPE_var:
return self.held_object[other]
except IndexError:
raise InvalidArguments(f'Index {other} out of bounds of array of size {len(self.held_object)}.')

@noPosargs
@noKwargs
@FeatureNew('array.flatten', '1.9.0')
def flatten_method(self, args: T.List[TYPE_var], kwargs: TYPE_kwargs) -> TYPE_var:
def flatten(obj: TYPE_var) -> T.Iterable[TYPE_var]:
if isinstance(obj, list):
for o in obj:
yield from flatten(o)
else:
yield obj

return list(flatten(self.held_object))
9 changes: 8 additions & 1 deletion test cases/common/56 array methods/meson.build
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project('array methods')
project('array methods', meson_version : '>= 1.9')

empty = []
one = ['abc']
Expand Down Expand Up @@ -68,3 +68,10 @@ endif
if not combined.contains('ghi')
error('Combined claims not to contain ghi.')
endif

# test array flattening
x = ['a', ['b'], [[[[[[['c'], 'd']]], 'e']]]]
assert(x.length() == 3)
assert(x.flatten().length() == 5)
assert(x.flatten() == ['a', 'b', 'c', 'd', 'e'])
assert(['a', ['b', 'c']].flatten() == ['a', 'b', 'c'])
Loading