>>> from Redy.Collections import Traversal, Flow
    >>> lst: Iterable[int] = [[1, 2, 3]]
    >>> x = Flow(lst)[Traversal.flatten_if(lambda _: isinstance(_, list))]
    >>> assert isinstance(x.unbox, Generator) and list(x.unbox) == [1, 2, 3]

def flatten_if(cond: Callable[[Union[T, ActualIterable[T]]], bool]):
    """
    >>> from Redy.Collections import Traversal, Flow
    >>> lst: Iterable[int] = [[1, 2, 3]]
    >>> x = Flow(lst)[Traversal.flatten_if(lambda _: isinstance(_, list))]
    >>> assert isinstance(x.unbox, Generator) and list(x.unbox) == [1, 2, 3]
    """

    def inner(nested: ActualIterable[Union[T, ActualIterable[T]]]) -> ActualIterable[T]:
        for each in nested:
            if cond(each):
                yield from inner(each)
            else:
                yield each

    return inner