sliceop

package
v0.34.1 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2023 License: BSD-3-Clause Imports: 1 Imported by: 0

Documentation

Overview

Package sliceop provides operations on slices not available in the stdlib slices package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Filter

func Filter[T any](dst, src []T, f func(v T) bool) []T

Filter creates a slice with all the elements x_i of src for which f(x_i) is true. Filter uses dst as work buffer, storing elements at the start of the slice. Filter clears dst if a slice is passed, and allocates a new slice if dst is nil.

Example

An example of slice filtering

package main

import (
	"fmt"

	"go-hep.org/x/hep/sliceop"
)

func main() {
	slice := []float64{1, 2, -99, 4, 5, -99, 7}
	condition := func(x float64) bool { return x > 0 }
	fmt.Println(sliceop.Filter(nil, slice, condition))

}
Output:

[1 2 4 5 7]

func Find

func Find[S ~[]E, E any](dst []int, src S, f func(v E) bool) []int

Find creates a slice with all indices corresponding to elements for which f(x) is true. Find uses dst as work buffer, storing indices at the start of the slice. Find clears dst if a slice is passed, and allocates a new slice if dst is nil.

Example

An example of slice finding

package main

import (
	"fmt"

	"go-hep.org/x/hep/sliceop"
)

func main() {
	slice := []float64{1, 2, -99, 4, 5, -99, 7}
	condition := func(x float64) bool { return x == -99 }
	fmt.Println(sliceop.Find(nil, slice, condition))

}
Output:

[2 5]

func Map

func Map[T, U any](dst []U, src []T, f func(v T) U) []U

Map creates a slice with all the elements f(x_i) where x_i are elements from src. Map uses dst as work buffer, storing elements at the start of the slice. Map allocates a new slice if dst is nil. Map will panic if the lengths of src and dst differ.

Example

An example of slice mapping

package main

import (
	"fmt"

	"go-hep.org/x/hep/sliceop"
)

func main() {
	slice := []float64{1, 2, -99, 4, 5, -99, 7}
	operation := func(x float64) float64 { return x * x }
	fmt.Println(sliceop.Map(nil, slice, operation))

}
Output:

[1 4 9801 16 25 9801 49]

func Resize added in v0.33.0

func Resize[S ~[]E, E any](sli S, n int) S

Resize returns a slice of size n, reusing the storage of the provided slice, appending new elements if the capacity is not sufficient.

Example

An example of resizing a slice.

package main

import (
	"fmt"

	"go-hep.org/x/hep/sliceop"
)

func main() {
	slice := []int{1, 2, 3, 4}
	fmt.Printf("slice: len=%d, cap=%d, vs=%v\n", len(slice), cap(slice), slice)
	slice = sliceop.Resize(slice, 8)
	fmt.Printf("slice: len=%d, cap=%d, vs=%v\n", len(slice), cap(slice), slice)
	slice = sliceop.Resize(slice, 3)
	fmt.Printf("slice: len=%d, cap=%d, vs=%v\n", len(slice), cap(slice), slice)

}
Output:

slice: len=4, cap=4, vs=[1 2 3 4]
slice: len=8, cap=8, vs=[1 2 3 4 0 0 0 0]
slice: len=3, cap=8, vs=[1 2 3]

func Take

func Take[S ~[]E, E any](dst, src S, indices []int) S

Take creates a sub-slice of src with all elements indiced by the provided indices. Take uses dst as work buffer, storing elements at the start of the slice. Take clears dst if a slice is passed, and allocates a new slice if dst is nil. Take will panic if indices is not sorted or has duplicates. Take will panic if length of indices is larger than length of src. Take will panic if length of indices is different from length of dst.

Example

An example of taking a sub-slice defined by indices

package main

import (
	"fmt"

	"go-hep.org/x/hep/sliceop"
)

func main() {
	slice := []float64{1, 2, -99, 4, 5, -99, 7}
	indices := []int{2, 5}
	fmt.Println(sliceop.Take(nil, slice, indices))

}
Output:

[-99 -99]

Types

This section is empty.

Directories

Path Synopsis
Package f64s provides common operations on float64 slices.
Package f64s provides common operations on float64 slices.

Jump to

Keyboard shortcuts

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