cuts

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2025 License: MIT Imports: 4 Imported by: 1

README

cuts

GoDoc Build Status

Provides useful, generic utilities for working with slices.

Install

go get github.com/ejfrick/cuts

Usage

package main

import (
	"fmt"
	"github.com/ejfrick/cuts"
	"strings"
	"time"
)

func main() {
	// dedupe some values
	words := []string{"hello", "hello", "world"}
	dedupedWords := cuts.Dedupe(words)
	fmt.Println(dedupedWords) // ["hello", "world"]

	// chunk a slice into smaller slices
	bigList := []string{"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}
	chunks := cuts.ChunkBy(bigList, 2) // chunk into lists of two elements
	fmt.Println(chunks)                // [["the", "quick"], ["brown", "fox"], ["jumps", "over"], ["the", "lazy"], ["dog"]]

	nums := []int{0, 1, 2, 3, 4, 5, 6, 7, 8}

	// get the first element that is even
	index, found := cuts.FirstWhere(nums, func(val int) bool { return val%2 == 0 })
	fmt.Println(index, found) // 2, true

	// get the last element that is odd
	index, found = cuts.LastWhere(nums, func(val int) bool { return val%2 == 1 })
	fmt.Println(index, found) // 7, true

	// get the last element that is greater than 10
	index, found = cuts.LastWhere(nums, func(val int) bool { return val > 10 })
	fmt.Println(index, found) // -1, false

	// check if any words in the slice contain the letter e
	ok := cuts.AnyWhere(bigList, func(val string) bool { return strings.Contains(val, "e") })
	fmt.Println(ok) // true

	// check if all words in the slice contain the letter e
	// fails fast!
	ok = cuts.AllWhere(bigList, func(val string) bool { return strings.Contains(val, "e") })
	fmt.Println(ok) // false
	
	// get closest value to target value
	acceptableTimes := []time.Duration{time.Hour, time.Hour * 6, time.Hour * 12}
	target := time.Hour * 7
	closest := cuts.SnapTo(acceptableTimes, target)
	fmt.Println(closest.String()) // 6h0m0s

}

See the cuts package documentation for more usage information.

License

MIT

Documentation

Overview

Package cuts provides useful, generic utilities for working with slices.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllWhere

func AllWhere[S ~[]E, E any](vals S, where func(val E) bool) bool

AllWhere returns whether all elements in the slice satisfy the condition function.

func AnyWhere

func AnyWhere[S ~[]E, E any](vals S, where func(val E) bool) bool

AnyWhere returns whether any element in the slice satisfies the condition function.

func ChunkBy

func ChunkBy[T any](items []T, chunkSize int) (chunks [][]T)

ChunkBy groups an array of items into batches of the given size.

func Dedupe

func Dedupe[T comparable](in []T) []T

Dedupe removes duplicate values from an array of comparable elements. It preserves the order of the original array.

func DedupeFunc added in v0.0.2

func DedupeFunc[T any, E comparable](in []T, cmp func(t T) E) []T

DedupeFunc removes duplicate values from an array of elements of type T, from which a comparable type E can be derived using the provided function. It does not preserve the order of the original array.

func FirstWhere

func FirstWhere[S ~[]E, E any](vals S, where func(val E) bool) (int, bool)

FirstWhere searches for the first element in a sorted slice that meets the condition function and returns its position. If no such element exists, returns -1; it also returns a bool saying whether an element matching the condition was found in the slice. The slice must be sorted in increasing order.

func LastWhere

func LastWhere[S ~[]E, E any](vals S, where func(val E) bool) (int, bool)

LastWhere searches for the last element in a sorted slice that meets the condition function and returns its position. If no such element exists, returns -1; it also returns a bool saying whether an element matching the condition was found in the slice. The slice must be sorted in increasing order.

func SnapTo

func SnapTo[S ~[]E, E constraints.Integer | constraints.Float](vals S, target E) E

SnapTo returns the element in an array that is closest to the target.

func SnapToFunc

func SnapToFunc[S ~[]E, E any](vals S, target E, cmp func(E, E) int, closest func(tgt, nxt, prv E) E) E

SnapToFunc returns the element in an array that is closest to the target, given a custom comparison and closest function.

Types

This section is empty.

Directories

Path Synopsis
stringsnap module

Jump to

Keyboard shortcuts

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