Skip to content

Commit

Permalink
Refactor exists (ivy-llc#22993)
Browse files Browse the repository at this point in the history
Co-authored-by: @AnnaTz
  • Loading branch information
SpyingEnvelope authored and druvdub committed Oct 14, 2023
1 parent c502062 commit 03474fb
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 3 deletions.
4 changes: 2 additions & 2 deletions ivy/data_classes/array/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
--------
Expand Down
124 changes: 124 additions & 0 deletions ivy/data_classes/container/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
2 changes: 1 addition & 1 deletion ivy/functional/ivy/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 03474fb

Please sign in to comment.