Skip to content

tillahoffmann/bazel-itertools

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bazel-itertools CI pipeline

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.

Usage

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)]

Functions

Terminating on the shortest input sequence

  • accumulate(iterable, func, *, initial=None): Make an iterator that returns accumulated sums, or accumulated results of other binary functions (specified via the optional func argument).
  • 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 for chain(). Gets chained inputs from a single iterable argument that is evaluated lazily. This function is implemented as chain_from_iterable because starlark does not support classes.
  • compress(data, selectors): Make an iterator that filters elements from data returning only those that have a corresponding element in selectors that evaluates to True. Stops when either the data or selectors iterables 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 from iterable returning only those for which the predicate is False. If predicate is None, return the items that are false.
  • groupby(iterable, key=None): Make an iterator that returns consecutive keys and groups from the iterable. The key is a function computing a key value for each element. If not specified or is None, key defaults to an identity function and returns the element unchanged. This function differs from python's implementation because it does not require iterable to be pre-sorted by key. The result is not necessarily sorted by key.
  • islice(iterable, stop) and islice(iterable, start, stop[, step]): Make an iterator that returns selected elements from the 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 the iterable as long as the predicate is True.
  • tee(iterable, n=2): Return n independent iterators from a single iterable. 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 the iterables. If the iterables are of uneven length, missing values are filled-in with fillvalue. Iteration continues until the longest iterable is exhausted.

Combinatoric functions

Infinite iterators

Infinite iterators are not supported because starlark does not support the concept of iterators, only finite sequences. Consequently, the following functions are not implemented.

  • count(start=0, step=1): Make an iterator that returns evenly spaced values starting with number start.
  • cycle(iterable): 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.
  • repeat(object[, times]): Make an iterator that returns object over and over again. Runs indefinitely unless the times argument is specified.

About

Functions for efficient looping matching python's itertools.

Topics

Resources

License

Stars

Watchers

Forks