utils

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2026 License: Apache-2.0 Imports: 0 Imported by: 1

README

Handy-dandy Utils

Meet our trusty companion, the 'utils' package! It's brimming with handy utilities, ready to be put to work across any of the fantastic products over at Alis Build. Aren't they quite the jack-of-all-trades? 😎

Subpackages

Installation

Get the package

go get go.alis.build/utils

Import the package

import "go.alis.build/utils"

Documentation

Overview

Package utils provides a lightweight set of utilities that can be used across products on Alis Build.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chunk added in v0.0.4

func Chunk[T any](arr []T, size int) [][]T

Chunk is a utility function for splitting a slice into chunks of a given size.

It takes a slice and a chunk size as arguments and returns a slice of slices, where each slice has at most the given chunk size.

For example, you can use it to split a slice of integers into chunks of size 2:

ints := []int{1, 2, 3, 4, 5}
chunks := Chunk(ints, 2)
// chunks = [[1, 2], [3, 4], [5]]

func Contains

func Contains[T comparable](s []T, searchTerm T) bool

Contains function is a nifty tool! It returns true if it finds the value you're looking for in the array. And guess what? It can handle any type, all thanks to the power of Go's Generics!

func Filter added in v0.0.4

func Filter[T any](arr []T, fn func(T) bool) []T

Filter is a utility function for filtering elements that satisfy a given predicate from a slice.

It takes a slice and a predicate function as arguments. The predicate function should return true if the element satisfies the condition, and false otherwise.

It returns a new slice containing only the elements that satisfy the condition.

func Find added in v0.0.4

func Find[T any](arr []T, fn func(T) bool) (T, int, bool)

Find is a utility function for finding the first element in a slice that satisfies a given predicate.

It takes a slice and a predicate function as arguments. The predicate function should return true if the element satisfies the condition, and false otherwise.

It returns the element, its index, and a boolean indicating whether the element was found. If the element is not found, it returns the zero value of type T, -1, and false.

For example, you can use it to find the first even number in a slice of integers:

ints := []int{1, 2, 3, 4, 5}
even, index, found := Find(ints, func(i int) bool { return i%2 == 0 })
// even = 2, index = 1, found = true

func GroupBy added in v0.0.4

func GroupBy[T any, K comparable](arr []T, fn func(T) K) map[K][]T

GroupBy is a utility function for grouping elements of a slice by a key function.

It takes a slice and a key function as arguments and returns a map where the keys are the result of applying the key function to the elements of the slice, and the values are slices of elements that have the same key.

For example, you can use it to group a slice of integers by their parity:

ints := []int{1, 2, 3, 4, 5}
grouped := GroupBy(ints, func(i int) string {
	if i%2 == 0 {
		return "even"
	}
	return "odd"
})
// grouped = map[string][]int{"even": [2, 4], "odd": [1, 3, 5]}

func Reduce added in v0.0.4

func Reduce[T any, R any](arr []T, fn func(R, T) R, initial R) R

Reduce is a utility function for reducing a slice to a single value.

It takes a slice, a reducer function, and an initial value as arguments. The reducer function should take two arguments of the same type as the elements of the slice and return a single value of the same type.

It returns the final reduced value.

func Transform added in v0.0.4

func Transform[T, U any](arr []T, fn func(T) U) []U

Transform is a utility function for transforming the elements of a slice.

For example, you can use it to double the values of a slice of integers:

ints := []int{1, 2, 3}
doubled := Transform(ints, func(i int) int { return i * 2 })
// doubled = [2, 4, 6]

func Unique added in v0.0.4

func Unique[T comparable](arr []T) []T

Unique is a utility function for removing duplicate elements from a slice.

It takes a slice as an argument and returns a new slice containing only the unique elements in the original slice.

The elements in the slice must be comparable.

For example, you can use it to remove duplicate integers from a slice:

ints := []int{1, 2, 2, 3, 3, 3}
uniqueInts := Unique(ints)
// uniqueInts = [1, 2, 3]

Types

This section is empty.

Directories

Path Synopsis
Package protobufutils provides utility functions for working with Google protobuf types.
Package protobufutils provides utility functions for working with Google protobuf types.
Package strings provides utilities for converting strings between common naming conventions used in programming.
Package strings provides utilities for converting strings between common naming conventions used in programming.

Jump to

Keyboard shortcuts

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