UniCoreFW

mapcat()

Map over an array, concatenating the results.

Implementation

Args: array: The array to map over. func: A function that takes an element and its index, and returns an iterable of values to be concatenated into the result. Returns: A new array with the concatenated results.

Example

mapcat([1, 2, 3], lambda x, _: [x, x])

Expected output: [1, 1, 2, 2, 3, 3]

Source Code

def mapcat(array: List[T], func: Callable[[T, int], List[U]]) -> List[U]: result: List[U] = [] for idx, x in enumerate(array): res = func(x, idx) # type: ignore result.extend(res or []) return result