Go 1.18 Generics based slice package

Related tags

Miscellaneous slice
Overview

The missing slice package

A Go-generics (Go 1.18) based functional library with no side-effects that adds the following functions to a slice package:

  • Unique(): removes duplicate items from a slice
  • SortedUnique(): removes duplicate items from a sorted slice. Fast!
  • Sort(): Sorts a slice
  • SortBy(): Sorts a slice by arbitrary criteria
  • Compare(): Iterates two slices comparing them together
  • Subtract(): subtract one slice from another
  • Map(): map over a slice and transform it
  • Reduce(): reduce a slice to a single value
  • Index(): return the index of an element
  • SortedIndex(): return the index of an element in a sorted list. Fast!
  • First(): return the first element
  • Last(): return the last element
  • Select(): select all elements matching some specified criteria
  • Contains(): returns true if the slice contains an element
  • SortedContains(): returns true if the sorted slice contains an element. Fast!
  • Pop(): pop the last element off the list
  • Shift(): shift the first element off the list
  • Unshift(): shift an element into the first place (prepend)
  • Find(): find the first element in a slice that matches some criteria

Method signatures:

(where T is almost any type)

  • slice.Unique([]T) []T
  • slice.SortedUnique([]T) []T
  • slice.Sort([]T) []T
  • slice.SortBy([]T, sortFunc func(slice []T, i, j int) bool) []T
  • slice.Compare(s1, s2 []T, left, equal, right func(elem T))
  • slice.Subtract(s1, s2 []T) []T
  • slice.Map([]T, func(i int, elem T) T) []T
  • slice.Reduce(items []T, initialAccumulator T, f AccumulatorFunc[T]) T
  • slice.Index([]T, elem T) int
  • slice.SortedIndex([]T, elem T) int
  • slice.First([]T) (T, bool)
  • slice.Last([]T) (T, bool)
  • slice.Select([]T, func(i int, elem T) T) []T
  • slice.Contains([]T, elem T) bool
  • slice.SortedContains([]T, elem T) bool
  • slice.Pop([]T) (T, []T)
  • slice.Shift([]T) (T, []T)
  • slice.Unshift([]T, elem T) []T
  • slice.Find([]T, func(i int, elem T) T) (elem T, found bool)

Note the AccumulatorFunc signature is func(acc T, i int, elem T) T

Examples

See tests for more examples. Here are a few:

  // sum function implemented with Reduce
  input := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	result := slice.Reduce(input, 0, func(acc int, i int, elem int) string {
		return acc + elem
	})
  // result == 55
You might also like...
A simple debugging Go package to perform Dump and Die

dump A simple Go package to perform Dump and Die.

Go package for dealing with Mantis Bug Tracking tool

BlueMantis is a Go package in development that aim to make the process of sending issues and bugs in Go applications to the Open Source Bug Tracking software MantisBT.

The package manager for macOS you didn’t know you missed. Simple, functional, and fast.
The package manager for macOS you didn’t know you missed. Simple, functional, and fast.

Stew The package manager for macOS you didn’t know you missed. Built with simplicity, functionality, and most importantly, speed in mind. Installation

sentry integrated logrus package for our internal projects

sentry integrated logrus package for our internal projects

Package fsm allows you to add finite-state machines to your Go code.

fsm Package fsm allows you to add finite-state machines to your Go code. States and Events are defined as int consts: const ( StateFoo fsm.State =

Go package providing tools for working with Library of Congress data.

go-libraryofcongress Go package providing tools for working with Library of Congress data. Documentation Tools $ make cli go build -mod vendor -o bin

Go package for working with Library of Congress data in an SFO Museum context.

go-sfomuseum-libraryofcongress Go package for working with Library of Congress data in an SFO Museum context. Documentation Documentation is incomplet

A tool to generate Pulumi Package schemas from Go type definitions

MkSchema A tool to generate Pulumi Package schemas from Go type definitions. This tool translates annotated Go files into Pulumi component schema meta

Provides the radix package that implements a radix tree.

go-radix Provides the radix package that implements a radix tree. The package only provides a single Tree implementation, optimized for sparse nodes.

Comments
  • Added possibility for mapping one type to another

    Added possibility for mapping one type to another

    Proposal

    Changed signature of Map to func Map[T any, R any](s []T, func (item T) R ) []R Now we have possibility to map one type to another BUT Current golang version cannot infer R

    // WRONG:
    // cannot infer R compilerErrorCode(138)
    require.Equal(t, []int{4}, slice.Map([]string{"fish"}, func(s string) int { return len(s) }))
    // CORRECT
    require.Equal(t, []int{4}, slice.Map[string, int]([]string{"fish"}, func(s string) int { return len(s) }))
    
    opened by jh9aea 1
  • feat: change `slice.Reduce` return type

    feat: change `slice.Reduce` return type

    Hello,

    I would like to see slice.Reduce returns a different type than the inner slice type. Example :

    type OddEven struct {
    	Odd  int
    	Even int
    }
    
    s := []int{0, 1, 2, 3, 4}
    
    result := slice.Reduce(s2, OddEven{}, func(acc OddEven, i int, s int) OddEven {
    	if s%2 == 0 {
    		acc.Even++
    	} else {
    		acc.Odd++
    	}
    	return acc
    })
    

    Thanks,

    opened by scorsi 1
Owner
Steven Soroka
Steven Soroka
Elegant generics for Go

genny - Generics for Go Install: go get github.com/cheekybits/genny ===== (pron. Jenny) by Mat Ryer (@matryer) and Tyler Bunnell (@TylerJBunnell). Un

null 1.7k Dec 23, 2022
go language generics system

Gotgo This document describes the third iteration of my attempt at a reasonable implementation of generics for go based on the idea of template packag

David Roundy 118 Dec 31, 2022
Aboriginal Generics: the future is here!

Aboriginal Generics: the future is here! Inspired by this gem of an idea (click the image to go to the original comment): Installation go get github.c

Pavel Vasilev 151 Oct 3, 2022
An implementation of standard generics APIs in Go.

generics This package shows an implementation outlook of proposed generics APIs import "changkun.de/x/generics" Related issues: golang/go#45458 golang

Changkun Ou 25 Dec 5, 2022
Advent of Code 2021 solutions using Go 1.18 Generics

advent-of-code-2021 Here are my solutions for Advent of Code 2021. This year, I chose to write my solutions using Go 1.18 with generics (by building t

Glenn Lewis 5 Dec 18, 2022
Go-generic - A collection of experiments using Go Generics coming out in Go 1.18

Go Generic - experiments with Go 1.18 beta Data structures: iter.Iter[T any] - l

Matthew Hall 2 Aug 15, 2022
Generic-list-go - Go container/list but with generics

generic-list-go Go container/list but with generics. The code is based on contai

Arne Bahlo 7 Dec 7, 2022
The kprobe package allows construction of dynamic struct based on kprobe event format descriptions.

The kprobe package allows construction of dynamic struct based on kprobe event format descriptions.

Dan Kortschak 4 Oct 27, 2021
:sunglasses:Package captcha provides an easy to use, unopinionated API for captcha generation

Package captcha provides an easy to use, unopinionated API for captcha generation. Why another captcha generator? I want a simple and framework-indepe

Weilin Shi 124 Dec 28, 2022
A Go package that reports processor topology

Description ------------ cpu package reports (some) processor topology information Note that the term package refers to a physical processor

Joseph Poirier 21 Nov 5, 2022