Functions for efficient looping matching python's itertools. These functions are convenient for generating targets, e.g. for hyperparameter search in machine learning or sensitivity analyses to test the robustness of scientific methods.
First, add the following lines to your WORKSPACE file to download the bazel-itertools functions.
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "bazel_itertools",
url = "https://github.com/tillahoffmann/bazel-itertools/archive/refs/tags/[VERSION].tar.gz",
strip_prefix = "bazel-itertools-[VERSION]",
# sha256 = "[add for peace of mind]",
)Second, load the functions and use them, e.g. in a BUILD file.
load("@bazel_itertools//lib:itertools.bzl", "itertools")
print(itertools.combinations([1, 2, 3], 2))
# [(1, 2), (1, 3), (2, 3)]-
accumulate(iterable, func, *, initial=None): Make an iterator that returns accumulated sums, or accumulated results of other binary functions (specified via the optionalfuncargument). -
chain(*iterables): Make an iterator that returns elements from the first iterable until it is exhausted, then proceeds to the next iterable, until all of the iterables are exhausted. -
chain.from_iterable(iterable): Alternate constructor forchain(). Gets chained inputs from a single iterable argument that is evaluated lazily. This function is implemented aschain_from_iterablebecause starlark does not support classes. -
compress(data, selectors): Make an iterator that filters elements fromdatareturning only those that have a corresponding element inselectorsthat evaluates toTrue. Stops when either thedataorselectorsiterables has been exhausted. -
dropwhile(predicate, iterable): Make an iterator that drops elements from the iterable as long as the predicate is true; afterwards, returns every element. -
filterfalse(predicate, iterable): Make an iterator that filters elements fromiterablereturning only those for which thepredicateisFalse. IfpredicateisNone, return the items that are false. -
groupby(iterable, key=None): Make an iterator that returns consecutive keys and groups from theiterable. Thekeyis a function computing a key value for each element. If not specified or isNone,keydefaults to an identity function and returns the element unchanged. This function differs from python's implementation because it does not requireiterableto be pre-sorted bykey. The result is not necessarily sorted bykey. -
: Make an iterator that returns selected elements from theislice(iterable, stop)andislice(iterable, start, stop[, step])iterable. This function is not implemented because starlark does not support the concept of iterators, only finite sequences. Standard slicing can always be used. -
pairwise(iterable): Return successive overlapping pairs taken from the input iterable. -
starmap(function, iterable): Make an iterator that computes the function using arguments obtained from the iterable. -
takewhile(predicate, iterable): Make an iterator that returns elements from theiterableas long as thepredicateisTrue. -
: Returntee(iterable, n=2)nindependent iterators from a singleiterable. This function is not implemented because starlark does not support the concept of iterators, only finite sequences. Sequences can always be reused without exhausting them. -
zip_longest(*iterables, fillvalue=None): Make an iterator that aggregates elements from each of theiterables. If theiterablesare of uneven length, missing values are filled-in withfillvalue. Iteration continues until the longest iterable is exhausted.
-
product(*iterables, repeat=1): Cartesian product of input iterables. -
permutations(iterable, r=None): Return successiverlength permutations of elements in theiterable. -
combinations(iterable, r): Returnrlength subsequences of elements from the inputiterable. -
combinations_with_replacement(iterable, r): Returnrlength subsequences of elements from the inputiterableallowing individual elements to be repeated more than once.
Infinite iterators are not supported because starlark does not support the concept of iterators, only finite sequences. Consequently, the following functions are not implemented.
-
: Make an iterator that returns evenly spaced values starting with number start.count(start=0, step=1) -
: Make an iterator returning elements from the iterable and saving a copy of each. When the iterable is exhausted, return elements from the saved copy. Repeats indefinitely.cycle(iterable) -
: Make an iterator that returns object over and over again. Runs indefinitely unless therepeat(object[, times])timesargument is specified.