Helper functions for common scenarios, using Go generics.

zeroflucs generics

When writing Go code for Go 1.17 or below, we've all written more than our fair share of methods to check "does this slice contain a thing", or "give me the first item matching a predicate". This package contains a roll-up of helpers and methods, as well as generic collection types that enable a lot of this boilerplate code to be removed.

Key attributes:

  • Context support for filters/mappers (Optional)
  • Does not mutate input during operation.

All code is covered 100% by tests for expected behaviour. Where filters or mappers are used methods are provided with and without context support.

Aggregations


Min

Returns the minimum value from the input slice. Returns 0 if no values.

Max

Returns the maximum value from the input slice. Returns 0 if no values.

Sum

Returns the total sum of the values. Note that when handling large values, you may overflow your input type.

Error Checking


Must

Returns the first value in a pair of (T, error) if there is no error. Otherwise will panic.

Slice Operations


All / AllContext

Returns true if all items in the slice match the filter. Will return true for an empty slice as no items fail the predicate.

Any / AnyContext

Returns true if any item in the slice matches the filter.

Concatenate

Joins N slices of items together in the given order.

Contains

Returns true if the slice contains the specified value T, false otherwise. Uses standard equality operator to compare types.

DefaultIfEmpty

Given a slice, if the slice is empty or nil will create a slice of a single default item.

Filter / FilterContext

Creates a filtered set of the values in the slice, using a filter function.

First

Returns the first item of the slice, or panics if the slice is empty. Use with DefaultIfEmpty to ensure panic-free operation over lists that are potentially empty.

FirstOrDefault

Returns the first item of the slice, or if the slice is empty returns a default value.

Group

Uses a mapper function to assign input values to buckets.

Last

Returns the last item of the slice, or panics if the slice is empty. Use with DefaultIfEmpty to ensure panic-free operation over lists that are potentially empty.

LastOrDefault

Returns the last item of the slice, or a default if the slice is empty.

Map / MapContext

Runs the specified mapper over each element of the input slice, creating an output slice of a different type.

Reverse

Creates a reverse-sorted version of the input slice.

Skip

Skip the first N elements of the slice.

Take

Take the next N elements of the slice.

ToMap / ToMapContext

Converts a slice of values to Go map, using mappers for the key and values respectively.

Owner
ZeroFlucs
Pricing Technology / Zero Flucs Given
ZeroFlucs
Similar Resources

tfu is a Terraform helper to update the providers of Terraform

tfu (speak 'TF-up') tfu is a Terraform helper to update the providers of Terraform Works only starting from version Terraform 0.13+ Nothing more nothi

Apr 26, 2022

💪 Helper Utils For The Go: string, array/slice, map, format, cli, env, filesystem, test and more.

💪 Helper Utils For The Go: string, array/slice, map, format, cli, env, filesystem, test and more.

💪 Helper Utils For The Go: string, array/slice, map, format, cli, env, filesystem, test and more. Go 的一些工具函数,格式化,特殊处理,常用信息获取等等

Jan 6, 2023

Little helper to create tar balls of an executable together with its ELF shared library dependencies.

Little helper to create tar balls of an executable together with its ELF shared library dependencies. This is useful for prototyping with gokrazy: htt

Sep 7, 2022

A helper function to create a pointer to a new object in Go 1.18+

A helper function to create a pointer to a new object in Go 1.18+

A helper function to create a pointer to a new object in Go 1.18+

Nov 9, 2022

Calling functions by name and getting outputs by using reflect package.

Invoker A library to call (invoke) functions by taking names and sample inputs of those functions as parameters. And returns the types and values of o

Dec 20, 2021

Code Generation for Functional Programming, Concurrency and Generics in Golang

goderive goderive derives mundane golang functions that you do not want to maintain and keeps them up to date. It does this by parsing your go code fo

Dec 25, 2022

Experiments with Go generics

generics Quick experiments with Go generics algebra, a generic square root function for float, complex and and rational. future, a concurrent cache ("

Dec 31, 2022

Example code for Go generics

go-generics-example Example code for Go generics. Usage $ go build -gcflags=-G=3 Requirements Go 1.17 or later Advertise Go 言語にやってくる Generics は我々に何をも

Dec 30, 2022

Collection of unusual generics usecases in Go

Unusual Generics Type parameters or Generics in Go designed to reduce boilerplate for container data types like lists, graphs, etc. and functions like

Dec 14, 2022
Extended library functions using generics in Go.

Just few extended standard library functions for Golang using generics.

Dec 16, 2021
Higher Order Functions using Golang Generics (Hack Days 2022)

hoff: Higher Order Functions (and Friends) Golang 1.18+ implementations of common methods/data structures using Go Generics Requirements Go 1.18 or ne

Jan 4, 2023
Leftright - A concurrent map that is optimized for scenarios where reads are more frequent than writes

leftright A concurrent map that is optimized for scenarios where reads are more

Jan 30, 2022
Use Golang to implement PHP's common built-in functions.

PHP2Go Use Golang to implement PHP's common built-in functions. About 140+ functions have been implemented. Install go get github.com/syyongx/php2go R

Dec 28, 2022
The one-stop shop for most common Go functions
The one-stop shop for most common Go functions

Pandati The one stop shop for most common Go functions Table of contents Pandati The one stop shop for most common Go functions Table of contents Purp

Mar 21, 2022
Go-path - A helper package that provides utilities for parsing and using ipfs paths

go-path is a helper package that provides utilities for parsing and using ipfs paths

Jan 18, 2022
Functional tools in Go 1.18 using newly introduced generics

functools functools is a simple Go library that brings you your favourite functi

Dec 5, 2022
Optional type using Go 1.18 generics.

go.eth-p.dev/goptional Generic Optional (or Go Optional, if you prefer) goptional is a package that provides an implementation of an Optional[T] monad

Apr 2, 2022
Use is a go utility library using go1.18 generics

use use is a go utility library using go1.18 generics created by halpdesk 2022-01-22 use/slice Map updates a slice by applying a function to all membe

Jan 22, 2022
Helper library for full uint64 randomness, pool backed for efficient concurrency

fastrand64-go Helper library for full uint64 randomness, pool backed for efficient concurrency Inspired by https://github.com/valyala/fastrand which i

Dec 5, 2021