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:
parent
4670e58007
commit
a3d7efe298
|
@ -188,3 +188,29 @@ Example:
|
|||
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
|
||||
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
"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
|
||||
filter = _filter
|
||||
|
@ -9,3 +9,4 @@ map = _map
|
|||
once = _once
|
||||
pick = _pick
|
||||
some = _some
|
||||
unique = _unique
|
||||
|
|
|
@ -117,3 +117,21 @@ def some(f, arr):
|
|||
if f(a):
|
||||
return True
|
||||
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
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
"""unit tests for lists"""
|
||||
|
||||
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):
|
||||
env = unittest.begin(ctx)
|
||||
|
@ -70,6 +70,15 @@ def _some_test_impl(ctx):
|
|||
|
||||
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():
|
||||
unittest.suite(
|
||||
"lists_tests",
|
||||
|
@ -80,4 +89,5 @@ def lists_test_suite():
|
|||
once_test,
|
||||
pick_test,
|
||||
some_test,
|
||||
unique_test,
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue