MCPcopy
hub / github.com/dask/dask / _Recurser

Class _Recurser

dask/array/numpy_compat.py:71–149  ·  view source on GitHub ↗

Utility class for recursing over nested iterables

Source from the content-addressed store, hash-verified

69
70
71class _Recurser:
72 """
73 Utility class for recursing over nested iterables
74 """
75
76 # This was copied almost verbatim from numpy.core.shape_base._Recurser
77 # See numpy license at https://github.com/numpy/numpy/blob/master/LICENSE.txt
78 # or NUMPY_LICENSE.txt within this directory
79
80 def __init__(self, recurse_if):
81 self.recurse_if = recurse_if
82
83 def map_reduce(
84 self,
85 x,
86 f_map=lambda x, **kwargs: x,
87 f_reduce=lambda x, **kwargs: x,
88 f_kwargs=lambda **kwargs: kwargs,
89 **kwargs,
90 ):
91 """
92 Iterate over the nested list, applying:
93 * ``f_map`` (T -> U) to items
94 * ``f_reduce`` (Iterable[U] -> U) to mapped items
95
96 For instance, ``map_reduce([[1, 2], 3, 4])`` is::
97
98 f_reduce([
99 f_reduce([
100 f_map(1),
101 f_map(2)
102 ]),
103 f_map(3),
104 f_map(4)
105 ]])
106
107
108 State can be passed down through the calls with `f_kwargs`,
109 to iterables of mapped items. When kwargs are passed, as in
110 ``map_reduce([[1, 2], 3, 4], **kw)``, this becomes::
111
112 kw1 = f_kwargs(**kw)
113 kw2 = f_kwargs(**kw1)
114 f_reduce([
115 f_reduce([
116 f_map(1), **kw2)
117 f_map(2, **kw2)
118 ], **kw1),
119 f_map(3, **kw1),
120 f_map(4, **kw1)
121 ]], **kw)
122 """
123
124 def f(x, **kwargs):
125 if not self.recurse_if(x):
126 return f_map(x, **kwargs)
127 else:
128 next_kwargs = f_kwargs(**kwargs)

Callers 1

blockFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…