sndt

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2025 License: MIT Imports: 2 Imported by: 0

README

SliceNDice 🍰

SliceNDice is a Go library that brings JavaScript-like Array methods to Go slices, making collection manipulation more expressive and functional.

🚀 Installation

go get github.com/MrSnakeDoc/snd

📋 Prerequisites

  • Go 1.24+ (required for generics and use of new built-in functions like min, max, etc.)

📖 Usage

package main

import (
	"fmt"
	
	"github.com/MrSnakeDoc/snd"
)

func main() {
	// Example with Map
	numbers := []int{1, 2, 3, 4, 5}
	doubled := snd.Map(numbers, func(x int) int {
		return x * 2
	})
	fmt.Println(doubled) // Output: [2 4 6 8 10]
	
	// Example with Filter
	evens := snd.Filter(numbers, func(x int) bool {
		return x%2 == 0
	})
	fmt.Println(evens) // Output: [2 4]
	
	// Example with Reduce
	sum := snd.Reduce(numbers, 0, func(acc, x int) int {
		return acc + x
	})
	fmt.Println(sum) // Output: 15
	
	// Example with ForEach
	snd.ForEach(numbers, func(x int) {
		fmt.Printf("%d ", x)
	}) // Output: 1 2 3 4 5
	
	// Combining operations
	result := snd.Reduce(
		snd.Filter(
			snd.Map(numbers, func(x int) int { return x * x }),
			func(x int) bool { return x > 10 },
		),
		0,
		func(acc, x int) int { return acc + x },
	)
	fmt.Println("\nSum of squares greater than 10:", result) // Output: 41 (16 + 25)
}

🛠️ Available Functions

This library provides the following functions to work with slices:

🔄 Transformation Functions
Function Description JS Equivalent
Map Transforms each element in a slice using the provided function Array.map()
FlatMap Maps each element using a function, then flattens the result Array.flatMap()
Flat Flattens a nested slice structure Array.flat()
Reverse Returns a new slice with elements in reversed order Array.reverse()
ToReversed Returns a new slice with elements in reversed order Array.toReversed()
Sort Sorts the elements in a slice (modifies the original slice) Array.sort()
With Returns a new slice with a specified element replaced at a given index Array.with()
Concat Concatenates multiple slices into one Array.concat()
Fill Fills all elements in a slice with a static value Array.fill()
CopyWithin Copies part of a slice to another position in-place Array.copyWithin()
Splice Removes and/or adds elements at a given index Array.splice()
🔄 Iteration Functions
Function Description JS Equivalent
ForEach Executes a provided function once for each slice element Array.forEach()
Reduce Reduces a slice to a single value by applying a function against an accumulator Array.reduce()
ReduceRight Like Reduce but processes the slice from right to left Array.reduceRight()
🔍 Search Functions
Function Description JS Equivalent
Find Returns the first element that satisfies the provided testing function Array.find()
FindIndex Returns the index of the first element that satisfies the provided testing function Array.findIndex()
Filter Creates a new slice with all elements that pass the test function Array.filter()
Every Tests whether all elements in the slice pass the provided function Array.every()
Some Tests whether at least one element in the slice passes the provided function Array.some()
Includes Determines whether a slice includes a certain value Array.includes()
IndexOf Returns the first index at which a given element can be found Array.indexOf()
EveryIndex Returns the index of the first element that satisfies the provided testing function Array.everyIndex()
✏️ Modification Functions
Function Description JS Equivalent
Pop Removes the last element from a slice and returns that element Array.pop()
Shift Removes the first element from a slice and returns that element Array.shift()
Unshift Adds one or more elements to the beginning of a slice Array.unshift()
Slice Returns a shallow copy of a portion of a slice Array.slice()
🗂️ Object Functions
Function Description JS Equivalent
Keys Returns the keys of a map Object.keys()
Values Returns the values of a map Object.values()
Entries Returns key-value pairs from a map as a slice of structs Object.entries()
📝 String Functions
Function Description JS Equivalent
Join Joins all elements of a slice into a string Array.join()
ToString Converts a slice to its string representation Array.toString()

🤔 Why SliceNDice?

Go is a powerful but minimalist language. Its standard library offers basic functionality for working with slices, but often lacks the higher-order operations found in other languages like JavaScript. SliceNDice bridges this gap by providing a familiar and functional API for slice manipulation.

🔧 Performance

SliceNDice is designed to be efficient while offering an elegant API. All functions use Go generics, which means there's no runtime cost associated with type assertions or reflections.

🤝 Contributing

Contributions are welcome! Feel free to open an issue or submit a pull request.

📄 License

This project is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Concat

func Concat[T any](slices ...[]T) []T

func CopyWithin

func CopyWithin[T any](in []T, target int, start int, end ...int) []T

func Entries

func Entries[K comparable, V any](m map[K]V) []struct {
	Key   K
	Value V
}

func Every

func Every[T any](in []T, pred func(T) bool) bool

func EveryIndex

func EveryIndex[T any](in []T, pred func(T) bool) (bool, int)

func Fill

func Fill[T any](template []T, value T) []T

func Filter

func Filter[T any](in []T, keep func(T) bool) []T

func Find

func Find[T any](in []T, pred func(T) bool) (T, bool)

func FindIndex

func FindIndex[T any](in []T, pred func(T) bool) int

func Flat

func Flat[T any](chunks [][]T) []T

func FlatMap

func FlatMap[T any, R any](in []T, fn func(T) []R) []R

func ForEach

func ForEach[T any](in []T, fn func(T))

func Includes

func Includes[T comparable](in []T, target T) bool

func IndexOf

func IndexOf[T comparable](in []T, target T) int

func IndexOfFunc

func IndexOfFunc[T any](in []T, pred func(T) bool) int

func Join

func Join[T fmt.Stringer](in []T, sep string) string

func Keys

func Keys[K comparable, V any](m map[K]V) []K

func LastIndexOf

func LastIndexOf[T comparable](in []T, target T) int

func LastIndexOfFunc

func LastIndexOfFunc[T any](in []T, pred func(T) bool) int

func Map

func Map[T any, R any](in []T, f func(T) R) []R

func Pop

func Pop[T any](in []T) (T, []T)

func PopFree

func PopFree[T any](in []T) (T, []T)

func Reduce

func Reduce[T, R any](in []T, init R, fn func(R, T) R) R

func ReduceRight

func ReduceRight[T, R any](in []T, init R, fn func(R, T) R) R

func Reverse

func Reverse[T any](in []T) []T

func Shift

func Shift[T any](in []T) (T, []T)

func Slice

func Slice[T any](in []T, start, end int) []T

func Some

func Some[T any](in []T, pred func(T) bool) bool

func Sort

func Sort[T any](in []T, less func(a, b T) bool)

func Splice

func Splice[T any](in []T, start, deleteCount int, items ...T) []T

func ToReversed

func ToReversed[T any](in []T) []T

func ToString

func ToString[T any](in []T) string

func Unshift

func Unshift[T any](in []T, item T) []T

func Values

func Values[K comparable, V any](m map[K]V) []V

func With

func With[T any](in []T, idx int, val T) []T

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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