MCPcopy
hub / github.com/tensorflow/tfjs / flatten

Function flatten

tfjs-core/src/util.ts:165–193  ·  view source on GitHub ↗
(
    arr: T|RecursiveArray<T>, result: T[] = [], skipTypedArray = false)

Source from the content-addressed store, hash-verified

163 * @doc {heading: 'Util', namespace: 'util'}
164 */
165export function
166flatten<T extends number|boolean|string|Promise<number>|TypedArray>(
167 arr: T|RecursiveArray<T>, result: T[] = [], skipTypedArray = false): T[] {
168 if (result == null) {
169 result = [];
170 }
171 if (typeof arr === 'boolean' || typeof arr === 'number' ||
172 typeof arr === 'string' || base.isPromise(arr) || arr == null ||
173 isTypedArray(arr) && skipTypedArray) {
174 result.push(arr as T);
175 } else if (Array.isArray(arr) || isTypedArray(arr)) {
176 for (let i = 0; i < arr.length; ++i) {
177 flatten(arr[i], result, skipTypedArray);
178 }
179 } else {
180 let maxIndex = -1;
181 for (const key of Object.keys(arr)) {
182 // 0 or positive integer.
183 if (/^([1-9]+[0-9]*|0)$/.test(key)) {
184 maxIndex = Math.max(maxIndex, Number(key));
185 }
186 }
187 for (let i = 0; i <= maxIndex; i++) {
188 // tslint:disable-next-line: no-unnecessary-type-assertion
189 flatten((arr as RecursiveArray<T>)[i], result, skipTypedArray);
190 }
191 }
192 return result;
193}

Callers 4

expectArraysPredicateFunction · 0.90
convertToTensorFunction · 0.90
makeTensorFunction · 0.90
toTypedArrayFunction · 0.70

Calls 3

isTypedArrayFunction · 0.85
maxMethod · 0.80
pushMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…