slices

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2022 License: MIT Imports: 0 Imported by: 3

README

Slices

Pure functions for slices. Slices are never operated on "in place". Instead, new ones are returned.

codecov

Documentation

For full documentation see pkg.go.dev.

Install

go get github.com/twharmon/slices

Usage

package main

import (
	"fmt"

	"github.com/twharmon/slices"
)

func main() {
	// use plain go slices
	s := []string{"foo", "ba"}

	// append new item to end of slice
	s = slices.Append(s, "b")
	fmt.Println(s) // [foo ba b]
	
	// sort by string length, ascending
	sorted := slices.SortFunc(s, func(a, b string) bool {
		return len(a) < len(b)
	})
	// original slice is not chaged
	fmt.Println(s, sorted) // [foo ba b] [b ba foo]

	// sum the lengths of all the strings    
	totalLen := slices.Reduce(s, func(cnt int, i string) int {
		return cnt + len(i)
	})
	fmt.Println(totalLen) // 6

	// find the first item with length 2
	str := slices.Find(s, func(item string) bool { return len(item) == 2 })    
	fmt.Println(str) // ba

	// map slice to new slice of different type
	ints := slices.Map(s, func(item string) int { return len(s) })    
	fmt.Println(ints) // [3 2 1]
}

Benchmarks

goos: darwin
goarch: arm64
pkg: github.com/twharmon/slices

BenchmarkSortFunc/std_lib-10         	  116497	      9424 ns/op	    4152 B/op	       3 allocs/op
BenchmarkSortFunc/slices-10          	  101791	     11479 ns/op	    4096 B/op	       1 allocs/op
BenchmarkSort/std_lib-10             	  125790	      9441 ns/op	    4120 B/op	       2 allocs/op
BenchmarkSort/slices-10              	  113931	     10162 ns/op	    4096 B/op	       1 allocs/op

BenchmarkReverse-10                  	 1631348	       749.4 ns/op	    4096 B/op	       1 allocs/op
BenchmarkConcat-10                   	  275463	      4259 ns/op	   40960 B/op	       1 allocs/op
BenchmarkFilter-10                   	 1000000	      1018 ns/op	    4096 B/op	       1 allocs/op

BenchmarkUnion/2x20-10               	 3258876	       361.2 ns/op	      32 B/op	       1 allocs/op
BenchmarkUnion/20x2-10               	  502176	      2329 ns/op	    1592 B/op	       3 allocs/op
BenchmarkUnion/20x2000-10            	    1140	   1054108 ns/op	    1590 B/op	       3 allocs/op
BenchmarkUnion/2000x20-10            	     714	   1697117 ns/op	  142481 B/op	      39 allocs/op

BenchmarkIntersection/2x20-10        	 2380771	       498.0 ns/op	      48 B/op	       1 allocs/op
BenchmarkIntersection/20x2-10        	  821226	      1427 ns/op	    1196 B/op	       3 allocs/op
BenchmarkIntersection/20x2000-10     	    1178	   1020620 ns/op	    1282 B/op	       3 allocs/op
BenchmarkIntersection/2000x20-10     	     938	   1276258 ns/op	  203294 B/op	      58 allocs/op

Contribute

Make a pull request.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append added in v0.0.2

func Append[T any](s []T, item ...T) []T

Append appends an item to the slice and returns the new slice. The given slice is not changed.

func Clone

func Clone[T any](s []T) []T

Clone creates a clone slice and returns it.

func Concat

func Concat[T any](s ...[]T) []T

Concat combines the contents of all the given slices. The given slices are not changed.

func Contains

func Contains[T Ordered](s []T, item T) bool

Contains checks if any of the items in the given slice are equal to the given item.

func Distinct added in v0.0.4

func Distinct[T Ordered](s []T) []T

Distinct creates a new slice that contains all of the distinct items from the given slices without duplicates.

func Every

func Every[T any](s []T, test func(item T) bool) bool

Every checks is every item in the given slice satisfies the given test function.

func Filter

func Filter[T any](s []T, test func(item T) bool) []T

Filter creates a new slice that contains items from the given slice that satisfy the given test function and returns it. The given slice is not changed.

func Find

func Find[T any](s []T, test func(item T) bool) T

Find finds an item in the given slice that satisfies the given test function.

func IndexOf

func IndexOf[T Ordered](s []T, item T) int

IndexOf finds the index of the first item in the given slice that satisfies the given test function.

func IndexOfFunc added in v0.0.4

func IndexOfFunc[T any](s []T, test func(item T) bool) int

IndexOfFunc finds the index of the first item in the given slice that satisfies the given test function.

func Intersection added in v0.0.3

func Intersection[T Ordered](s ...[]T) []T

Intersection creates a new slice that contains the intersection of all the given slices. The given slices are not changed. All items in the returned slice are distinct.

func Map

func Map[T any, K any](s []T, m func(item T) K) []K

Map creates a new slice with items that are mapped to new values according to the given m function. The given slice is not changed.

func Max

func Max[T Ordered](s []T) T

Max returns the max item in the given slice.

func MaxFunc

func MaxFunc[T any](s []T, less func(T, T) bool) T

MaxFunc returns the max item in the given slice according to the given less func.

func Min

func Min[T Ordered](s []T) T

Min returns the min item in the given slice.

func MinFunc

func MinFunc[T any](s []T, less func(T, T) bool) T

MinFunc returns the min item in the given slice according to the given less func.

func Reduce

func Reduce[T any, K any](s []T, f func(prev K, cur T) K) K

Reduce iterates through the given slice, reducing the items to a value according to the given reducer function and returns the reduced value. The given slice is not changed.

func Reverse

func Reverse[T any](s []T) []T

Reverse creates a slice that is the reverse of the provided slice and returns it. The given slice is not changed.

func Some

func Some[T any](s []T, test func(item T) bool) bool

Some checks is any of the items in the given slice satisfies the given test function.

func Sort

func Sort[T Ordered](s []T) []T

Sort creates a new slice that is sorted in ascending order. The given slice is not changed.

func SortFunc

func SortFunc[T any](s []T, less func(a T, b T) bool) []T

SortFunc creates a new slice that is sorted in ascending order according the the given less func and returns it. The given slice is not changed.

func Splice

func Splice[T any](s []T, index int, cnt int, item ...T) []T

Splice creates a new slice that is spliced by removing or replacing existing elements and/or adding new elements in place. The given slice is not changed.

func Union added in v0.0.3

func Union[T Ordered](s ...[]T) []T

Union creates a new slice that contains the union of all the given slices. The given slices are not changed. All items in the returned slice are distinct.

func Unshift

func Unshift[T any](s []T, item ...T) []T

Unshift creates a new slice and prepends all the given items and returns it. The given slice is not changed.

Types

type Ordered

type Ordered interface {
	int | int32 | int16 | int8 | int64 | uint | uint32 | uint16 | uint8 | uint64 | float32 | float64 | string
}

Jump to

Keyboard shortcuts

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