find()
Find the first element in the array that matches the predicate function.
Implementation
Args: array: The array to search func: A predicate function that returns True for a match Returns: The first matching element, or None if not found Raises: TypeError: If array is not iterable or func is not callable
Example
find([1, 2, 3], lambda x: x % 2 == 0)
Expected output: 2
Source Code
def find(array: List[T], func: Callable[[T], bool]) -> Optional[T]:
# Input validation
if not hasattr(array, "__iter__"):
raise TypeError("The 'array' parameter must be iterable.")
if not callable(func):
raise TypeError("The 'func' parameter must be callable.")
for x in array:
try:
if func(x):
return x
except Exception:
# Handle exceptions securely
# Optionally log the exception without exposing sensitive information
continue # Skip elements that cause exceptions
return None