lo

package module
v0.0.0-...-7ae329a Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2025 License: MIT Imports: 1 Imported by: 0

README

lo

Go Reference

A Go utility library providing functional programming helpers for slices, maps, and other data structures. This library is based on and inspired by samber/lo, offering type-safe operations using Go generics.

Installation

go get github.com/basemachina/lo

Usage

package main

import (
    "fmt"

    "github.com/basemachina/lo"
)

func main() {
    // Map operation
    numbers := []int{1, 2, 3, 4, 5}
    doubled := lo.Map(numbers, func(x int) int {
        return x * 2
    })
    fmt.Println(doubled) // [2, 4, 6, 8, 10]

    // Filter operation
    evens := lo.Filter(numbers, func(x int) bool {
        return x%2 == 0
    })
    fmt.Println(evens) // [2, 4]

    // Check for duplicates
    hasDuplicates := lo.HasDuplicates([]int{1, 2, 2, 3})
    fmt.Println(hasDuplicates) // true

    // Invert map
    original := map[string]int{"a": 1, "b": 2}
    inverted := lo.Invert(original)
    fmt.Println(inverted) // map[1:a 2:b]
}

Requirements

  • Go 1.24.0 or later

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Every

func Every[T comparable](collection []T, subset []T) bool

Every returns true if all elements of a subset are contained into a collection or if the subset is empty.

func Filter

func Filter[V any](collection []V, predicate func(item V) bool) []V

Filter iterates over elements of collection, returning an array of all elements predicate returns truthy for.

func FilterWithIndex

func FilterWithIndex[V any](collection []V, predicate func(item V, index int) bool) []V

FilterWithIndex iterates over elements of collection, returning an array of all elements predicate returns truthy for.

func Find

func Find[T any](collection []T, predicate func(item T) bool) (T, bool)

Find search an element in a slice based on a predicate. It returns element and true if element was found.

func FlatMap

func FlatMap[T any, R any](collection []T, iteratee func(item T) []R) []R

FlatMap manipulates a slice and transforms and flattens it to a slice of another type. The transform function can either return a slice or a `nil`, and in the `nil` case no value is added to the final slice.

func FlatMapWithIndex

func FlatMapWithIndex[T any, R any](collection []T, iteratee func(item T, index int) []R) []R

FlatMapWithIndex manipulates a slice and transforms and flattens it to a slice of another type. The transform function can either return a slice or a `nil`, and in the `nil` case no value is added to the final slice.

func FromPtrOr

func FromPtrOr[T any](x *T, fallback T) T

FromPtrOr returns the pointer value or the fallback value.

func HasDuplicates

func HasDuplicates[T comparable](collection []T) bool

HasDuplicates checks whether the given slice contains duplicate values and returns a boolean result.

func HasDuplicatesBy

func HasDuplicatesBy[T any, U comparable](collection []T, iteratee func(item T) U) bool

HasDuplicatesBy determines whether the provided slice contains duplicate values by examining the return value from the passed function and returns a boolean result.

func Intersect

func Intersect[T comparable, Slice ~[]T](list1 Slice, list2 Slice) Slice

Intersect returns the intersection between two collections.

func Invert

func Invert[K comparable, V comparable](in map[K]V) map[V]K

Invert creates a map composed of the inverted keys and values. If map contains duplicate values, subsequent values overwrite property assignments of previous values. Play: https://go.dev/play/p/rFQ4rak6iA1

func Map

func Map[T any, R any](collection []T, iteratee func(item T) R) []R

Map manipulates a slice and transforms it to a slice of another type.

func MapWithError

func MapWithError[T any, R any](collection []T, iteratee func(item T) (R, error)) ([]R, error)

MapWithError manipulates a slice and transforms it to a slice of another type. If iteratee returns an error the function shortcircuits and the error is returned alongside nil.

func MapWithIndex

func MapWithIndex[T any, R any](collection []T, iteratee func(item T, index int) R) []R

MapWithIndex manipulates a slice and transforms it to a slice of another type.

func MapWithIndexError

func MapWithIndexError[T any, R any](collection []T, iteratee func(item T, index int) (R, error)) ([]R, error)

MapWithIndexError manipulates a slice and transforms it to a slice of another type. If iteratee returns an error the function shortcircuits and the error is returned alongside nil.

func Reduce

func Reduce[T any, R any](collection []T, accumulator func(agg R, item T) R, initial R) R

Reduce reduces collection to a value which is the accumulated result of running each element in collection through accumulator, where each successive invocation is supplied the return value of the previous.

func ReduceWithIndex

func ReduceWithIndex[T any, R any](collection []T, accumulator func(agg R, item T, index int) R, initial R) R

ReduceWithIndex reduces collection to a value which is the accumulated result of running each element in collection through accumulator, where each successive invocation is supplied the return value of the previous. The accumulator function receives the index of each element.

func Some

func Some[T comparable](collection []T, subset []T) bool

Some returns true if at least 1 element of a subset is contained into a collection. If the subset is empty Some returns false.

func ToPtr

func ToPtr[T any](x T) *T

ToPtr returns a pointer copy of value.

Types

This section is empty.

Jump to

Keyboard shortcuts

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