Collection functions
Collection constructors
|
Creates a dictionary. |
|
Returns an empty dictionary with key type key_type and value type value_type. |
|
Construct an array expression. |
|
Returns an empty array of elements of a type t. |
|
Convert a set expression. |
|
Returns an empty set of elements of a type t. |
Collection functions
|
Returns the size of a collection or string. |
|
Transform each element of a collection. |
|
Map each element of the collection to a new collection, and flatten the results. |
|
Transform each element of a collection of tuples. |
|
Zip together arrays into a single array. |
|
Returns an array of (index, element) tuples. |
|
Deprecated in favor of |
|
Flatten a nested collection by concatenating sub-collections. |
|
Check for any |
|
Check for all |
|
Returns a new collection containing elements where f returns |
|
Returns a sorted array. |
|
Returns the first element where f returns |
|
Group collection elements into a dict according to a lambda function. |
|
Reduces a collection with the given function f, provided the initial value zero. |
|
Map each element of a to cumulative value of function f, with initial value zero. |
|
Reverses the elements of a collection. |
|
Compute the intersection of sorted arrays on a given key. |
|
Compute the distinct union of sorted arrays on a given key. |
- hail.expr.functions.len(x)[source]
Returns the size of a collection or string.
Examples
>>> a = ['The', 'quick', 'brown', 'fox'] >>> s = {1, 3, 5, 6, 7, 9}
>>> hl.eval(hl.len(a)) 4
>>> hl.eval(hl.len(s)) 6
>>> hl.eval(hl.len("12345")) 5
- Parameters:
x (
ArrayExpression
orSetExpression
orDictExpression
orStringExpression
) – String or collection expression.- Returns:
Expression
of typetint32
- hail.expr.functions.map(f, *collections)[source]
Transform each element of a collection.
Examples
>>> a = ['The', 'quick', 'brown', 'fox'] >>> b = [2, 4, 6, 8]
>>> hl.eval(hl.map(lambda x: hl.len(x), a)) [3, 5, 5, 3]
>>> hl.eval(hl.map(lambda s, n: hl.len(s) + n, a, b)) [5, 9, 11, 11]
- Parameters:
f (function ( (*arg) ->
Expression
)) – Function to transform each element of the collection.*collections (
ArrayExpression
orSetExpression
) – A single collection expression or multiple array expressions.
- Returns:
ArrayExpression
orSetExpression
. – Collection where each element has been transformed by f.
- hail.expr.functions.flatmap(f, collection)[source]
Map each element of the collection to a new collection, and flatten the results.
Examples
>>> a = [[0, 1], [1, 2], [4, 5, 6, 7]]
>>> hl.eval(hl.flatmap(lambda x: x[1:], a)) [1, 2, 5, 6, 7]
- Parameters:
f (function ( (arg) ->
CollectionExpression
)) – Function from the element type of the collection to the type of the collection. For instance, flatmap on aset<str>
should take astr
and return aset
.collection (
ArrayExpression
orSetExpression
) – Collection expression.
- Returns:
- hail.expr.functions.starmap(f, collection)[source]
Transform each element of a collection of tuples.
Examples
>>> a = [(1, 5), (3, 2), (7, 8)]
>>> hl.eval(hl.starmap(lambda x, y: hl.if_else(x < y, x, y), a)) [1, 2, 7]
- Parameters:
f (function ( (*args) ->
Expression
)) – Function to transform each element of the collection.collection (
ArrayExpression
orSetExpression
) – Collection expression.
- Returns:
ArrayExpression
orSetExpression
. – Collection where each element has been transformed by f.
- hail.expr.functions.zip(*arrays, fill_missing=False)[source]
Zip together arrays into a single array.
Examples
>>> hl.eval(hl.zip([1, 2, 3], [4, 5, 6])) [(1, 4), (2, 5), (3, 6)]
If the arrays are different lengths, the behavior is decided by the fill_missing parameter.
>>> hl.eval(hl.zip([1], [10, 20], [100, 200, 300])) [(1, 10, 100)]
>>> hl.eval(hl.zip([1], [10, 20], [100, 200, 300], fill_missing=True)) [(1, 10, 100), (None, 20, 200), (None, None, 300)]
Notes
The element type of the resulting array is a
ttuple
with a field for each array.- Parameters:
arrays (: variable-length args of
ArrayExpression
) – Array expressions.fill_missing (
bool
) – IfFalse
, return an array with length equal to the shortest length of the arrays. IfTrue
, return an array equal to the longest length of the arrays, by extending the shorter arrays with missing values.
- Returns:
- hail.expr.functions.enumerate(a, start=0, *, index_first=True)[source]
Returns an array of (index, element) tuples.
Examples
>>> hl.eval(hl.enumerate(['A', 'B', 'C'])) [(0, 'A'), (1, 'B'), (2, 'C')]
>>> hl.eval(hl.enumerate(['A', 'B', 'C'], start=3)) [(3, 'A'), (4, 'B'), (5, 'C')]
>>> hl.eval(hl.enumerate(['A', 'B', 'C'], index_first=False)) [('A', 0), ('B', 1), ('C', 2)]
- Parameters:
a (
ArrayExpression
)start (
Int32Expression
) – The index value from which the counter is started, 0 by default.index_first (
bool
) – IfTrue
, the index is the first value of the element tuples. IfFalse
, the index is the second value.
- Returns:
ArrayExpression
– Array of (index, element) or (element, index) tuples.
- hail.expr.functions.zip_with_index(a, index_first=True)[source]
Deprecated in favor of
enumerate()
.Returns an array of (index, element) tuples.
Examples
>>> hl.eval(hl.zip_with_index(['A', 'B', 'C'])) [(0, 'A'), (1, 'B'), (2, 'C')]
>>> hl.eval(hl.zip_with_index(['A', 'B', 'C'], index_first=False)) [('A', 0), ('B', 1), ('C', 2)]
- Parameters:
a (
ArrayExpression
)index_first (
bool
) – IfTrue
, the index is the first value of the element tuples. IfFalse
, the index is the second value.
- Returns:
ArrayExpression
– Array of (index, element) or (element, index) tuples.
- hail.expr.functions.flatten(collection)[source]
Flatten a nested collection by concatenating sub-collections.
Examples
>>> a = [[1, 2], [2, 3]]
>>> hl.eval(hl.flatten(a)) [1, 2, 2, 3]
- Parameters:
collection (
ArrayExpression
orSetExpression
) – Collection with element typetarray
ortset
.- Returns:
collection (
ArrayExpression
orSetExpression
)
- hail.expr.functions.any(*args)[source]
Check for any
True
in boolean expressions or collections of booleans.any()
comes in three forms:hl.any(boolean, ...)
. Is at least one argumentTrue
?hl.any(collection)
. Is at least one element of this collectionTrue
?hl.any(function, collection)
. Doesfunction
returnTrue
for at least one value in this collection?
Examples
The first form:
>>> hl.eval(hl.any()) False
>>> hl.eval(hl.any(True)) True
>>> hl.eval(hl.any(False)) False
>>> hl.eval(hl.any(False, False, True, False)) True
The second form:
>>> hl.eval(hl.any([False, True, False])) True
>>> hl.eval(hl.any([False, False, False])) False
The third form:
>>> a = ['The', 'quick', 'brown', 'fox'] >>> s = {1, 3, 5, 6, 7, 9}
>>> hl.eval(hl.any(lambda x: x[-1] == 'x', a)) True
>>> hl.eval(hl.any(lambda x: x % 4 == 0, s)) False
Notes
any()
returnsFalse
when given an empty array or empty argument list.
- hail.expr.functions.all(*args)[source]
Check for all
True
in boolean expressions or collections of booleans.all()
comes in three forms:hl.all(boolean, ...)
. Are all argumentsTrue
?hl.all(collection)
. Are all elements of the collectionTrue
?hl.all(function, collection)
. Doesfunction
returnTrue
for all values in this collection?
Examples
The first form:
>>> hl.eval(hl.all()) True
>>> hl.eval(hl.all(True)) True
>>> hl.eval(hl.all(False)) False
>>> hl.eval(hl.all(True, True, True)) True
>>> hl.eval(hl.all(False, False, True, False)) False
The second form:
>>> hl.eval(hl.all([False, True, False])) False
>>> hl.eval(hl.all([True, True, True])) True
The third form:
>>> a = ['The', 'quick', 'brown', 'fox'] >>> s = {1, 3, 5, 6, 7, 9}
>>> hl.eval(hl.all(lambda x: hl.len(x) > 3, a)) False
>>> hl.eval(hl.all(lambda x: x < 10, s)) True
Notes
all()
returnsTrue
when given an empty array or empty argument list.
- hail.expr.functions.filter(f, collection)[source]
Returns a new collection containing elements where f returns
True
.Examples
>>> a = [1, 2, 3, 4] >>> s = {'Alice', 'Bob', 'Charlie'}
>>> hl.eval(hl.filter(lambda x: x % 2 == 0, a)) [2, 4]
>>> hl.eval(hl.filter(lambda x: ~(x[-1] == 'e'), s)) {'Bob'}
Notes
Returns a same-type expression; evaluated on a
SetExpression
, returns aSetExpression
. Evaluated on anArrayExpression
, returns anArrayExpression
.- Parameters:
f (function ( (arg) ->
BooleanExpression
)) – Function to evaluate for each element of the collection. Must return aBooleanExpression
.collection (
ArrayExpression
orSetExpression
.) – Array or set expression to filter.
- Returns:
ArrayExpression
orSetExpression
– Expression of the same type as collection.
- hail.expr.functions.sorted(collection, key=None, reverse=False)[source]
Returns a sorted array.
Examples
>>> a = ['Charlie', 'Alice', 'Bob']
>>> hl.eval(hl.sorted(a)) ['Alice', 'Bob', 'Charlie']
>>> hl.eval(hl.sorted(a, reverse=True)) ['Charlie', 'Bob', 'Alice']
>>> hl.eval(hl.sorted(a, key=lambda x: hl.len(x))) ['Bob', 'Alice', 'Charlie']
Notes
The ordered types are
tstr
and numeric types.- Parameters:
collection (
ArrayExpression
orSetExpression
orDictExpression
) – Collection to sort.key (function ( (arg) ->
Expression
), optional) – Function to evaluate for each element to compute sort key.reverse (
BooleanExpression
) – Sort in descending order.
- Returns:
ArrayExpression
– Sorted array.
- hail.expr.functions.find(f, collection)[source]
Returns the first element where f returns
True
.Examples
>>> a = ['The', 'quick', 'brown', 'fox'] >>> s = {1, 3, 5, 6, 7, 9}
>>> hl.eval(hl.find(lambda x: x[-1] == 'x', a)) 'fox'
>>> hl.eval(hl.find(lambda x: x % 4 == 0, s)) None
Notes
If f returns
False
for every element, then the result is missing.Sets are unordered. If collection is of type
tset
, then the element returned comes from no guaranteed ordering.- Parameters:
f (function ( (arg) ->
BooleanExpression
)) – Function to evaluate for each element of the collection. Must return aBooleanExpression
.collection (
ArrayExpression
orSetExpression
) – Collection expression.
- Returns:
Expression
– Expression whose type is the element type of the collection.
- hail.expr.functions.group_by(f, collection)[source]
Group collection elements into a dict according to a lambda function.
Examples
>>> a = ['The', 'quick', 'brown', 'fox'] >>> hl.eval(hl.group_by(lambda x: hl.len(x), a)) {3: ['The', 'fox'], 5: ['quick', 'brown']}
- Parameters:
f (function ( (arg) ->
Expression
)) – Function to evaluate for each element of the collection to produce a key for the resulting dictionary.collection (
ArrayExpression
orSetExpression
) – Collection expression.
- Returns:
DictExpression
. – Dictionary keyed by results of f.
- hail.expr.functions.fold(f, zero, collection)[source]
Reduces a collection with the given function f, provided the initial value zero.
Examples
>>> a = [0, 1, 2]
>>> hl.eval(hl.fold(lambda i, j: i + j, 0, a)) 3
- Parameters:
f (function ( (
Expression
,Expression
) ->Expression
)) – Function which takes the cumulative value and the next element, and returns a new value.zero (
Expression
) – Initial value to pass in as left argument of f.collection (
ArrayExpression
orSetExpression
)
- Returns:
- hail.expr.functions.array_scan(f, zero, a)[source]
Map each element of a to cumulative value of function f, with initial value zero.
Examples
>>> a = [0, 1, 2]
>>> hl.eval(hl.array_scan(lambda i, j: i + j, 0, a)) [0, 0, 1, 3]
- Parameters:
f (function ( (
Expression
,Expression
) ->Expression
)) – Function which takes the cumulative value and the next element, and returns a new value.zero (
Expression
) – Initial value to pass in as left argument of f.a (
ArrayExpression
)
- Returns:
- hail.expr.functions.reversed(x)[source]
Reverses the elements of a collection.
Examples
>>> a = ['The', 'quick', 'brown', 'fox'] >>> hl.eval(hl.reversed(a)) ['fox', 'brown', 'quick', 'The']
- Parameters:
x (
ArrayExpression
orStringExpression
) – Array or string expression.- Returns:
- hail.expr.functions.keyed_intersection(*arrays, key)[source]
Compute the intersection of sorted arrays on a given key.
Requires sorted arrays with distinct keys.
Warning
Experimental. Does not support downstream randomness.
- Parameters:
arrays
key
- Returns: