diff --git a/ivy/data_classes/array/general.py b/ivy/data_classes/array/general.py index 7c94e779a673f..62c1454211d72 100644 --- a/ivy/data_classes/array/general.py +++ b/ivy/data_classes/array/general.py @@ -988,7 +988,7 @@ def value_is_nan(self: ivy.Array, /, *, include_infs: bool = True) -> bool: """ return ivy.value_is_nan(self, include_infs=include_infs) - def exists(self: ivy.Array) -> bool: + def exists(self: ivy.Array, /) -> bool: """ ivy.Array instance method variant of ivy.exists. This method simply wraps the function, and so the docstring for ivy.exists also applies to this method with @@ -1002,7 +1002,7 @@ def exists(self: ivy.Array) -> bool: Returns ------- ret - True if x is not None, else False. + True if input is not None, else False. Examples -------- diff --git a/ivy/data_classes/container/general.py b/ivy/data_classes/container/general.py index 8f094f546ade0..6b7dc87e08a66 100644 --- a/ivy/data_classes/container/general.py +++ b/ivy/data_classes/container/general.py @@ -4245,3 +4245,127 @@ def strides( A tuple containing the strides. """ return self.static_strides(self) + + @staticmethod + def _static_exists( + x: ivy.Container, + /, + *, + key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, + to_apply: Union[bool, ivy.Container] = True, + prune_unapplied: Union[bool, ivy.Container] = False, + map_sequences: Union[bool, ivy.Container] = False, + ) -> ivy.Container: + """ + ivy.Container instance method variant of ivy.exists. This method simply wraps + the function, and so the docstring for ivy.exists also applies to this method + with minimal changes. + + Parameters + ---------- + x + The input container. + key_chains + The key-chains to apply or not apply the method to. Default is ``None``. + to_apply + If True, the method will be applied to key_chains, otherwise key_chains + will be skipped. Default is ``True``. + prune_unapplied + Whether to prune key_chains for which the function was not applied. + Default is ``False``. + map_sequences + Whether to also map method to sequences (lists, tuples). + Default is ``False``. + + Returns + ------- + ret + A boolean container detaling if any of the leaf nodes are None. + True if not None, False if None. + + Examples + -------- + >>> x = ivy.Container(a=ivy.array([0,4,5]), b=ivy.array([2,2,0])) + >>> y = x._static_exists(x) + >>> print(y) + { a: True, b: True } + + >>> x = ivy.Container(a=[1,2], b=None) + >>> y = x._static_exists(x) + >>> print(y) + { a: True, b: False } + + >>> x = ivy.Container(a={"d": 1, "c": 3}, b={"d": 20, "c": None}) + >>> y = x._static_exists(x) + >>> print(y) + { a: { c: True, d: True }, b: { c: False, d: True } } + """ + return ContainerBase.cont_multi_map_in_function( + "exists", + x, + key_chains=key_chains, + to_apply=to_apply, + prune_unapplied=prune_unapplied, + map_sequences=map_sequences, + ) + + def exists( + self: ivy.Container, + /, + *, + key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, + to_apply: Union[bool, ivy.Container] = True, + prune_unapplied: Union[bool, ivy.Container] = False, + map_sequences: Union[bool, ivy.Container] = False, + ) -> ivy.Container: + """ + ivy.Container instance method variant of ivy.exists. This method simply wraps + the function, and so the docstring for ivy.exists also applies to this method + with minimal changes. + + Parameters + ---------- + self + The input container. + key_chains + The key-chains to apply or not apply the method to. Default is ``None``. + to_apply + If True, the method will be applied to key_chains, otherwise key_chains + will be skipped. Default is ``True``. + prune_unapplied + Whether to prune key_chains for which the function was not applied. + Default is ``False``. + map_sequences + Whether to also map method to sequences (lists, tuples). + Default is ``False``. + + Returns + ------- + ret + A boolean container detaling if any of the leaf nodes are None. + True if not None, False if None. + + Examples + -------- + >>> x = ivy.Container(a=[1,2,3,4], b=[]) + >>> y = x.exists() + >>> print(y) + { a: True, b: True } + + >>> x = ivy.Container(a=None, b=[1,2]) + >>> y = x.exists() + >>> print(y) + { a: False, b: True } + + >>> x = ivy.Container(a={"d": 1, "c": 3}, b=None) + >>> y = x.exists() + >>> print(y) + { a: { c: True, d: True }, b: False } + """ + return self._static_exists( + self, + key_chains=key_chains, + to_apply=to_apply, + prune_unapplied=prune_unapplied, + map_sequences=map_sequences, + ) diff --git a/ivy/functional/ivy/general.py b/ivy/functional/ivy/general.py index f80a3fae31133..8df26f4055af4 100644 --- a/ivy/functional/ivy/general.py +++ b/ivy/functional/ivy/general.py @@ -1351,7 +1351,7 @@ def has_nans( @handle_exceptions -def exists(x: Any) -> bool: +def exists(x: Any, /) -> bool: """ Check as to whether the input is None or not.