A collection of array utility functions for Zig, inspired by Lodash.
Add SQ to your project using the Zig package manager:
zig fetch --save git+https://github.com/pacifio/sq#mainConfigure your build.zig:
const sq_dep = b.dependency("sq", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("sq", sq_dep.module("sq"));const std = @import("std");
const sq = @import("sq");
pub fn main() !void {
const allocator = std.heap.page_allocator;
// Remove falsey values
const input = [_]i32{ 0, 1, 0, 2, 0, 3 };
const result = try sq.arrays.compact(i32, allocator, &input);
defer allocator.free(result);
// result: [1, 2, 3]
// Get first element
const langs = [_][]const u8{ "Dart", "Javascript", "Swift" };
const first = sq.arrays.head([]const u8, &langs);
// first: "Dart"
}const input = [_]u8{ 'a', 'b', 'c', 'd' };
const result = try sq.arrays.chunk(u8, allocator, &input, 2);
defer sq.arrays.freeChunked(u8, allocator, result);
// result: [['a', 'b'], ['c', 'd']]const input = [_]i32{ 0, 1, 0, 2, 0, 3 };
const result = try sq.arrays.compact(i32, allocator, &input);
defer allocator.free(result);
// result: [1, 2, 3]const input = [_]i32{ 1, 2, 3 };
const result = try sq.arrays.drop(i32, allocator, &input, 1);
defer allocator.free(result);
// result: [2, 3]const arr1 = [_]i32{ 1, 2, 3 };
const arr2 = [_]i32{ 4, 5, 6 };
const input = [_][]const i32{ &arr1, &arr2 };
const result = try sq.arrays.flatten(i32, allocator, &input);
defer allocator.free(result);
// result: [1, 2, 3, 4, 5, 6, 7, 8, 9]const arr1 = [_]i32{ 1, 2, 3 };
const arr2 = [_]i32{ 2, 4, 5 };
const arr3 = [_]i32{ 2, 8, 9 };
const input = [_][]const i32{ &arr1, &arr2, &arr3 };
const result = try sq.arrays.intersection(i32, allocator, &input);
defer allocator.free(result);
// result: [2]const input = [_][]const u8{ "a", "b", "c" };
const result = try sq.arrays.join(allocator, &input, "~");
defer allocator.free(result);
// result: "a~b~c"const square = struct {
fn call(n: i32, _: usize, _: []const i32) i32 {
return n * n;
}
}.call;
const input = [_]i32{ 4, 8 };
const result = try sq.arrays.map(i32, i32, allocator, &input, square);
defer allocator.free(result);
// result: [16, 64]const letters = [_]u8{ 'a', 'b', 'c' };
const numbers = [_]i32{ 1, 2, 3 };
const zipped = try sq.arrays.zip(u8, i32, allocator, &letters, &numbers);
defer allocator.free(zipped);
// zipped: [('a', 1), ('b', 2), ('c', 3)]
const unzipped = try sq.arrays.unzip(u8, i32, allocator, zipped);
defer allocator.free(unzipped[0]);
defer allocator.free(unzipped[1]);
// unzipped: (['a', 'b', 'c'], [1, 2, 3])const input = [_]u8{ 'a', 'b', 'c', 'd' };
const elem = sq.arrays.nth(u8, &input, 2); // 'c'
const last = sq.arrays.nth(u8, &input, -1); // 'd' (negative index from end)chunk(T, allocator, slice, size)- Splits array into groups of specified sizecompact(T, allocator, slice)- Removes falsey values (0, false, null, NaN)compactStrings(allocator, slice)- Removes empty stringsdrop(T, allocator, slice, n)- Removes n elements from beginningdropRight(T, allocator, slice, n)- Removes n elements from endfill(T, slice, value, start, end)- Fills array with value in specified rangeflatten(T, allocator, slice)- Flattens a 2D array into 1Dtake(T, allocator, slice, n)- Takes n elements from beginningtakeRight(T, allocator, slice, n)- Takes n elements from endsliceRange(T, allocator, slice, start, end)- Creates sub-slice from range
head(T, slice)- Gets first element (returns ?T)last(T, slice)- Gets last element (returns ?T)initial(T, allocator, slice)- Gets all elements except lasttail(T, allocator, slice)- Gets all elements except firstnth(T, slice, n)- Gets element at index (supports negative indices)
findIndex(T, slice, element)- Finds first index of elementfindLastIndex(T, slice, element)- Finds last index of elementsortedIndex(T, slice, value)- Finds insertion index for sorted arrayduplicate(T, allocator, slice)- Finds all duplicate elements
intersection(T, allocator, arrays)- Finds common elements across arraysunionArrays(T, allocator, arrays)- Combines unique elements from arrays
zip(T, U, allocator, a, b)- Combines two arrays into pairsunzip(T, U, allocator, pairs)- Splits pairs into two separate arraysjoin(allocator, slice, separator)- Joins strings with separator
map(T, U, allocator, slice, func)- Transforms elements using function
pairs(allocator, pair_slice)- Creates StringHashMap from key-value pairsfreeChunked(T, allocator, chunks)- Helper to free chunked result
Most functions allocate memory and return owned slices. The caller is responsible for freeing the memory:
const result = try sq.arrays.compact(i32, allocator, &input);
defer allocator.free(result);For chunked results, use the helper function:
const result = try sq.arrays.chunk(u8, allocator, &input, 2);
defer sq.arrays.freeChunked(u8, allocator, result);git clone https://github.com/yourusername/sq
cd sq
zig build test