slicex

package
v0.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 30, 2025 License: MIT Imports: 4 Imported by: 0

README

SliceX

Type-safe, modern helpers for slice operations and manipulation in Go.

Usage Example

package main

import (
    "fmt"
    "github.com/kaptinlin/gozod/pkg/slicex"
)

func main() {
    // Sample slice
    vals := []int{1, 2, 3}

    // Conversion
    anySlice, _ := slicex.ToAny(vals)         // []any{1,2,3}
    intSlice, _ := slicex.ToTyped[int](anySlice) // []int{1,2,3}
    strSlice, _ := slicex.ToStrings(vals)     // []string{"1","2","3"}

    // Extraction
    if s, ok := slicex.Extract(vals); ok {
        fmt.Println(s) // [1 2 3]
    }

    // Operations
    merged, _ := slicex.Merge([]int{1,2}, []int{3,4}) // [1 2 3 4]
    appended, _ := slicex.Append([]int{1,2}, 3, 4)    // [1 2 3 4]
    reversed, _ := slicex.Reverse([]int{1,2,3})       // [3 2 1]
    unique, _ := slicex.Unique([]int{1,2,2,3})        // [1 2 3]

    // Functional
    evens, _ := slicex.Filter([]int{1,2,3,4}, func(v any) bool {
        return v.(int)%2 == 0
    })
    fmt.Println(evens) // [2 4]
}

Quick Reference

import "github.com/kaptinlin/gozod/pkg/slicex"

// Conversion
slicex.ToAny(slice)           // []any
slicex.ToTyped[T](slice)      // []T
slicex.ToStrings(slice)       // []string
slicex.StringToChars(str)     // []any (chars)

// Extraction
slicex.Extract(val)           // (slice, ok)
slicex.ExtractArray(val)      // (array, ok)
slicex.ExtractSlice(val)      // (slice, ok)

// Operations
slicex.Merge(a, b)            // merge slices
slicex.Append(slice, vals...) // append
slicex.Prepend(slice, vals...)// prepend

// Utility
slicex.Length(slice)          // length
slicex.IsEmpty(slice)         // bool
slicex.Contains(slice, v)     // bool
slicex.IndexOf(slice, v)      // int
slicex.Reverse(slice)         // reversed
slicex.Unique(slice)          // deduped
slicex.Join(slice, sep)       // string

// Functional
slicex.Filter(slice, fn)      // filter
slicex.Map(slice, fn)         // map/transform

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidReflectValue   = errors.New("invalid reflect value")
	ErrNotSliceArrayOrString = errors.New("input is not a slice, array, or string")

	ErrCannotConvertSliceElement = errors.New("cannot convert slice element")
	ErrCannotConvertFirstSlice   = errors.New("cannot convert first slice")
	ErrCannotConvertSecondSlice  = errors.New("cannot convert second slice")
	ErrCannotConvertSlice        = errors.New("cannot convert slice")
)

Sentinel errors for slicex package.

Functions

func Append

func Append(slice any, elements ...any) (any, error)

Append appends elements to a slice

func Contains

func Contains(slice any, value any) bool

Contains checks if a slice contains a specific value

func Extract

func Extract(input any) ([]any, bool)

Extract extracts slice from input, returns the slice and whether extraction was successful

func ExtractArray

func ExtractArray(input any) ([]any, bool)

ExtractArray extracts array from input, returns the slice and whether extraction was successful

func ExtractSlice

func ExtractSlice(input any) ([]any, bool)

ExtractSlice extracts slice from input, returns the slice and whether extraction was successful

func Filter

func Filter(slice any, predicate func(any) bool) (any, error)

Filter filters a slice based on a predicate function

func FromReflect

func FromReflect(rv reflect.Value) ([]any, error)

FromReflect converts reflect.Value to []any

func IndexOf

func IndexOf(slice any, value any) int

IndexOf returns the index of the first occurrence of value in slice

func IsEmpty

func IsEmpty(input any) bool

IsEmpty checks if a slice is empty

func Join

func Join(slice any, separator string) string

Join joins slice elements with a separator

func Length

func Length(input any) (int, error)

Length returns the length of a slice or array

func Map

func Map(slice any, mapper func(any) any) (any, error)

Map transforms each element of a slice using a mapper function

func Merge

func Merge(a, b any) (any, error)

Merge merges two slices of any type

func Prepend

func Prepend(slice any, elements ...any) (any, error)

Prepend prepends elements to a slice

func Reverse

func Reverse(slice any) (any, error)

Reverse reverses a slice

func StringToChars

func StringToChars(s string) []any

StringToChars converts a string to []any of characters

func ToAny

func ToAny(input any) ([]any, error)

ToAny converts any slice/array to []any

func ToStrings

func ToStrings(input any) ([]string, error)

ToStrings converts any slice to []string

func ToTyped

func ToTyped[T any](input any) ([]T, error)

ToTyped converts any slice to []T using generics

func Unique

func Unique(slice any) (any, error)

Unique removes duplicate elements from a slice

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL