slicesx

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 19, 2026 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package slicesx provides generic utility functions for working with slices.

This package offers functional programming utilities for slice operations using Go generics, avoiding the need to write repetitive boilerplate code.

Map Operations

Transform each element of a slice:

numbers := []int{1, 2, 3, 4, 5}
doubled := slicesx.Map(numbers, func(n int) int { return n * 2 })
// Result: []int{2, 4, 6, 8, 10}

Convert between types:

ages := []int{25, 30, 35}
strings := slicesx.Map(ages, func(age int) string {
    return fmt.Sprintf("%d years", age)
})
// Result: []string{"25 years", "30 years", "35 years"}

Extract fields from structs:

users := []User{{ID: "1", Name: "Alice"}, {ID: "2", Name: "Bob"}}
ids := slicesx.Map(users, func(u User) string { return u.ID })
// Result: []string{"1", "2"}

Find Operations

Find the first element matching a condition:

numbers := []int{1, 2, 3, 4, 5}
found, ok := slicesx.Find(numbers, func(n int) bool { return n > 3 })
// found = 4, ok = true

notFound, ok := slicesx.Find(numbers, func(n int) bool { return n > 10 })
// notFound = 0 (zero value), ok = false

Find in struct slices:

users := []User{{Name: "Alice"}, {Name: "Bob"}}
user, found := slicesx.Find(users, func(u User) bool {
    return u.Name == "Alice"
})

Common Use Cases

Proto conversion:

protoUsers := slicesx.Map(users, UserToProto)

ID extraction:

userIDs := slicesx.Map(users, func(u User) string { return u.ID })

Filtering by conversion:

activeUser, found := slicesx.Find(users, func(u User) bool {
    return u.Active
})

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Find

func Find[I any](input []I, predicate func(I) bool) (element I, found bool)

Find returns the first element in the slice that satisfies the predicate function.

The function iterates through the slice and returns the first element for which the predicate returns true, along with a boolean indicating whether a match was found. If no element matches, the zero value for the type and false are returned.

Type Parameters:

  • I: Element type

Example:

numbers := []int{1, 2, 3, 4, 5}
result, found := slicesx.Find(numbers, func(n int) bool { return n > 3 })
// result = 4, found = true

result, found := slicesx.Find(numbers, func(n int) bool { return n > 10 })
// result = 0 (zero value), found = false

func Map

func Map[I any, O any](input []I, transform func(I) O) []O

Map transforms each element of the input slice using the provided transform function.

The function creates a new slice with the transformed elements, leaving the original slice unchanged. If the input slice is empty, an empty output slice is returned.

Type Parameters:

  • I: Input element type
  • O: Output element type

Example:

numbers := []int{1, 2, 3}
doubled := slicesx.Map(numbers, func(n int) int { return n * 2 })
// Result: []int{2, 4, 6}

// Convert types
strings := slicesx.Map(numbers, func(n int) string { return fmt.Sprintf("%d", n) })
// Result: []string{"1", "2", "3"}

Types

This section is empty.

Jump to

Keyboard shortcuts

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