A collection of array utility functions for Zig, inspired by Lodash. Found myself using these utilities again and again so thought it might be handy to the community.
API Reference
Array Manipulation
-
chunk(T, allocator, slice, size)- Splits array into groups of specified size -
compact(T, allocator, slice)- Removes falsey values (0, false, null, NaN) -
compactStrings(allocator, slice)- Removes empty strings -
drop(T, allocator, slice, n)- Removes n elements from beginning -
dropRight(T, allocator, slice, n)- Removes n elements from end -
fill(T, slice, value, start, end)- Fills array with value in specified range -
flatten(T, allocator, slice)- Flattens a 2D array into 1D -
take(T, allocator, slice, n)- Takes n elements from beginning -
takeRight(T, allocator, slice, n)- Takes n elements from end -
sliceRange(T, allocator, slice, start, end)- Creates sub-slice from range
Element Access
-
head(T, slice)- Gets first element (returns ?T) -
last(T, slice)- Gets last element (returns ?T) -
initial(T, allocator, slice)- Gets all elements except last -
tail(T, allocator, slice)- Gets all elements except first -
nth(T, slice, n)- Gets element at index (supports negative indices)
Search and Query
-
findIndex(T, slice, element)- Finds first index of element -
findLastIndex(T, slice, element)- Finds last index of element -
sortedIndex(T, slice, value)- Finds insertion index for sorted array -
duplicate(T, allocator, slice)- Finds all duplicate elements
Set Operations
-
intersection(T, allocator, arrays)- Finds common elements across arrays -
unionArrays(T, allocator, arrays)- Combines unique elements from arrays
Combination
-
zip(T, U, allocator, a, b)- Combines two arrays into pairs -
unzip(T, U, allocator, pairs)- Splits pairs into two separate arrays -
join(allocator, slice, separator)- Joins strings with separator
Transformation
map(T, U, allocator, slice, func)- Transforms elements using function
Utilities
-
pairs(allocator, pair_slice)- Creates StringHashMap from key-value pairs -
freeChunked(T, allocator, chunks)- Helper to free chunked result