gog

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2023 License: MIT Imports: 0 Imported by: 9

README

GoG (Go Generics)

GoG is a Go library with useful generic functions and types.

Documentation

Overview

Package gog provides utilities for working with Go generics.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dedupe added in v0.2.0

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

Dedupe returns a new slice that contains only distinct elements from the original slice.

func FindFunc added in v0.7.0

func FindFunc[T any](slice []T, fn func(e T) bool) (T, bool)

FindFunc iterates over the slice and uses the provided closure function to check whether the elements match a user-provided condition. The first value that matches is returned as well as a true flag. Otherwise a false flag is returned.

func FindFuncPtr added in v0.7.0

func FindFuncPtr[T any](slice []T, fn func(e T) bool) *T

FindFuncPtr iterates over the slice and uses the provided closure function to check whether the elements match a user-provided condition. A pointer to the first element that passes the condition is returned. If no value is applicable, then nil is returned.

func Flatten added in v0.3.0

func Flatten[T any](slice [][]T) []T

Flatten returns a new slice that is the result of merging all nested slices into a single top-level slice.

func Map

func Map[S, T any](slice []S, fn func(S) T) []T

Map can be used to transform one slice into another by providing a function to do the mapping.

func Mapping added in v0.3.0

func Mapping[S any, K comparable, V any](slice []S, fn func(S) (K, V)) map[K][]V

Mapping is similar to Partition, except that it allows one to transform the values stored in the partition buckets. In essence, it allows the caller to construct an almost arbitrary map (it is always of kind map[key][]value, though the types of the keys and the values are user controlled) from an arbitrary slice.

func Mutate added in v0.7.0

func Mutate[T any](slice []T, fn func(e *T))

Mutate iterates over the slice and calls the provided closure function, passing a pointer to each element. This allows the user to easily modify all the elements of a slice.

func Partition

func Partition[S any, K comparable](slice []S, fn func(S) K) map[K][]S

Partition splits the specified slice into groups, each having a key according to the specified function.

func PtrOf

func PtrOf[T any](v T) *T

PtrOf returns a pointer to the passed value.

func Reduce

func Reduce[S, T any](slice []S, initialValue T, fn func(accum T, value S) T) T

Reduce compacts a slice into a single value. The provided function is used to perform the reduction starting with the initialValue.

func Select

func Select[S any](slice []S, fn func(S) bool) []S

Select returns a new slice that contains only the elements of the original slice that pass the filter function.

func ValueOf

func ValueOf[T any](v *T, defaultValue T) T

ValueOf is the opposite of PtrOf. It takes a pointer an dereferences it. If the pointer is nil, then it uses the provided defaultValue.

Types

type KV added in v0.3.0

type KV[K comparable, V any] struct {
	// Key holds the key of this key-value pair.
	Key K

	// Value holds the value of this key-value pair.
	Value V
}

KV represents a key-value pair.

func Entries added in v0.3.0

func Entries[K comparable, V any](m map[K]V) []KV[K, V]

Entries returns a slice of key-value pair mappings that make up the specified map.

Note: There is no guarantee as to the order of the returned entries, nor is that order guaranteed to be consistent across two calls.

Directories

Path Synopsis
Package ds provides various data structure implementations with generics.
Package ds provides various data structure implementations with generics.
Package filter adds helper functions for performing filtering over a data set.
Package filter adds helper functions for performing filtering over a data set.
Package opt provides utilities for optional types.
Package opt provides utilities for optional types.

Jump to

Keyboard shortcuts

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