"""Collection utilities for mmar_envops package."""

from collections.abc import Callable
from typing import TypeVar

T = TypeVar("T")


def rfind(items: list[T], func: Callable[[T], bool]) -> int | None:
    """Find the last index in a list where func(item) is True.

    Args:
        items: List to search
        func: Predicate function

    Returns:
        Index of the last matching item, or None if not found
    """
    for ii in range(len(items) - 1, -1, -1):
        item = items[ii]
        if func(item):
            return ii
    return None
