Generic slices for Go 1.8+

Slice

A simple package that makes working with slices a little bit easier with the help of generics.

Install

go get github.com/twharmon/slice

Example

package main

import (
	"fmt"
	
	"github.com/twharmon/slice"
)

func main() {
    s := slice.New("foo", "ba")
    s.Push("b")
    s.Sort(func(a, b string) bool {
        // sort by string length, ascending
        return len(a) < len(b)
    })
    fmt.Println(s) // [b ba foo]

    i := slice.New(-3, 4, 2, -8)
    i.Sort(func(a, b int) bool {
        // sort by asbolute value, ascending
        return a*a < b*b
    })
    fmt.Println(i) // [2, -3, 4, -8]
}

Benchmarks

BenchmarkStdLibShortSort-10    	 1831299	       653.5 ns/op	     112 B/op	       4 allocs/op
BenchmarkSortShort-10       	 1634320	       732.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkStdLibSortLong-10     	  366229	      3261 ns/op	     112 B/op	       4 allocs/op
BenchmarkSortLong-10        	  292084	      4097 ns/op	       0 B/op	       0 allocs/op

Contribute

Make a pull request.

Owner
Similar Resources

Generic slices for Go 1.8+

Slice A simple package that makes working with slices a little bit easier with the help of generics. Install go get github.com/twharmon/slice Example

Nov 1, 2022

Generic utility methods for Go slices / arrays / collections, heavily inspired by Lodash.

slicy import "github.com/sudhirj/slicy" Usage func All func All[S ~[]T, T any](slice S, predicate func(value T, index int, slice S) bool) bool All re

Aug 30, 2022

Provider-generic-workflows - A generic provider which uses argo workflows to define the backend actions.

provider-generic-workflows provider-generic-workflows is a generic provider which uses argo workflows for managing the external resource. This will re

Jan 1, 2022

Go-generic-unboxing - A quick ready to ship demo for go generic using the official example

Go generic This repo contain basic demo for installing and running go1.18beta1 v

Feb 1, 2022

Generic - Golang generic example

