Coder Social home page Coder Social logo

generics's Introduction

Generics

This is a repo for some helper methods/structs that involve generics (added in Go 1.18).

slices package

This package contains all the functions in the official slices package but adds extra functions as well, resulting in a superset of the official API. Any official functions are just forwarded to the official implementations. This allows you to use this package wherever you would otherwise use the official slices package.

As the official slices package evolves, so too will this package. If a function is added to the official package that does basically the same thing as a function from this package, we'll replace our function for the official function.

Here are the newly added functions.

func Some[T any](slice []T, test func(T) bool) bool
func Every[T any](slice []T, test func(T) bool) bool
func ForEach[T any](slice []T, f func(T))
func ForEachWithIndex[T any](slice []T, f func(T, int))
func TryForEach[T any](slice []T, f func(T) error) error
func TryForEachWithIndex[T any](slice []T, f func(T, int) error) error
func Map[T any, V any](slice []T, f func(T) V) []V
func MapWithIndex[T any, V any](slice []T, f func(T, int) V) []V
func TryMap[T any, V any](slice []T, f func(T) (V, error)) ([]V, error)
func TryMapWithIndex[T any, V any](slice []T, f func(T, int) (V, error)) ([]V, error)
func MapInPlace[T any](slice []T, f func(T) T)
func Filter[T any](slice []T, test func(T) bool) []T
func FilterWithIndex[T any](slice []T, f func(T, int) bool) []T
func TryFilter[T any](slice []T, test func(T) (bool, error)) ([]T, error)
func TryFilterWithIndex[T any](slice []T, test func(T, int) (bool, error)) ([]T, error)
func FilterInPlace[T any](slice []T, test func(T) bool) []T
func FilterMap[T any, E any](slice []T, test func(T) (E, bool)) []E
func TryFilterMap[T any, E any](slice []T, test func(T) (E, bool, error)) ([]E, error)
func TryFilterMapWithIndex[T any, E any](slice []T, test func(T, int) (E, bool, error))
func FlatMap[T any, V any](slice []T, f func(T) []V) []V
func Flatten[T any](slice [][]T) []T
func Find[T any](slice []T, f func(T) bool) (T, bool)
func FindMap[T any, V any](slice []T, f func(T) (V, bool)) (V, bool)
func Reverse[T any](slice []T) []T
func ReverseInPlace[T any](slice []T)
func Prepend[T any](slice []T, values ...T) []T
func Remove[T any](slice []T, index int) []T
func Move[T any](slice []T, fromIndex int, toIndex int) []T
func Swap[T any](slice []T, index1 int, index2 int)
func Concat[T any](slice []T, values ...T) []T
func ContainsFunc[T any](slice []T, f func(T) bool) bool
func Pop[T any](slice []T) (T, []T)
func Shift[T any](slice []T) (T, []T)
func Partition[T any](slice []T, test func(T) bool) ([]T, []T)
func MaxBy[T any, V constraints.Ordered](slice []T, f func(T) V) V
func MinBy[T any, V constraints.Ordered](slice []T, f func(T) V) V

There's a good chance I'll have the Map/Filter functions take an index argument unconditionally and leave it to the user to omit that if they want. That will cut down on the number of functions here, but add some boilerplate. I'm currently comparing both approaches on a sizable repo to help decide.

list package

This package provides a List struct which wraps a slice and gives you access to all the above functions, with a couple exceptions. For example, there's no Map method because go does not support type parameters on struct methods.

set package

This package provides a Set struct with the following methods:

Add(values ...T)
AddSlice(slice []T)
Remove(value T)
RemoveSlice(slice []T)
Includes(value T) bool
ToSlice() []T

maps package

Provides some helper methods for maps:

func Keys[Key comparable, Value any](m map[Key]Value) []Key
func Values[Key comparable, Value any](m map[Key]Value) []Value
func TransformValues[Key comparable, Value any, NewValue any
func TransformKeys[Key comparable, Value any, NewKey comparable](m map[Key]Value, fn func(Key) NewKey) map[NewKey]Value
func MapToSlice[Key comparable, Value any, Mapped any](m map[Key]Value, f func(Key, Value) Mapped) []Mapped
func Filter[Key comparable, Value any](m map[Key]Value, f func(Key, Value) bool) map[Key]Value

Alternatives

Check out https://github.com/samber/lo for some more generic helper functions

generics's People

Contributors

jesseduffield avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.