gophers

package module
v0.0.0-...-0f58786 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2023 License: MIT Imports: 2 Imported by: 0

README

Small gophers that help you write Go programs

Functional-style utilities. You can think of this as the missing toplevel builtins, things you can find e.g. in Python.

Requires Go 1.18 (generics).

All functions have short, familiar names, like Assert or Filter, and you are encouraged to dot-import the module for easy access.

Handling errors

package main
import (
    . "github.com/rollcat/gophers"
)

Now instead of writing this:

x, err := GetX()
if err != nil {
    panic(err)
}

You can write this:

x := Must(GetX())

It will crash just the same.

Functional style

For those, who do not believe that Go has a Lisp nature, I present the familiar repertoire of Map, Filter, and Reduce.

There is also a handful of other small utilities such as GroupBy (which classifies elements from a list into a map, according to a function), or SortBy (duh).

These are all made possible because of the introduction of generics.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Assert

func Assert(err error)

func BContains

func BContains[T comparable](xs []T, x T) bool

Check if xs (which must be sorted) contains x, using binary search.

func BSearch

func BSearch[T comparable](xs []T, x T) int

Search for the position of x in xs, using binary search. The input slice xs must be sorted. Return len(xs) if no match is found.

func Compose

func Compose[A, B, C any](f func(A) B, g func(B) C) func(A) C

Create a function that composes f and g into a single function n, such that n(x) = g(f(x)).

func Compose3

func Compose3[A, B, C, D any](f func(A) B, g func(B) C, h func(C) D) func(A) D

Create a function that composes f, g and h into a single function n, such that n(x) = h(g(f(x))).

func Contains

func Contains[T comparable](xs []T, x T) bool

Check if x exists in xs (linear time).

func Filter

func Filter[T any](f func(T) bool, xs []T) []T

Filter elements in xs based on boolean predicate f.

func GroupBy

func GroupBy[T any, K comparable](f func(T) K, xs []T) map[K][]T

Group xs by key function f into a map of arrays. Relative order of xs in map values is preserved.

func Map

func Map[A, B any](f func(A) B, xs []A) []B

Apply f to each element in xs and return an array with the results.

func MapKeys

func MapKeys[K comparable, V any](m map[K]V) (keys []K)

Return the keys in the map.

func MapValues

func MapValues[K comparable, V any](m map[K]V) (values []V)

Return the values in the map.

func Must

func Must[T any](x T, err error) T

func Must2

func Must2[T1, T2 any](x1 T1, x2 T2, err error) (T1, T2)

func Reduce

func Reduce[A, V any](f func(A, V) A, acc A, xs []V) A

Reduce the array of xs into an accumulator value using reducer f.

func SlicePop

func SlicePop[T any](xs []T, idx int) ([]T, T)

Return (newxs, value), with value at idx removed from newxs.

func Sort

func Sort[T constraints.Ordered](xs []T)

Sort xs in place.

func SortBy

func SortBy[T any, K constraints.Ordered](xs []T, f func(T) K)

Sort xs in place, comparing elements using keyfunc f.

func Sorted

func Sorted[T constraints.Ordered](xs []T) []T

Create a sorted copy of xs.

func Uniq

func Uniq[T comparable](xs []T) []T

Return only unique values in xs, disregarding order.

Types

This section is empty.

Jump to

Keyboard shortcuts

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