invoke_map()
Invokes the given iteratee function on each element of the given collection and returns an array of the results. The iteratee is invoked with the elements of the collection as the first argument and any additional arguments provided to invoke_map as additional arguments.
Implementation
Args: collection: The collection to process. Should be a list. method: The function to invoke on each element. Can be a string (in which case getattr is used to get the method from the element) or a callable. *args: Additional arguments to pass to the iteratee. **kwargs: Additional keyword arguments to pass to the iteratee. Returns: A list of the results of invoking the iteratee on each element of the collection.
Example
invoke_map([1, 2, 3], lambda x: x * 2)
Expected output: [2, 4, 6]
Source Code
def invoke_map(
collection: List[Any], method: Union[str, Callable], *args, **kwargs
) -> List[Any]:
result: List[Any] = []
for x in collection:
if isinstance(method, str):
fn = getattr(x, method)
result.append(fn(*args, **kwargs))
else:
result.append(method(x, *args, **kwargs))
return result