2
0
Fork 0
mirror of https://github.com/bazelbuild/bazel-skylib synced 2024-11-28 08:43:51 +00:00

Switch to manual for loop to support select statements.

This commit is contained in:
Sami Kalliomäki 2022-11-16 10:33:11 +00:00
parent 6129057cb3
commit ee14cbd3d9
3 changed files with 20 additions and 10 deletions

View file

@ -81,7 +81,9 @@ A new list with all unique elements from `iterable`.
collections.flatten(<a href="#collections.flatten-iterable">iterable</a>)
</pre>
Collapses the first dimension of the iterable and returns the result as a list.
Flattens an iterable to the sum of those iterables.
This behaves like collapsing the first dimension of the iterable.
**PARAMETERS**

View file

@ -66,7 +66,9 @@ def _uniq(iterable):
return list(unique_elements.keys())
def _flatten(iterable):
"""Collapses the first dimension of the iterable and returns the result as a list.
"""Flattens an iterable to the sum of those iterables.
This behaves like collapsing the first dimension of the iterable.
Args:
iterable: An iterable of iterables to be collapsed to a list.
@ -74,11 +76,10 @@ def _flatten(iterable):
Returns:
A new list with the collapsed elements from `iterable`.
"""
return [
element
for subiterable in iterable
for element in subiterable
]
result = []
for element in iterable:
result += element
return result
collections = struct(
after_each = _after_each,

View file

@ -105,9 +105,16 @@ uniq_test = unittest.make(_uniq_test)
def _flatten_test(ctx):
env = unittest.begin(ctx)
asserts.equals(env, collections.flatten([[0, 1], [2, 3]]), [0, 1, 2, 3])
asserts.equals(env, collections.flatten([]), [])
asserts.equals(env, collections.flatten([[], [0, 1]]), [0, 1])
asserts.equals(env, [0, 1, 2, 3], collections.flatten([[0, 1], [2, 3]]))
asserts.equals(env, [], collections.flatten([]), [])
asserts.equals(env, [0, 1], collections.flatten([[], [0, 1]]))
asserts.equals(env,
'[] + select({"//conditions:default": [0, 1]}) + select({"//conditions:default": [2, 3]})',
str(collections.flatten([select({
"//conditions:default": [0, 1],
}), select({
"//conditions:default": [2, 3],
})])))
return unittest.end(env)