Git Product home page Git Product logo

slices's Introduction

Logo

Go Report Card Build Status Coverage Status Go Reference 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.

slices's People

Contributors

dependabot[bot] avatar nrwiersma avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

isgasho

slices's Issues

Add Join and Except operations

Hey, I love this project. I made something similar to this a couple years back, but your implementation is much more robust. I implemented the Intersection and Except operations and would love to see something similar here.

Intersection - Take two slices and return values that are the same in both slices

a := []int{1, 2, 3}
b := []int{2, 3, 4}
Intersection(a, b)
returns: [2 3]

Except - Take two slices and return values that only appear in the first slice

a := []int{1, 2, 5}
b := []int{2, 3, 4}
Except(a, b)
returns: [1 5]

My code is here. Feel free to use whatever you'd like. Super excited for your project!

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.