github.com/hamba/slices@v0.2.1-0.20220316050741-75c057d92699/README.md (about)

     1  ![Logo](http://svg.wiersma.co.za/hamba/project?title=slices&tag=Slice%20helper%20functions)
     2  
     3  [![Go Report Card](https://goreportcard.com/badge/github.com/hamba/slices)](https://goreportcard.com/report/github.com/hamba/slices)
     4  [![Build Status](https://github.com/hamba/cmd/actions/workflows/test.yml/badge.svg)](https://github.com/hamba/cmd/actions)
     5  [![Coverage Status](https://coveralls.io/repos/github/hamba/slices/badge.svg?branch=master)](https://coveralls.io/github/hamba/slices?branch=master)
     6  [![Go Reference](https://pkg.go.dev/badge/github.com/hamba/slices.svg)](https://pkg.go.dev/github.com/hamba/slices)
     7  [![GitHub release](https://img.shields.io/github/release/hamba/slices.svg)](https://github.com/hamba/slices/releases)
     8  [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/hamba/slices/master/LICENSE)
     9  
    10  Go slice helper functions
    11  
    12  ## Overview
    13  
    14  Install with:
    15  
    16  ```shell
    17  go get github.com/hamba/slices
    18  ```
    19  
    20  ## Usage
    21  
    22  ### Contains
    23  
    24  Contains is a generic slice contains function.
    25  
    26  Supports: bool, string, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64
    27  
    28  ```go
    29  slice := []string{"foo", "bar"}
    30  v := "bar"
    31  
    32  fmt.Println(slices.Contains(slice, v))
    33  // Outputs: true
    34  ```
    35  
    36  ### GreaterOf
    37  
    38  GreaterOf returns a greater function for the given slice type for slice sorting.
    39  
    40  Supports: string, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64
    41  
    42  ```go
    43  slice := []string{"foo", "bar"}
    44  sort.Slice(slice, slices.GreaterOf(slice))
    45  
    46  fmt.Println(slice)
    47  // Outputs: [foo bar]
    48  ```
    49  
    50  ### LesserOf
    51  
    52  LesserOf returns a lesser function for the given slice type for slice sorting.
    53  
    54  Supports: string, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64
    55  
    56  ```go
    57  slice := []string{"foo", "bar"}
    58  sort.Slice(slice, slices.LesserOf(slice))
    59  
    60  fmt.Println(slice)
    61  // Outputs: [bar foo]
    62  ```
    63  
    64  ### Intersect
    65  
    66  Intersect returns the intersection of 2 slices.
    67  
    68  Supports: string, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64
    69  
    70  ```go
    71  slice := []string{"foo", "bar", "baz", "bat"}
    72  other := []string{"bar", "baz", "test"}
    73  
    74  fmt.Println(slices.Intersect(slice, other))
    75  // Outputs: [bar baz]
    76  ```
    77  
    78  ### Except
    79  
    80  Except returns the all elements in the first slice that are not in the second.
    81  
    82  Supports: string, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64
    83  
    84  ```go
    85  slice := []string{"foo", "bar", "baz", "bat"}
    86  other := []string{"bar", "baz", "test"}
    87  
    88  fmt.Println(slices.Except(slice, other))
    89  // Outputs: [foo bat]
    90  ```
    91  
    92  ## Benchmark
    93  
    94  ```
    95  BenchmarkContains-8          	35621572	        30.5 ns/op	       0 B/op	       0 allocs/op
    96  BenchmarkContainsNative-8    	50106157	        23.9 ns/op	       0 B/op	       0 allocs/op
    97  BenchmarkExcept-8            	 6070610	       200 ns/op	      96 B/op	       2 allocs/op
    98  BenchmarkExceptNative-8      	 5933550	       193 ns/op	      96 B/op	       2 allocs/op
    99  BenchmarkGreaterOf-8         	 6290626	       189 ns/op	      80 B/op	       3 allocs/op
   100  BenchmarkGreaterOfNative-8   	 8201284	       149 ns/op	      64 B/op	       2 allocs/op
   101  BenchmarkIntersect-8         	 6012298	       196 ns/op	      96 B/op	       2 allocs/op
   102  BenchmarkIntersectNative-8   	 6305799	       198 ns/op	      96 B/op	       2 allocs/op
   103  BenchmarkLesserOf-8          	 6449050	       189 ns/op	      80 B/op	       3 allocs/op
   104  BenchmarkLesserOfNative-8    	 8077785	       149 ns/op	      64 B/op	       2 allocs/op
   105  ```
   106  
   107  Always benchmark with your own workload. The result depends heavily on the data input.