feat(list): add `unique` function (#716)

* feat(list): add `unique` function

* Update lists.bzl

* Update lists.bzl

* Update lists.bzl

* Update lists.bzl

---------

Co-authored-by: Alex Eagle <alex@aspect.dev>
This commit is contained in:
Marc Redemske 2024-02-27 17:25:00 +01:00 committed by GitHub
parent 4670e58007
commit a3d7efe298
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 57 additions and 2 deletions

26
docs/lists.md generated
View File

@ -188,3 +188,29 @@ Example:
True or False True or False
<a id="unique"></a>
## unique
<pre>
unique(<a href="#unique-arr">arr</a>)
</pre>
Return a new list with unique items in it.
Example:
`unique(["foo", "bar", "foo", "baz"]) // ["foo", "bar", "baz"]`
**PARAMETERS**
| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="unique-arr"></a>arr | List to iterate over | none |
**RETURNS**
A new list with unique items

View File

@ -1,6 +1,6 @@
"Functions for lists" "Functions for lists"
load("//lib/private:lists.bzl", _every = "every", _filter = "filter", _find = "find", _map = "map", _once = "once", _pick = "pick", _some = "some") load("//lib/private:lists.bzl", _every = "every", _filter = "filter", _find = "find", _map = "map", _once = "once", _pick = "pick", _some = "some", _unique = "unique")
every = _every every = _every
filter = _filter filter = _filter
@ -9,3 +9,4 @@ map = _map
once = _once once = _once
pick = _pick pick = _pick
some = _some some = _some
unique = _unique

View File

@ -117,3 +117,21 @@ def some(f, arr):
if f(a): if f(a):
return True return True
return False return False
def unique(arr):
"""Return a new list with unique items in it.
Example:
`unique(["foo", "bar", "foo", "baz"]) // ["foo", "bar", "baz"]`
Args:
arr: List to iterate over
Returns:
A new list with unique items
"""
res = []
for a in arr:
if a not in res:
res.append(a)
return res

View File

@ -1,7 +1,7 @@
"""unit tests for lists""" """unit tests for lists"""
load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest") load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
load("//lib/private:lists.bzl", "every", "filter", "find", "map", "once", "pick", "some") load("//lib/private:lists.bzl", "every", "filter", "find", "map", "once", "pick", "some", "unique")
def _every_test_impl(ctx): def _every_test_impl(ctx):
env = unittest.begin(ctx) env = unittest.begin(ctx)
@ -70,6 +70,15 @@ def _some_test_impl(ctx):
some_test = unittest.make(_some_test_impl) some_test = unittest.make(_some_test_impl)
def _unique_test_impl(ctx):
env = unittest.begin(ctx)
asserts.equals(env, unique(["foo", {"bar": "baz"}, 42, {"bar": "baz"}, "foo"]), ["foo", {"bar": "baz"}, 42])
return unittest.end(env)
unique_test = unittest.make(_unique_test_impl)
def lists_test_suite(): def lists_test_suite():
unittest.suite( unittest.suite(
"lists_tests", "lists_tests",
@ -80,4 +89,5 @@ def lists_test_suite():
once_test, once_test,
pick_test, pick_test,
some_test, some_test,
unique_test,
) )