collection

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 26, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package collection provides a fluent, chainable API for performing functional-style operations like map, filter, and reduce on slices.

It is inspired by collection helpers from other languages (like Laravel's Collections) and works around Go's current generic limitations using reflection.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToTypedSlice

func ToTypedSlice[T any](c Collection) ([]T, error)

ToTypedSlice casts the result of the Collection to a typed slice.

It is a standalone generic function (not a method) due to Go's generic limitations. The type parameter T specifies the element type.

Example:

strings, err := ToTypedSlice[string](c)

This function will return an error if the underlying data cannot be cast to the requested or if the provided Collection already contains an error.

Types

type Collection

type Collection struct {
	// contains filtered or unexported fields
}

Collection represents a wrapper around a slice, allowing chained operations like Map, Filter, and Reduce. It holds the underlying data and tracks any errors that occur during chained operations.

If any operation in the chain fails, the error is stored in the Collection and subsequent operations are skipped until ToSlice or Reduce is called.

func FromSlice

func FromSlice(s any) Collection

FromSlice creates a new Collection from a given slice.

The input must be a slice type; otherwise, the returned Collection will carry an error. This is the entry point for starting a chain of collection operations.

func (Collection) Filter

func (c Collection) Filter(f any) Collection

Filter applies the provided function to each element of the underlying slice, returning a new Collection containing only the elements for which the function returns true.

The provided function must:

  • Be a function type
  • Take one argument matching the element type of the slice
  • Return exactly one bool value

Example:

c := FromSlice([]int{1, 2, 3, 4}).Filter(func(n int) bool { return n%2 == 0 })

func (Collection) ForEach

func (c Collection) ForEach(f any) Collection

ForEach applies the provided function to each element of the underlying slice, allowing side effects like printing, logging, or collecting external results.

The provided function must:

  • Be a function type
  • Take one argument matching the element type of the slice
  • Return no value

ForEach is intended for actions with side effects, not for transforming data. The Collection returned is the same as the input, allowing further chaining.

Example:

FromSlice([]string{"a", "b", "c"}).ForEach(func(s string) {
    fmt.Println("Value:", s)
})

func (Collection) Map

func (c Collection) Map(f any) Collection

Map applies the provided function to each element of the underlying slice, returning a new Collection with the transformed elements.

The provided function must:

  • Be a function type
  • Take one argument matching the element type of the slice
  • Return exactly one value (the transformed element)

The resulting Collection holds a slice of the new output type.

Example:

c := FromSlice([]int{1, 2, 3}).Map(func(n int) string { return strconv.Itoa(n) })

func (Collection) Reduce

func (c Collection) Reduce(reducer any, initial any) (any, error)

Reduce applies a reducer function over the slice, accumulating a single result.

The reducer function must:

  • Be a function type
  • Take two arguments: (accumulator, element), where the accumulator type matches the type of 'initial'
  • Return exactly one value, which must match the accumulator type

Example:

sum, err := FromSlice([]int{1, 2, 3}).Reduce(func(acc, n int) int { return acc + n }, 0)

func (Collection) ToSlice

func (c Collection) ToSlice() (any, error)

ToSlice returns the underlying slice after all chained operations, along with any accumulated error.

The returned value is of type 'any', which can be type-asserted by the caller.

Example:

result, err := FromSlice([]int{1, 2, 3}).Map(...).ToSlice()

Jump to

Keyboard shortcuts

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