slices

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 19, 2020 License: MIT Imports: 1 Imported by: 1

README

Logo

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

Go slice helper functions

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: 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: 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]

Intersect

Intersect returns the intersection of 2 slices.

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

slice := []string{"foo", "bar", "baz", "bat"}
other := []string{"bar", "baz", "test"}

fmt.Println(slices.Intersect(slice, other))
// Outputs: [bar baz]

Except

Except returns the all elements in the first slice that are not in the second.

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

slice := []string{"foo", "bar", "baz", "bat"}
other := []string{"bar", "baz", "test"}

fmt.Println(slices.Except(slice, other))
// Outputs: [foo bat]

Benchmark

BenchmarkContains-8          	35621572	        30.5 ns/op	       0 B/op	       0 allocs/op
BenchmarkContainsNative-8    	50106157	        23.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkExcept-8            	 6070610	       200 ns/op	      96 B/op	       2 allocs/op
BenchmarkExceptNative-8      	 5933550	       193 ns/op	      96 B/op	       2 allocs/op
BenchmarkGreaterOf-8         	 6290626	       189 ns/op	      80 B/op	       3 allocs/op
BenchmarkGreaterOfNative-8   	 8201284	       149 ns/op	      64 B/op	       2 allocs/op
BenchmarkIntersect-8         	 6012298	       196 ns/op	      96 B/op	       2 allocs/op
BenchmarkIntersectNative-8   	 6305799	       198 ns/op	      96 B/op	       2 allocs/op
BenchmarkLesserOf-8          	 6449050	       189 ns/op	      80 B/op	       3 allocs/op
BenchmarkLesserOfNative-8    	 8077785	       149 ns/op	      64 B/op	       2 allocs/op

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

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains(slice, val interface{}) bool

Contains determines if the slice contains val.

Example
package main

import (
	"fmt"

	"github.com/hamba/slices"
)

func main() {
	slice := []string{"foo", "bar"}
	v := "bar"

	fmt.Println(slices.Contains(slice, v))
}
Output:

true

func Except

func Except(slice, other interface{}) interface{}

Except returns a slice with the elements of slice that are not in other. When the slices different, slice is returned.

Example
package main

import (
	"fmt"

	"github.com/hamba/slices"
)

func main() {
	slice := []string{"foo", "bar", "baz", "bat"}
	other := []string{"bar", "baz", "test"}

	fmt.Println(slices.Except(slice, other))
	// Outputs: [foo bat]
}
Output:

func GreaterOf

func GreaterOf(slice interface{}) func(i, j int) bool

GreaterOf returns a greater function for slice sorting.

Example
package main

import (
	"fmt"
	"sort"

	"github.com/hamba/slices"
)

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

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

func Intersect

func Intersect(slice, other interface{}) interface{}

Intersection returns a slice with the intersection of slice and other. When the slices are the same, slice is returned.

Example
package main

import (
	"fmt"

	"github.com/hamba/slices"
)

func main() {
	slice := []string{"foo", "bar", "baz", "bat"}
	other := []string{"bar", "baz", "test"}

	fmt.Println(slices.Intersect(slice, other))
	// Outputs: [bar baz]
}
Output:

func LesserOf

func LesserOf(slice interface{}) func(i, j int) bool

LesserOf returns a lesser function for slice sorting.

Example
package main

import (
	"fmt"
	"sort"

	"github.com/hamba/slices"
)

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

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

Types

This section is empty.

Directories

Path Synopsis
internal
gen

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL