2
0
Fork 0
mirror of https://github.com/bazel-contrib/bazel-lib synced 2024-11-30 01:41:21 +00:00
bazel-lib/docs/lists.md
Marc Redemske 45986f000d
feat: add list utils (#512)
* feat: add list utils

* chore: add docs for lists

* fixes accoding to review

---------

Co-authored-by: Alex Eagle <alex@aspect.dev>
2023-09-21 10:12:47 -07:00

3.8 KiB
Generated

Functions for lists

every

every(f, arr)

Check if every item of arr passes function f.

Example: every(lambda i: i.endswith(".js"), ["app.js", "lib.js"]) // True

PARAMETERS

Name Description Default Value
f Function to execute on every item none
arr List to iterate over none

RETURNS

True or False

filter

filter(f, arr)

Filter a list arr by applying a function f to each item.

Example: filter(lambda i: i.endswith(".js"), ["app.ts", "app.js", "lib.ts", "lib.js"]) // ["app.js", "lib.js"]

PARAMETERS

Name Description Default Value
f Function to execute on every item none
arr List to iterate over none

RETURNS

A new list containing items that passed the filter function.

find

find(f, arr)

Find a particular item from list arr by a given function f.

Unlike pick, the find method returns a tuple of the index and the value of first item passing by f. Furhermore find does not fail if no item passes f. In this case (-1, None) is returned.

PARAMETERS

Name Description Default Value
f Function to execute on every item none
arr List to iterate over none

RETURNS

Tuple (index, item)

map

map(f, arr)

Apply a function f with each item of arr and return a new list.

Example: map(lambda i: i*2, [1, 2, 3]) // [2, 4, 6]

PARAMETERS

Name Description Default Value
f Function to execute on every item none
arr List to iterate over none

RETURNS

A new list with all mapped items.

once

once(f, arr)

Check if exactly one item in list arr passes the given function f.

PARAMETERS

Name Description Default Value
f Function to execute on every item none
arr List to iterate over none

RETURNS

True or False

pick

pick(f, arr)

Pick a particular item in list arr by a given function f.

Unlike filter, the pick method returns the first item found by f. If no item has passed f, the function will fail.

PARAMETERS

Name Description Default Value
f Function to execute on every item none
arr List to iterate over none

RETURNS

item

some

some(f, arr)

Check if at least one item of arr passes function f.

Example: some(lambda i: i.endswith(".js"), ["app.js", "lib.ts"]) // True

PARAMETERS

Name Description Default Value
f Function to execute on every item none
arr List to iterate over none

RETURNS

True or False