From 6129057cb327c250621d3497ba205e254eccae5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sami=20Kalliom=C3=A4ki?= Date: Tue, 15 Nov 2022 15:00:44 +0000 Subject: [PATCH 1/4] Add flatten function. --- docs/collections_doc.md | 22 ++++++++++++++++++++++ lib/collections.bzl | 16 ++++++++++++++++ tests/collections_tests.bzl | 11 +++++++++++ 3 files changed, 49 insertions(+) diff --git a/docs/collections_doc.md b/docs/collections_doc.md index cd9db8b1..e29feb0f 100755 --- a/docs/collections_doc.md +++ b/docs/collections_doc.md @@ -73,3 +73,25 @@ Requires all the elements to be hashable. A new list with all unique elements from `iterable`. + + +## collections.flatten + +
+collections.flatten(iterable)
+
+ +Collapses the first dimension of the iterable and returns the result as a list. + +**PARAMETERS** + + +| Name | Description | Default Value | +| :------------- | :------------- | :------------- | +| iterable | An iterable of iterables to be collapsed to a list. | none | + +**RETURNS** + +A new list with the collapsed elements from `iterable`. + + diff --git a/lib/collections.bzl b/lib/collections.bzl index f41eea2e..2e777e6f 100644 --- a/lib/collections.bzl +++ b/lib/collections.bzl @@ -65,8 +65,24 @@ def _uniq(iterable): # TODO(bazel-team): Remove when testing frameworks no longer require python compatibility. return list(unique_elements.keys()) +def _flatten(iterable): + """Collapses the first dimension of the iterable and returns the result as a list. + + Args: + iterable: An iterable of iterables to be collapsed to a list. + + Returns: + A new list with the collapsed elements from `iterable`. + """ + return [ + element + for subiterable in iterable + for element in subiterable + ] + collections = struct( after_each = _after_each, before_each = _before_each, uniq = _uniq, + flatten = _flatten, ) diff --git a/tests/collections_tests.bzl b/tests/collections_tests.bzl index 9e866721..c74bdb83 100644 --- a/tests/collections_tests.bzl +++ b/tests/collections_tests.bzl @@ -103,6 +103,16 @@ def _uniq_test(ctx): 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]) + + 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( @@ -110,4 +120,5 @@ def collections_test_suite(): after_each_test, before_each_test, uniq_test, + flatten_test, ) From ee14cbd3d94880f7e02d77849efa97ac56d8cbfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sami=20Kalliom=C3=A4ki?= Date: Wed, 16 Nov 2022 10:33:11 +0000 Subject: [PATCH 2/4] Switch to manual for loop to support select statements. --- docs/collections_doc.md | 4 +++- lib/collections.bzl | 13 +++++++------ tests/collections_tests.bzl | 13 ++++++++++--- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/docs/collections_doc.md b/docs/collections_doc.md index e29feb0f..0c74499f 100755 --- a/docs/collections_doc.md +++ b/docs/collections_doc.md @@ -81,7 +81,9 @@ A new list with all unique elements from `iterable`. collections.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. **PARAMETERS** diff --git a/lib/collections.bzl b/lib/collections.bzl index 2e777e6f..615d1f1d 100644 --- a/lib/collections.bzl +++ b/lib/collections.bzl @@ -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, diff --git a/tests/collections_tests.bzl b/tests/collections_tests.bzl index c74bdb83..1c911e49 100644 --- a/tests/collections_tests.bzl +++ b/tests/collections_tests.bzl @@ -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) From faeeaa2a7a3d5c20ef4d2f0d348d4abfe759e435 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sami=20Kalliom=C3=A4ki?= Date: Wed, 16 Nov 2022 10:34:38 +0000 Subject: [PATCH 3/4] Update docs. --- docs/collections_doc.md | 2 +- lib/collections.bzl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/collections_doc.md b/docs/collections_doc.md index 0c74499f..f32b9640 100755 --- a/docs/collections_doc.md +++ b/docs/collections_doc.md @@ -81,7 +81,7 @@ A new list with all unique elements from `iterable`. collections.flatten(iterable) -Flattens an iterable to the sum of those iterables. +Flattens an iterable to the sum of the elements with a starting value of []. This behaves like collapsing the first dimension of the iterable. diff --git a/lib/collections.bzl b/lib/collections.bzl index 615d1f1d..bfb421ed 100644 --- a/lib/collections.bzl +++ b/lib/collections.bzl @@ -66,7 +66,7 @@ def _uniq(iterable): return list(unique_elements.keys()) def _flatten(iterable): - """Flattens an iterable to the sum of those iterables. + """Flattens an iterable to the sum of the elements with a starting value of []. This behaves like collapsing the first dimension of the iterable. From dfe86a797ddd6d9c54c58a20da5572ab9c8e69fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sami=20Kalliom=C3=A4ki?= Date: Wed, 16 Nov 2022 10:35:39 +0000 Subject: [PATCH 4/4] Update docs 2. --- docs/collections_doc.md | 2 +- lib/collections.bzl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/collections_doc.md b/docs/collections_doc.md index f32b9640..cc8b6dbe 100755 --- a/docs/collections_doc.md +++ b/docs/collections_doc.md @@ -90,7 +90,7 @@ This behaves like collapsing the first dimension of the iterable. | Name | Description | Default Value | | :------------- | :------------- | :------------- | -| iterable | An iterable of iterables to be collapsed to a list. | none | +| iterable | An iterable to be collapsed to a list. | none | **RETURNS** diff --git a/lib/collections.bzl b/lib/collections.bzl index bfb421ed..5f59a2df 100644 --- a/lib/collections.bzl +++ b/lib/collections.bzl @@ -71,7 +71,7 @@ def _flatten(iterable): This behaves like collapsing the first dimension of the iterable. Args: - iterable: An iterable of iterables to be collapsed to a list. + iterable: An iterable to be collapsed to a list. Returns: A new list with the collapsed elements from `iterable`.