Skip to content

Commit

Permalink
[GraphBolt] recursive_apply_reduce_all. (#7600)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfbalin authored Jul 27, 2024
1 parent 6b60d7a commit 0af92c2
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions python/dgl/graphbolt/internal_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,44 @@ def recursive_apply(data, fn, *args, **kwargs):
return fn(data, *args, **kwargs)


def recursive_apply_reduce_all(data, fn, *args, **kwargs):
"""Recursively apply a function to every element in a container and reduce
the boolean results with all.
If the input data is a list or any sequence other than a string, returns
True if and only if the given function returns True for all elements.
If the input data is a dict or any mapping, returns True if and only if the
given function returns True for values.
If the input data is a nested container, the result will be reduced over the
nested structure where each element is tested recursively.
The first argument of the function will be passed with the individual elements from
the input data, followed by the arguments in :attr:`args` and :attr:`kwargs`.
Parameters
----------
data : any
Any object.
fn : callable
Any function returning a boolean.
args, kwargs :
Additional arguments and keyword-arguments passed to the function.
"""
if isinstance(data, Mapping):
return all(
recursive_apply_reduce_all(v, fn, *args, **kwargs)
for v in data.values()
)
elif isinstance(data, tuple) or is_listlike(data):
return all(
recursive_apply_reduce_all(v, fn, *args, **kwargs) for v in data
)
else:
return fn(data, *args, **kwargs)


def download(
url,
path=None,
Expand Down

0 comments on commit 0af92c2

Please sign in to comment.