泛型 场景 假设需要写一个列表总数计算的函数,根据不同数据类型,我们可能分别要写 SumInts(data []int),SumFloats(data []fl

Jan 27, 2022

This library implements the pub/sub pattern in a generic way. It uses Go's generic types to declare the type of the event.

observer This library implements the pub/sub pattern in a generic way. It uses Go's generic types to declare the type of the event. Usage go get githu

Nov 16, 2022

[Go] Package of validators and sanitizers for strings, numerics, slices and structs

govalidator A package of validators and sanitizers for strings, structs and collections. Based on validator.js. Installation Make sure that Go is inst

Jan 6, 2023

[Go] Package of validators and sanitizers for strings, numerics, slices and structs

govalidator A package of validators and sanitizers for strings, structs and collections. Based on validator.js. Installation Make sure that Go is inst

Dec 28, 2022

Helper functions for the manipulation of slices of all types in Go

go-slices Unlike many other programming languages, Go doesn't provide helper functions for slices in it's core. I felt like this was quite an essentia

Jan 3, 2023

Go package for dealing with maps, slices, JSON and other data.

Objx Objx - Go package for dealing with maps, slices, JSON and other data. Get started: Install Objx with one line of code, or update it with another

Dec 27, 2022

Slice graph uses graphviz in order to pretty print slices for you.

slicegraph Slice graph uses graphviz in order to make you understand what happens underneath your slices.

Sep 22, 2022

Scan database/sql rows directly to structs, slices, and primitive types

Scan Scan standard lib database rows directly to structs or slices. For the most comprehensive and up-to-date docs see the godoc Examples Multiple Row

Dec 28, 2022

Bitwise AND on two byte-slices using SIMD instructions

This package provides a vectorised function which performs bitwise AND operation on all pairs of elements in two byte-slices. It detects CPU instruction set and chooses the available best one (AVX512, AVX2, SSE2).

Oct 17, 2022

A Go package for checking conditions for slices and maps.

check Go package The check package of Go helps one to check various conditions for slices: []int []float64 []string []bool maps: map[string]int map[st

Aug 26, 2022

SliceX provides functional operations on Go slices using Go 1.18 type parameters.

SliceX provides functional operations on Go slices using Go 1.18 type parameters.

Nov 6, 2021

Create deep copies (clones) of your maps and slices without using reflection.

DeepCopy DeepCopy helps you create deep copies (clones) of your maps and slices. Create deep copies (clones) of your objects The package is based on t

Nov 20, 2022

Helper functions for the manipulation of slices of all types in Go

go-slices Unlike many other programming languages, Go doesn't provide helper functions for slices in it's core. I felt like this was quite an essentia

Jan 3, 2023

A go1.18+ package to (maybe) simplify performing operations on slices in a fluent-like style.

sop ✨ W.I.P. ✨ sop (slices operation) is a go1.18+ package to (maybe) simplify performing operations on slices in a fluent-like style with common oper

Oct 1, 2022

Golang: unify nil and empty slices and maps

unifynil, unify nil and empty slices and maps in Golang Empty slices and maps can be nil or not nil in Go. It may become a nightmare in tests and JSON

Jan 16, 2022
Comments
  • Closes #13 performance improvements, add more benchmarks, add intersectHash method

    Closes #13 performance improvements, add more benchmarks, add intersectHash method

    Hi!
    I've added more benchmarks and improved performance for some methods.
    Benchmark results without changes were:

    goos: darwin
    goarch: arm64
    pkg: github.com/twharmon/slices
    BenchmarkStdLibSortFunc-8   	   98938	     11663 ns/op	    8232 B/op	      11 allocs/op
    BenchmarkSortFuncShort-8    	   73940	     13807 ns/op	   16352 B/op	      18 allocs/op
    BenchmarkStdLibSort-8       	  118772	      9962 ns/op	    8200 B/op	      10 allocs/op
    BenchmarkSortShort-8        	  107100	     10912 ns/op	   16352 B/op	      18 allocs/op
    BenchmarkReverse/strings_237_pure-8         	 1987234	       594.0 ns/op	    4096 B/op	       1 allocs/op
    BenchmarkReverse/strings_237-8              	    5991	    187634 ns/op	 1238684 B/op	    1878 allocs/op
    BenchmarkReverse/strings_7584_pure-8        	   77500	     15337 ns/op	  122880 B/op	       1 allocs/op
    BenchmarkReverse/strings_7584-8             	       5	 229619742 ns/op	1780660038 B/op	  113723 allocs/op
    BenchmarkReverse/int64_70_pure-8            	14534846	        79.49 ns/op	     576 B/op	       1 allocs/op
    BenchmarkReverse/int64_70-8                 	  102580	     11547 ns/op	   55424 B/op	     433 allocs/op
    BenchmarkConcat/strings_6x237_pure-8        	  496095	      2189 ns/op	   24576 B/op	       1 allocs/op
    BenchmarkConcat/strings_6x237-8             	   47464	     25390 ns/op	  230323 B/op	      59 allocs/op
    BenchmarkConcat/int64_6x70_pure-8           	 4193443	       284.0 ns/op	    3456 B/op	       1 allocs/op
    BenchmarkConcat/int64_6x70-8                	  317482	      3681 ns/op	   33304 B/op	      49 allocs/op
    BenchmarkFilter/strings_237_(len>5)_pure-8  	 1372065	       884.6 ns/op	    4096 B/op	       1 allocs/op
    BenchmarkFilter/strings_237_(len>5)-8       	   24979	     47321 ns/op	  261826 B/op	     722 allocs/op
    BenchmarkFilter/int64_70_(val>40)_pure-8    	 6286593	       190.3 ns/op	     576 B/op	       1 allocs/op
    BenchmarkFilter/int64_70_(val>40)-8         	  219350	      5487 ns/op	   19376 B/op	     218 allocs/op
    BenchmarkMap/strings_237_replace_a_b_pure-8 	  223681	      5273 ns/op	    4768 B/op	      99 allocs/op
    BenchmarkMap/strings_237_replace_a_b-8      	    5971	    195865 ns/op	 1239359 B/op	    1976 allocs/op
    BenchmarkMap/int64_237_*5_pure-8            	 9245161	       128.5 ns/op	     576 B/op	       1 allocs/op
    BenchmarkMap/int64_237_*5-8                 	  101328	     11642 ns/op	   55424 B/op	     433 allocs/op
    BenchmarkIntersect/int64_70-46-47_pure-8    	  228531	      5262 ns/op	     576 B/op	       1 allocs/op
    BenchmarkIntersect/int64_70-46-47-8         	  157063	      7575 ns/op	    6136 B/op	     107 allocs/op
    
  • performance issues

    performance issues

    Hi! I've added more benchmarks and got these results on my air M1: There are some information about benchmark in it's name: method/<type>_<slice_length> etc.

    BenchmarkReverse/strings_174_pure-8         	 3096415	       387.3 ns/op	    3072 B/op	       1 allocs/op
    BenchmarkReverse/strings_174-8              	   10000	    112034 ns/op	  723591 B/op	    1311 allocs/op
    BenchmarkReverse/strings_5568_pure-8        	   94143	     12591 ns/op	   90112 B/op	       1 allocs/op
    BenchmarkReverse/strings_5568-8             	      10	 107924375 ns/op	913949232 B/op	   78472 allocs/op
    BenchmarkReverse/int64_94_pure-8            	11687761	       102.4 ns/op	     768 B/op	       1 allocs/op
    BenchmarkReverse/int64_94-8                 	   64416	     18711 ns/op	  104384 B/op	     625 allocs/op
    BenchmarkConcat/strings_6x174_pure-8        	  755361	      1504 ns/op	   18432 B/op	       1 allocs/op
    BenchmarkConcat/strings_6x174-8             	   62305	     19247 ns/op	  180147 B/op	      57 allocs/op
    BenchmarkConcat/int64_6x174_pure-8          	 3307166	       361.6 ns/op	    4864 B/op	       1 allocs/op
    BenchmarkConcat/int64_6x174-8               	  259530	      4438 ns/op	   44376 B/op	      51 allocs/op
    BenchmarkFilter/strings_174_(len>5)_pure-8  	 1963303	       608.7 ns/op	    3072 B/op	       1 allocs/op
    BenchmarkFilter/strings_174_(len>5)-8       	   37066	     32502 ns/op	  166961 B/op	     538 allocs/op
    BenchmarkFilter/int64_94_(val>40)_pure-8    	 4716992	       253.2 ns/op	     768 B/op	       1 allocs/op
    BenchmarkFilter/int64_94_(val>40)-8         	  135060	      8387 ns/op	   34808 B/op	     323 allocs/op
    BenchmarkMap/strings_174_replace_a_b_pure-8 	  327822	      3592 ns/op	    3536 B/op	      69 allocs/op
    BenchmarkMap/strings_174_replace_a_b-8      	    8994	    116112 ns/op	  724056 B/op	    1379 allocs/op
    BenchmarkMap/int64_174_*5_pure-8            	 7208736	       166.9 ns/op	     768 B/op	       1 allocs/op
    BenchmarkMap/int64_174_*5-8                 	   63901	     18898 ns/op	  104384 B/op	     625 allocs/op
    

    as you can see there are some performance issues. I'm going to create PR to fix them as soon as it ready :)

Related tags
Generic - Golang generic example

泛型 场景 假设需要写一个列表总数计算的函数,根据不同数据类型,我们可能分别要写 SumInts(data []int),SumFloats(data []fl

Jan 27, 2022
a generic object pool for golang

Go Commons Pool The Go Commons Pool is a generic object pool for Golang, direct rewrite from Apache Commons Pool. Features Support custom PooledObject

Jan 5, 2023
Optimistic rollup tech, minimal and generic.

Opti Optimistic rollup tech, minimal and generic. VERY experimental, just exploratory code, question is: 1:1 EVM rollup with interactive fraud proof p

Aug 30, 2022
Generic mapStringInterface tool for extracting of data for CSV output

Generic mapStringInterface tool for extracting of data for CSV output

Nov 2, 2021
Generic tools for go 1.18+

Gtools Generic tools for go 1.18+ FT (func tools) Provide func tools over iterators Iterators for functions like Filter, Map, Reduce, etc solve 3 main

Jan 12, 2022
Ecsgo - Cache friendly, Multi threading Entity Component System in Go (with Generic)

ECSGo ECSGo is an Entity Component System(ECS) in Go. This is made with Generic

Oct 19, 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

Aug 15, 2022
Nune - High-performance numerical engine based on generic tensors

Nune (v0.1) Numerical engine is a library for performing numerical computation i

Nov 9, 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

Dec 7, 2022
Nune-go - High-performance numerical engine based on generic tensors

Nune (v0.1) Numerical engine is a library for performing numerical computation i

Nov 9, 2022