Thanks to visit codestin.com
Credit goes to github.com

Skip to content

pacifio/sq

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SQ (Sequential)

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

Installation

Add SQ to your project using the Zig package manager:

zig fetch --save git+https://github.com/pacifio/sq#main

Configure your build.zig:

const sq_dep = b.dependency("sq", .{
    .target = target,
    .optimize = optimize,
});

exe.root_module.addImport("sq", sq_dep.module("sq"));

Quick Start

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"
}

Usage Examples

Chunk - Split into Groups

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']]

Compact - 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]

Drop - Remove from Beginning

const input = [_]i32{ 1, 2, 3 };
const result = try sq.arrays.drop(i32, allocator, &input, 1);
defer allocator.free(result);
// result: [2, 3]

Flatten - Flatten 2D Array

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]

Intersection - Find Common Elements

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]

Join - Join Strings

const input = [_][]const u8{ "a", "b", "c" };
const result = try sq.arrays.join(allocator, &input, "~");
defer allocator.free(result);
// result: "a~b~c"

Map - Transform Elements

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]

Zip and Unzip

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])

Nth - Get Element at Index

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)

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

Memory Management

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);

Building from Source

git clone https://github.com/yourusername/sq
cd sq
zig build test

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages