slices

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2022 License: BSD-3-Clause Imports: 1 Imported by: 2

README

Slices

A utility package for working with slices.

This package resembles the strings and bytes packages in the Go standard library.

Example Usage

package main

import (
	"github.com/vorduin/slices"
)

func main() {
	s := []int{1, 2, 3}

	clone := slices.Clone(s) // creates a copy and returns it
	slices.Equal(clone, s) // returns true

	slices.Sum(s) // returns the sum of the elems
	slices.Prod(s) // returns the prod of the elems

	slices.Contains(s, 0) // returns whether or not s contains a 0

	incByOne := func(x int) int {
		return x + 1
	}
	s = slices.Map(incByOne, s) // creates a a copy of s and maps the function on it
	// s is now [2, 3, 4]

	r := slices.Repeat(s, 2) // repeats s 2 times
	// s is now [2, 3, 4, 2, 3, 4]

	slices.Index(r, 2) // returns 0
	slices.LastIndex(r, 2) // returns 3

	slices.Join(s, r) // returns [2, 3, 4, 2, 3, 4, 2, 3, 4]

	slices.Count(r, 3) // returns the number of 3s in r

	slices.Reverse(s) // returns a copy of s with the elems reversed

	slices.ReplaceAll(s, 4, 0) // returns a copy of s with 4s replaced by 0s
}

License

Slices a BSD-style license, which can be found in the LICENSE file.

Documentation

Overview

Package slices is a utility package for working with slices, just like the strings and bytes packages.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clone added in v1.0.5

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

Clone makes a new slice and copies the given slice's elements into it.

func Contains added in v1.1.0

func Contains[T constraints.Ordered](s []T, el T) bool

Contains returns whether or not a slice contains a certain element.

func Count added in v1.1.0

func Count[T constraints.Ordered](s []T, el T) int

Count counts the number of instances of el in s

func Equal

func Equal[T constraints.Ordered](a, b []T) bool

Equal returns whether or not two slices are equal.

func Index added in v1.1.0

func Index[T constraints.Ordered](s []T, el T) int

Index returns the index of the first instance of el in s, or -1 if el is not present in s.

func Join added in v1.1.0

func Join[T any](lhs, rhs []T) []T

Join joins two slices together. If an argument is nil, it returns a clone of the other argument. If both are nil, it returns nil.

func Last added in v1.1.0

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

Last returns the last element in a slice. Panics if s is empty.

func LastIndex added in v1.1.0

func LastIndex[T constraints.Ordered](s []T, el T) int

LastIndex returns the index of the last instance of el in s, or -1 if el is not present in s.

func Map added in v1.1.0

func Map[T any](mapping func(T) T, s []T) []T

Map returns a copy of the slice s with all its elements modified according to the mapping function.

func Prod

func Prod[T constraints.Integer | constraints.Float](s []T) T

Prod returns the product of the elements of the slice.

func Repeat added in v1.1.0

func Repeat[T any](s []T, count int) []T

Repeat returns a new slice consisting of count copies of the slice s. It panics if count is negative or if the result of (len(s) * count) overflows.

func Replace added in v1.1.0

func Replace[T constraints.Ordered](s []T, old, new T, n int) []T

Replace returns a copy of the slice s with the first n instances of old replaced by new. If n < 0, there is no limit on the number of replacements.

func ReplaceAll added in v1.1.0

func ReplaceAll[T constraints.Ordered](s []T, old, new T) []T

ReplaceAll returns a copy of the slice s with all instances of old replaced by new.

func Reverse added in v1.1.0

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

Reverse returns a copy of the slice s with order of the elements reversed.

func Sum

func Sum[T constraints.Integer | constraints.Float](s []T) T

Sum returns the sum of the elements of the slice.

func WithCap

func WithCap[T any](c int) []T

WithCap makes a new slice with the given capacity and returns it.

func WithLen

func WithLen[T any](l int) []T

WithLen makes a new slice with the given length and returns it.

Types

This section is empty.

Jump to

Keyboard shortcuts

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