# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/90_alls.ipynb.

# %% auto 0
__all__ = ['allmap', 'areinstances', 'alltype', 'allsame', 'allint', 'alldig', 'allstr', 'isints', 'isstrs']

# %% ../nbs/90_alls.ipynb 4
from typing import Iterable, TypeGuard

# %% ../nbs/90_alls.ipynb 5
from .atyp import G, GuardFunc
from .core import isiter, isint, isstr
from .wrap import makeguard
from .strs import isdig

# %% ../nbs/90_alls.ipynb 7
def allmap(x, f: GuardFunc) -> bool:
    '''Maps `TypeGuard` function `f` over `x` and returns `all` of the results'''
    return isiter(x) and all(map(f, x))

def areinstances(x, t: G) -> TypeGuard[Iterable[G]]:
    '''Checks if all elements of `x` are instances of `t`
    
    Notes
    -----
    The following are all functionally equivalent to the current implementation:
    ```python
    allmap(x, vanilla(t))
    allmap(x, lambda y: isinstance(y, t))
    isiter(x) and all(isinstance(y, t) for y in x)
    isiter(x) and all(vanilla(t)(y) for y in x)
    isiter(x) and all(map(lambda y: isinstance(y, t), x))
    
    # NOTE: dtype: TypeLike, isfunc: GuardFunc = isinstance
    isiter(vals) and all(isfunc(val, dtype) for val in vals) 
    ```

    See Also
    --------
    allmap : Maps `f` over `x` and returns `all` of the results
    alltype : Alias for `areinstances`
    '''
    return allmap(x, makeguard(t))

# %% ../nbs/90_alls.ipynb 9
def alltype(x, t: G) -> TypeGuard[Iterable[G]]: 
    '''Checks if all elements of `x` are of type `t`'''
    return areinstances(x, t)

def allsame(x: Iterable) -> TypeGuard[Iterable[G]]:
    '''Checks if all elements of `x` are of the same type'''
    return alltype(x, type(x[0])) if (isiter(x) and len(x) > 0) else True

# %% ../nbs/90_alls.ipynb 11
def allint(x) -> TypeGuard[Iterable[int]]: 
    '''Check if all elements of `x` are an integer
    
    See Also
    --------
    isints : Alias for `allint`
    '''
    return allmap(x, isint)

def alldig(x) -> TypeGuard[Iterable[str]]: 
    '''Check if all elements of `x` are a digit string'''
    return allmap(x, isdig)

def allstr(x) -> TypeGuard[Iterable[str]]: 
    '''Check if all elements of `x` are a string
    
    See Also
    --------
    isstrs : Alias for `allstr`
    '''
    return allmap(x, isstr)

# %% ../nbs/90_alls.ipynb 13
def isints(x) -> TypeGuard[Iterable[int]]:
    '''Check if all elements of `x` are an integer.
     
    See Also
    --------
    allint : Alias for `isints`
    '''
    return allint(x)


def isstrs(x) -> TypeGuard[Iterable[str]]:
    '''Check if all elements of `x` are a string.
     
    See Also
    --------
    allstr : Alias for `isstrs`
    '''
    return allstr(x)
