Skip to content
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

Add flatten function that collapses first dimension of an iterable into a list. #413

Open
wants to merge 4 commits into
base: main
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions docs/collections_doc.md
Expand Up @@ -73,3 +73,27 @@ Requires all the elements to be hashable.
A new list with all unique elements from `iterable`.


<a id="collections.flatten"></a>

## collections.flatten

<pre>
collections.flatten(<a href="#collections.flatten-iterable">iterable</a>)
</pre>

Flattens an iterable to the sum of the elements with a starting value of [].

This behaves like collapsing the first dimension of the iterable.

**PARAMETERS**


| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="collections.flatten-iterable"></a>iterable | An iterable to be collapsed to a list. | none |

**RETURNS**

A new list with the collapsed elements from `iterable`.


17 changes: 17 additions & 0 deletions lib/collections.bzl
Expand Up @@ -65,8 +65,25 @@ def _uniq(iterable):
# TODO(bazel-team): Remove when testing frameworks no longer require python compatibility.
return list(unique_elements.keys())

def _flatten(iterable):
"""Flattens an iterable to the sum of the elements with a starting value of [].

This behaves like collapsing the first dimension of the iterable.

Args:
iterable: An iterable to be collapsed to a list.

Returns:
A new list with the collapsed elements from `iterable`.
"""
result = []
for element in iterable:
result += element
return result

collections = struct(
after_each = _after_each,
before_each = _before_each,
uniq = _uniq,
flatten = _flatten,
)
18 changes: 18 additions & 0 deletions tests/collections_tests.bzl
Expand Up @@ -103,11 +103,29 @@ def _uniq_test(ctx):

uniq_test = unittest.make(_uniq_test)

def _flatten_test(ctx):
env = unittest.begin(ctx)
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)

flatten_test = unittest.make(_flatten_test)

def collections_test_suite():
"""Creates the test targets and test suite for collections.bzl tests."""
unittest.suite(
"collections_tests",
after_each_test,
before_each_test,
uniq_test,
flatten_test,
)