collection

package
v0.0.0-...-77dcbbd Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2024 License: Apache-2.0 Imports: 0 Imported by: 0

Documentation

Overview

Package collection provides utility functions for slices.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BinarySearch

func BinarySearch[T any](list []T, less func(T) bool) int

BinarySearch returns the index where the target should exist based on the less function provided. less function should return true when value is less.

Example
res1 := BinarySearch([]int{3, 4, 5}, func(v int) bool { return v > 4 })
res2 := BinarySearch([]int{3, 4, 5}, func(v int) bool { return v > 6 })
res3 := BinarySearch([]int{3, 4, 5}, func(v int) bool { return v > 2 })
fmt.Println(res1, res2, res3)
Output:

2 3 0

func CommonPrefix

func CommonPrefix[T comparable](a, b []T) []T

CommonPrefix returns a new slice with common elements at the beginning of the both inputs.

func Copy

func Copy[T any](val []T) []T

Copy returns a new splice of the input using standard "copy" function.

func Equal

func Equal[T comparable](a, b []T) bool

Equal returns true if input a and b have the same value in the same order.

func Find

func Find[T any](list []T, predicate func(T) bool) T

Find returns first value of slice when predicate returns true. It returns default value of T if no match is found.

func FindIndex

func FindIndex[T any](list []T, predicate func(T) bool) int

FindIndex returns first index when predicate returns true. It returns "-1" if no match is found.

func Insert

func Insert[T any](list []T, index int, val T) []T

Insert a value into the specified index and return a new slice. Elements after the index will be shifted.

func Reverse

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

Reverse the input slice and return a new slice.

Types

This section is empty.

Directories

Path Synopsis
Package bytes provides utility functions for byte slices.
Package bytes provides utility functions for byte slices.
Package ints provides utility functions for int slices.
Package ints provides utility functions for int slices.
Package strings provides utility functions for string slices.
Package strings provides utility functions for string slices.

Jump to

Keyboard shortcuts

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