From f334b5ec5d51235d0daddc0c4fe700cb65bd825d Mon Sep 17 00:00:00 2001 From: Muriithi Frederick Muriuki Date: Thu, 22 Jul 2021 15:44:59 +0300 Subject: Extract common `is_list_or_tuple' function Issue: https://github.com/genenetwork/gn-gemtext-threads/blob/main/topics/gn1-migration-to-gn2/clustering.gmi * Extract the common function `is_list_or_tuple' making it accessible to later parts of the code. --- gn3/computations/slink.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'gn3/computations') diff --git a/gn3/computations/slink.py b/gn3/computations/slink.py index 3156b31..b15c058 100644 --- a/gn3/computations/slink.py +++ b/gn3/computations/slink.py @@ -4,10 +4,11 @@ class LengthError(BaseException): class MirrorError(BaseException): pass +def is_list_or_tuple(item): + return type(item) in [list, tuple] + def raise_valueerror_if_data_is_not_lists_or_tuples(lists): """Check that `lists` is a list of lists: If not, raise an exception.""" - def is_list_or_tuple(item): - return type(item) == type([]) or type(item) == type(tuple) if (not is_list_or_tuple(lists)) or (not all(map(is_list_or_tuple, lists))): raise ValueError("Expected list or tuple") @@ -72,7 +73,7 @@ This description should be updated once the form/type of 'distance' identified." #### END: Guard Functions #### if type(i) == int and type(j) == int: # From member i to member j return lists[i][j] - elif type(i) == int and (type(j) in [list, tuple]): + elif type(i) == int and is_list_or_tuple(j): return min(map(lambda j_new: nearest(lists, i, j_new), j)) - elif type(j) == int and (type(i) in [list, tuple]): + elif type(j) == int and is_list_or_tuple(i): return min(map(lambda i_new: nearest(lists, i_new, j), i)) -- cgit v1.2.3