from collections import deque
from collections.abc import Iterable, Generator
import numpy as np


def shingle(sequence: Iterable, size: int) -> Generator[np.ndarray, None, None]:
    """
    Generator that yields shingles (a rolling window) of a given size.

    Parameters
    ----------
    sequence : iterable
               Sequence to be shingled
    size : int
           size of shingle (window)
    """
    iterator = iter(sequence)
    init = (next(iterator) for _ in range(size))
    window = deque(init, maxlen=size)
    if len(window) < size:
        raise IndexError('Sequence smaller than window size')
    yield np.asarray(window)
    for elem in iterator:
        window.append(elem)
        yield np.asarray(window)
