MCPcopy Create free account
hub / github.com/dask/dask / _Recurser

Class _Recurser

dask/array/numpy_compat.py:67–145  ·  view source on GitHub ↗

Utility class for recursing over nested iterables

Source from the content-addressed store, hash-verified

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

Callers 1

blockFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected