slices

package module
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2024 License: MIT Imports: 5 Imported by: 0

README

slices

SPAN Digital Slices

Open in Dev Containers Develop Go Action Workflow Status Main Go Action Workflow Status Release status

Usage

Generic functions which operate on Go slices.

func Filter[S ~[]V, V any](s S, predicate func(V V) bool) []V

Filter a slice with a predicate

package main

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

func main() {
	a := []int{1, 2, 3, 4, 5, 6, 7, 8}
	
	b := slices.Filter(a, func(v int) bool {
        return v % 2 == 0 // even
	})
	
	println(b) // 2, 4, 6, 8
}
func Flatten[T any](s [][]T) (flattened []T) and 3+ dimensional variants

This function flats a 2 dimensional slice into a 1 dimensional slice, There are variants Flatten3, Flatten4 for 3 and 4 dimensions respectively.

package main

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

func main() {
	a := [][]int{{1, 2, 3, 4}, {5, 6}, {7, 8}}
	
	b := slices.Flatten(a)
	
	println(b) // 1, 2, 3, 4, 5, 6, 7, 8
}
func FilterNil[T any](in []*T) (out []*T)

Removes nil values from slices

Usage:

package main

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

func main() {
	type exampleStruct struct{}

	a := new(exampleStruct)
	b := new(exampleStruct)
	c := new(exampleStruct)

	filtered := slices.FilterNil([]*exampleStruct{a, b, nil, c, nil})
	println(len(filtered)) // 3
}
func AppendNotNil
package main

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

func main() {
	type exampleStruct struct{}

	a := new(exampleStruct)
	b := new(exampleStruct)
	c := new(exampleStruct)

	filtered := slices.FilterNil([]*exampleStruct{a, b, nil, c, nil})
	println(len(filtered)) // 3
}    
func Contains[S ~[]E, E comparable](s S, v E) bool
package main

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

func main() {
	println(slices.Contains([]int{1,2,3,4,5}, 4)) //true
	println(slices.Contains([]string{"foo", "bar"}, "baz")) //false
}
func Gro
func Index[S ~[]E, E comparable](s S, v E) int
package main

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

func main() {
	println(slices.Index([]int{1,2,3,4,5}, 4)) //3
	println(slices.Contains([]string{"foo", "bar"}, "baz")) //-1
}
func Intersection[T cmp.Ordered](pS ...[]T) []T
package main

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

func main() {
	println(slices.Intersection([]int{1,2,3,4,5}, []int{4,5,6,7,8})) // {4, 5}
	println(slices.Intersection([]string{"foo", "bar"}, []string{"baz"})) // {}
}
func Map[S ~[]V, V any, E any](s S, extract func(v V) E) []E
package main

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

func main() {
	println(slices.Map([]int{1,2,3,4,5}, func(v int) {
	    return v*2
	}) // 2, 4, 6, 8, 10
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendNotNil added in v0.4.0

func AppendNotNil[T any](in []*T, values ...*T) (out []*T)

func Contains

func Contains[S ~[]E, E comparable](s S, v E) bool

Contains reports whether v is present in s.

func Filter

func Filter[S ~[]V, V any](s S, predicate func(V V) bool) []V

func FilterNil added in v0.4.0

func FilterNil[T any](in []*T) (out []*T)

func Flatten

func Flatten[T any](s [][]T) (flattened []T)

func Flatten3

func Flatten3[T any](s [][][]T) (flattened []T)

func Flatten4

func Flatten4[T any](s [][][][]T) (flattened []T)

func GroupBy

func GroupBy[S ~[]V, V any, K comparable](s S, extract func(v V) K) map[K][]V

func GroupByLen added in v0.7.0

func GroupByLen[S ~[]V, V any](input S, length int) (output [][]V)

func Index

func Index[S ~[]E, E comparable](s S, v E) int

Index returns the index of the first occurrence of v in s, or -1 if not present.

func Intersection added in v0.2.1

func Intersection[T cmp.Ordered](pS ...[]T) []T

func Map

func Map[S ~[]V, V any, E any](s S, extract func(v V) E) []E

func MapFrom

func MapFrom[S ~[]I, I any, K comparable, V any](s S, extractKey func(i I) K, extractValue func(i I) V) map[K]V

func NumPages

func NumPages[V any](s []V, pageSize int) int

func Page

func Page[V any](s []V, pageSize int, pageIndex int) []V

func RemoveNil

func RemoveNil[T any](in []T) (out []T)

func SyncMap added in v0.5.0

func SyncMap[S ~[]V, V any, E any](ctx context.Context, s S, extract func(ctx context.Context, value V) (mapped E, err error)) (mapped []E, err error)

func Unique added in v0.3.0

func Unique[T comparable](s []T) []T

Types

This section is empty.

Jump to

Keyboard shortcuts

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