Array Functions
Functional Operations
Array transforms like map, filter, reduce, and group_by.
Functional programming operations for working with arrays.
map()
Applies a function to each element of an array
Implementation
Uses list comprehension with input validation and rate limiting
Example
doubled = UniCoreFW.map([1, 2, 3], lambda x: x * 2)
Expected output: [2, 4, 6]
Source Code
def map(array, func):
validate_type(array, (list, tuple), "array")
validate_callable(func, "func")
with RateLimiter():
return [func(x) for x in array]
reduce()
Reduces an array to a single value using a function
Implementation
Iteratively applies the function to elements with an accumulator
Example
sum_result = UniCoreFW.reduce([1, 2, 3, 4], lambda acc, x: acc + x, 0)
Expected output: 10
Source Code
def reduce(array, func, initial=None):
result = initial
for x in array:
if result is None:
result = x
else:
result = func(result, x)
return result
filter()
Keeps elements that pass a predicate function
Implementation
Uses list comprehension to include matching elements
Example
evens = UniCoreFW.filter([1, 2, 3, 4, 5], lambda x: x % 2 == 0)
Expected output: [2, 4]
Source Code
def filter(array, func):
return [x for x in array if func(x)]
reject()
Opposite of filter - excludes elements that pass a predicate
Implementation
Uses list comprehension with negated predicate
Example
odds = UniCoreFW.reject([1, 2, 3, 4, 5], lambda x: x % 2 == 0)
Expected output: [1, 3, 5]
Source Code
def reject(array, predicate):
return [x for x in array if not predicate(x)]
find()
Gets the first element that matches a predicate
Implementation
Uses next() with a generator expression and validation
Example
found = UniCoreFW.find([1, 2, 3, 4, 5], lambda x: x > 3)
Expected output: 4
Source Code
def find(array, func):
UniCoreFW.validate_input(array, (list, tuple))
UniCoreFW.validate_input(func, callable)
return next((x for x in array if func(x)), None)
every()
Checks if all elements pass a predicate
Implementation
Uses Python's all() with a generator expression
Example
all_positive = UniCoreFW.every([1, 2, 3], lambda x: x > 0)
Expected output: True
Source Code
def every(array, func):
return all(func(x) for x in array)
some()
Checks if any element passes a predicate
Implementation
Uses Python's any() with a generator expression
Example
has_even = UniCoreFW.some([1, 3, 5, 6], lambda x: x % 2 == 0)
Expected output: True
Source Code
def some(array, func):
return any(func(x) for x in array)
sort_by()
Sorts an array by a key function
Implementation
Uses Python's sorted() with a key function
Example
sorted_list = UniCoreFW.sort_by(["abc", "a", "ab"], lambda s: len(s))
Expected output: ["a", "ab", "abc"]
Source Code
def sort_by(array, key_func):
return sorted(array, key=key_func)
group_by()
Groups array elements by the result of a key function
Implementation
Creates a dictionary with keys from the key function and values as lists of matching elements
Example
grouped = UniCoreFW.group_by([1, 2, 3, 4, 5], lambda x: x % 2 == 0)
Expected output: {True: [2, 4], False: [1, 3, 5]}
Source Code
def group_by(array, key_func):
if not hasattr(array, "__iter__"):
raise TypeError("The 'array' parameter must be iterable.")
if not callable(key_func):
raise TypeError("The 'key_func' parameter must be callable.")
grouped = {}
for item in array:
try:
key = key_func(item)
grouped.setdefault(key, []).append(item)
except Exception:
continue
return grouped
count_by()
Counts elements by the result of a key function
Implementation
Similar to group_by but counts occurrences instead of collecting elements
Example
counts = UniCoreFW.count_by(["a", "b", "c", "a", "b"], lambda x: x)
Expected output: {"a": 2, "b": 2, "c": 1}
Source Code
def count_by(array, key_func):
if not hasattr(array, "__iter__"):
raise TypeError("The 'array' parameter must be iterable.")
if not callable(key_func):
raise TypeError("The 'key_func' parameter must be callable.")
counts = {}
for item in array:
try:
key = key_func(item)
counts[key] = counts.get(key, 0) + 1
except Exception:
continue
return counts
pluck()
Extracts a property from each object in an array
Implementation
Handles both dictionaries and objects with attributes
Example
names = UniCoreFW.pluck([{"name": "John"}, {"name": "Jane"}], "name")
Expected output: ["John", "Jane"]
Source Code
def pluck(array, key):
return [
obj.get(key) if isinstance(obj, dict) else getattr(obj, key, None)
for obj in array
]
invoke()
Calls a method on each item in an array
Implementation
Uses getattr to find and call the method on each item
Example
results = UniCoreFW.invoke(["hello", "world"], "upper")
Expected output: ["HELLO", "WORLD"]
Source Code
def invoke(array, func_name, *args):
return [
getattr(item, func_name, None)(*args) if hasattr(item, func_name) else None
for item in array
]