from functools import reduce


def class_recall(queried_dataset, top_indices, query_image_class):
    """Computes the recall for the results of the given top distances query.

    Args:
      queried_dataset: the dataset that was queried. It must have the exact same classes as the query dataset.
    It is used to retrieve the classes for the given `top_indices`.
      top_indices: a list with the indices, in the `queried_dataset`, of the most similar images to the
    query image.
      query_image_class: the class index (not name) of the query image.

    Returns:
      the class recall, which corresponds to the percentage of the retrieved results (the given most similar
      images to the query image) have the same class as the query image

    """
    # noinspection PyTypeChecker
    return reduce(lambda part, i: part + queried_dataset[i][1] == query_image_class, top_indices, 0) / len(top_indices)