<commit_before>"""
List-related functions and classes.
"""
import sys

from taipan._compat import imap, xrange
from taipan.collections import ensure_iterable, ensure_sequence
from taipan.functional import ensure_callable, ensure_keyword_args


__all__ = [
    'head', 'last', 'tail', 'init',
    'index', 'lastindex',
    'intersperse', 'intercalate', 'concat', 'join', 'flatten',
]


# Element access

def head(list_):
    """Returns head of a list, i.e. it's first element."""
    ensure_sequence(list_)
    return list_[0]


def last(list_):
    """Returns last element of a list."""
    ensure_sequence(list_)
    return list_[-1]


def tail(list_):
    """Returns tail of a list (all elements without the first one)."""
    ensure_sequence(list_)
    if not list_:
        raise ValueError("can't tail an empty list")
    return list(list_[1:])


def init(list_):
    """Returns all the elements of a list except the last one."""
    ensure_sequence(list_)
    if not list_:
        raise ValueError("can't extract initial part of an empty list")
    return list(list_[:-1])


# Searching for elements

def index(*args, **kwargs):
    """Search a list for an exact element, or element satisfying a predicate.

    Usage::

        index(element, list_)
        index(of=element, in_=list_)
        index(where=predicate, in_=list_)

    :param element, of: Element to search for
    :param where: Predicate defining an element to search for.
                  This should be a callable taking a single argument
                  and returning a boolean result.
    :param list_, in_: List to search in

    :return: Index of first matching element, or -1 if none was found

    .. versionadded:: 0.0.3
    """
    return _index(*args, start=0, step=1, **kwargs)


def lastindex(*args, **kwargs):
    """Search a list backwards for an exact element,
    or element satisfying a predicate.

    Usage::

        lastindex(element, list_)
        lastindex(of=element, in_=list_)
        lastindex(where=predicate, in_=list_)

    :param element, of: Element to search for
    :param where: Predicate defining an element to search for.
                  This should be a callable taking a single argument
                  and returning a boolean result.
    :param list_, in_: List to search in

    :return: Index of the last matching element, or -1 if none was found

    .. versionadded:: 0.0.3
    """
    return _index(*args, start=sys.maxint, step=-1, **kwargs)


def _index(*args, **kwargs):
    """Implementation of list searching.

    :param of: Element to search for
    :param where: Predicate to search for
    :param in_: List to search in<commit_msg>Fix Python 3 issue in .collections.lists.lastindex<commit_after>"""
List-related functions and classes.
"""
import sys

from taipan._compat import imap, xrange
from taipan.collections import ensure_iterable, ensure_sequence
from taipan.functional import ensure_callable, ensure_keyword_args


__all__ = [
    'head', 'last', 'tail', 'init',
    'index', 'lastindex',
    'intersperse', 'intercalate', 'concat', 'join', 'flatten',
]


# Element access

def head(list_):
    """Returns head of a list, i.e. it's first element."""
    ensure_sequence(list_)
    return list_[0]


def last(list_):
    """Returns last element of a list."""
    ensure_sequence(list_)
    return list_[-1]


def tail(list_):
    """Returns tail of a list (all elements without the first one)."""
    ensure_sequence(list_)
    if not list_:
        raise ValueError("can't tail an empty list")
    return list(list_[1:])


def init(list_):
    """Returns all the elements of a list except the last one."""
    ensure_sequence(list_)
    if not list_:
        raise ValueError("can't extract initial part of an empty list")
    return list(list_[:-1])


# Searching for elements

def index(*args, **kwargs):
    """Search a list for an exact element, or element satisfying a predicate.

    Usage::

        index(element, list_)
        index(of=element, in_=list_)
        index(where=predicate, in_=list_)

    :param element, of: Element to search for
    :param where: Predicate defining an element to search for.
                  This should be a callable taking a single argument
                  and returning a boolean result.
    :param list_, in_: List to search in

    :return: Index of first matching element, or -1 if none was found

    .. versionadded:: 0.0.3
    """
    return _index(*args, start=0, step=1, **kwargs)


def lastindex(*args, **kwargs):
    """Search a list backwards for an exact element,
    or element satisfying a predicate.

    Usage::

        lastindex(element, list_)
        lastindex(of=element, in_=list_)
        lastindex(where=predicate, in_=list_)

    :param element, of: Element to search for
    :param where: Predicate defining an element to search for.
                  This should be a callable taking a single argument
                  and returning a boolean result.
    :param list_, in_: List to search in

    :return: Index of the last matching element, or -1 if none was found

    .. versionadded:: 0.0.3
    """
    return _index(*args, start=sys.maxsize, step=-1, **kwargs)


def _index(*args, **kwargs):
    """Implementation of list searching.

    :param of: Element to search for
    :param where: Predicate to search for
    :param in_: List to search in
{'fraction_non_alphanumeric': 0.1032750279121697, 'fraction_numerical': 0.0057685150725716415, 'mean_word_length': 3.0022338049143706, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3367910', 'n_tokens_mistral': 1590, 'n_tokens_neox': 1512, 'n_words': 604}