U UniCore Community Public content rendered for search, speed, and sharing

Array Functions

Basic Operations

Core array operations like first, last, uniq, and compact.

Core array operations for manipulating and accessing array elements.

first()

Returns the first element of an array or the first n elements if specified

Implementation

Handles various input patterns including direct array or positional arguments

Example

first_elem = UniCoreFW.first([1, 2, 3, 4, 5])

Expected output: 1

Source Code

def first(*args, **kwargs):
    n = kwargs.get("n", None)
    if len(args) == 0:
        return None if n is None else []
    if n is None and isinstance(args[-1], int):
        n = args[-1]
        args = args[:-1]
    if len(args) == 0 or args[0] is None or (len(args) == 1 and isinstance(args[0], (list, tuple)) and not args[0]):
        return None if n is None else []
    if len(args) == 1 and isinstance(args[0], (list, tuple)):
        array = args[0]
    else:
        array = list(args)
    if n is None:
        return array[0] if array else None
    if not isinstance(n, int) or n <= 0:
        return []
    if n == 1:
        return array[0] if array else None
    return array[:n]

last()

Returns the last element of an array or the last n elements if specified

Implementation

Similar to first but operates from the end of the array

Example

last_elem = UniCoreFW.last([1, 2, 3, 4, 5])

Expected output: 5

Source Code

def last(*args, **kwargs):
    n = kwargs.get("n", None)
    if not args:
        return None if n is None else []
    if len(args) == 1:
        array = args[0]
        if array is None or (isinstance(array, (list, tuple)) and not array):
            return None if n is None else []
        elif not isinstance(array, (list, tuple)):
            array = [array]
    elif len(args) == 2 and isinstance(args[0], (list, tuple)) and isinstance(args[1], int) and n is None:
        array = args[0]
        n = args[1]
    else:
        array = list(args)
    if n is None:
        return array[-1] if array else None
    if not isinstance(n, int) or n <= 0:
        return []
    return array[-n:]

rest()

Returns all elements except the first n elements

Implementation

Uses array slicing with the specified offset

Example

rest_elems = UniCoreFW.rest([1, 2, 3, 4, 5], 2)

Expected output: [3, 4, 5]

Source Code

def rest(array, n=1):
    return array[n:]

initial()

Returns all elements except the last n elements

Implementation

Uses negative slicing to exclude the last n elements

Example

initial_elems = UniCoreFW.initial([1, 2, 3, 4, 5], 2)

Expected output: [1, 2, 3]

Source Code

def initial(array, n=1):
    return array[:-n] if n < len(array) else []

uniq()

Removes duplicates from an array

Implementation

Converts to set to remove duplicates and back to list

Example

unique_values = UniCoreFW.uniq([1, 2, 2, 3, 4, 4, 5])

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

Source Code

def uniq(array):
    return list(set(array))

compact()

Removes falsey values from an array

Implementation

Uses list comprehension with boolean evaluation

Example

compacted = UniCoreFW.compact([0, 1, False, 2, '', 3, None])

Expected output: [1, 2, 3]

Source Code

def compact(array):
    return [x for x in array if x]

chunk()

Splits an array into chunks of specified size

Implementation

Uses list comprehension with array slicing

Example

chunks = UniCoreFW.chunk([1, 2, 3, 4, 5, 6], 2)

Expected output: [[1, 2], [3, 4], [5, 6]]

Source Code

def chunk(array, size):
    return [array[i : i + size] for i in range(0, len(array), size)]

contains()

Checks if a value is present in the array

Implementation

Uses the 'in' operator for existence check

Example

has_value = UniCoreFW.contains([1, 2, 3, 4], 3)

Expected output: True

Source Code

def contains(array, value):
    return value in array

without()

Returns an array excluding all provided values

Implementation

Uses list comprehension to filter out specified values

Example

filtered = UniCoreFW.without([1, 2, 3, 4, 5], 2, 4)

Expected output: [1, 3, 5]

Source Code

def without(array, *values):
    return [x for x in array if x not in values]

shuffle()

Randomly shuffles the values in an array

Implementation

Uses the Fisher-Yates shuffle algorithm with cryptographically secure randomness

Example

shuffled = UniCoreFW.shuffle([1, 2, 3, 4, 5])

Expected output: e.g., [3, 1, 5, 2, 4] (random order)

Source Code

def shuffle(array):
    array_copy = list(array)
    for i in range(len(array_copy) - 1, 0, -1):
        j = secrets.randbelow(i + 1)
        array_copy[i], array_copy[j] = array_copy[j], array_copy[i]
    return array_copy

sample()

Returns a random sample from an array

Implementation

Uses Python's random.sample with bounds checking

Example

sample = UniCoreFW.sample([1, 2, 3, 4, 5], 2)

Expected output: e.g., [2, 4] (random selection)

Source Code

def sample(array, n=1):
    from random import sample
    return sample(array, n if n < len(array) else len(array))