diff options
author | Muriithi Frederick Muriuki | 2021-11-20 18:06:32 +0300 |
---|---|---|
committer | GitHub | 2021-11-20 18:06:32 +0300 |
commit | 92d5766f5514181cd6aa82fc0d0f225666e892cb (patch) | |
tree | 74840e8e2118e24e1f49eb780ca0bbf24704e510 /gn3/data_helpers.py | |
parent | abc0d36f39c691652fee81bce808d625fc368e72 (diff) | |
parent | 08c81b8892060353bb7fb15555875f03bbdcb46e (diff) | |
download | genenetwork3-92d5766f5514181cd6aa82fc0d0f225666e892cb.tar.gz |
Merge pull request #56 from genenetwork/partial-correlations
Partial correlations
Diffstat (limited to 'gn3/data_helpers.py')
-rw-r--r-- | gn3/data_helpers.py | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/gn3/data_helpers.py b/gn3/data_helpers.py index d3f942b..b72fbc5 100644 --- a/gn3/data_helpers.py +++ b/gn3/data_helpers.py @@ -24,6 +24,21 @@ def partition_all(num: int, items: Sequence[Any]) -> Tuple[Tuple[Any, ...], ...] in reduce( __compute_start_stop__, iterations, tuple())]) +def partition_by(partition_fn, items): + """ + Given a sequence `items`, return a tuple of tuples, each of which contain + the values in `items` partitioned such that the first item in each internal + tuple, when passed to `partition_function` returns True. + + This is an approximation of Clojure's `partition-by` function. + """ + def __partitioner__(accumulator, item): + if partition_fn(item): + return accumulator + ((item,),) + return accumulator[:-1] + (accumulator[-1] + (item,),) + + return reduce(__partitioner__, items, tuple()) + def parse_csv_line( line: str, delimiter: str = ",", quoting: Optional[str] = '"') -> Tuple[str, ...]: |