import dill


def pairwise_worker(payload):
    """
    Worker function used to compute pairwise computations over chunks of
    an upper triangular matrix in parallel.

    """
    similarity, I, J, offset_i, offset_j = payload

    similarity = dill.loads(similarity)
    pairs = []

    diagonal_chunk = offset_i == offset_j

    if diagonal_chunk:
        J = I

    for i in range(len(I)):
        A = I[i]

        for j in range(0 if not diagonal_chunk else i + 1, len(J)):
            B = J[j]

            if similarity(A, B):
                pairs.append((offset_i + i, offset_j + j))

    return pairs