Sq.zig, A collection of array utility functions for Zig, inspired by Lodash

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.

https://github.com/pacifio/sq

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

7 Likes

I don’t like that there is so much allocation, especially for functions that could be an iterator or some other simple operation already supported by zig.

That doesn’t diminish what you have here, much of it is useful.

3 Likes

To give an example of this concept, compare the chunk function to std.mem.window which doesn’t allocate:

3 Likes

to be very honest I used this for my own personal cases, however I have been thinking about writing a sequential writer, one single allocator and you can chain these utilitly functions like .first().map().zip() etc.

What do you think, it might make the library much more resourceful in my opinion.

Thanks for pointing this out mate, I will try to reduce the number of allocators and also introduce a chain like structure.
And of course, more useful functions, not present in the current std.

This is very cool! You might be interested in Fluent as well, GitHub - andrewCodeDev/Fluent: Fluent interface for REGEX, iteration, and algorithm chaining.