Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Apr 19, 2022. It is now read-only.

hamba/slices

Repository files navigation

Logo

Go Report Card Build Status Coverage Status GoDoc GitHub release GitHub license

A fast Go slices codec

Overview

Install with:

go get github.com/hamba/slices

Usage

Contains

Contains is a generic slice contains function.

Supports: bool, string, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64

slice := []string{"foo", "bar"}
v := "bar"

fmt.Println(slices.Contains(slice, v))
// Outputs: true

GreaterOf

GreaterOf returns a greater function for the given slice type for slice sorting.

Supports: bool, string, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64

slice := []string{"foo", "bar"}
sort.Slice(slice, slices.GreaterOf(slice))

fmt.Println(slice)
// Outputs: [foo bar]

LesserOf

LesserOf returns a lesser function for the given slice type for slice sorting.

Supports: bool, string, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64

slice := []string{"foo", "bar"}
sort.Slice(slice, slices.LesserOf(slice))

fmt.Println(slice)
// Outputs: [bar foo]

Benchmark

BenchmarkContains-8          	36659943	        30.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkContainsNative-8    	48539482	        24.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkGreaterOf-8         	 6257299	       193 ns/op	      80 B/op	       3 allocs/op
BenchmarkGreaterOfNative-8   	 7963461	       149 ns/op	      64 B/op	       2 allocs/op
BenchmarkLesserOf-8          	 6122317	       192 ns/op	      80 B/op	       3 allocs/op
BenchmarkLesserOfNative-8    	 7947242	       152 ns/op	      64 B/op	       2 allocs/op

Always benchmark with your own workload. The result depends heavily on the data input.

About

Go slice helper functions

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Contributors 2

  •  
  •