slice

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2022 License: Apache-2.0 Imports: 2 Imported by: 1

Documentation

Overview

Package slice returns generic functions to work with slices.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains[T comparable](s []T, elem T) bool

Contains checks whether a slice contains an element

func ContainsEq

func ContainsEq[T any](s []T, elem T, eq equality.Equality[T]) bool

ContainsEq checks whether a slice contains an element

func Equal

func Equal[T any](a []T, b []T, eq equality.Equality[T]) bool

Equal checks whether two slices are equal

func Exists

func Exists[T any](s []T, cond func(T) bool) bool

Exists checks whether some element in the slice fulfills the given condition

func Filter added in v1.3.0

func Filter[A any](s []A, p func(A) bool) []A

Filter only keeps the elements where the predicate returns true

Example
package main

import (
	"fmt"

	"github.com/peterzeller/go-fun/slice"
)

func main() {
	l := []int{4, 13, 7, 13, 13, 8}
	r := slice.Filter(l, func(x int) bool { return x < 10 })
	fmt.Printf("r = %#v\n", r)
}
Output:

r = []int{4, 7, 8}

func Forall

func Forall[T any](s []T, cond func(T) bool) bool

Forall checks whether all elements in the slice fulfill the given condition

func IndexOf

func IndexOf[T any](elem T, elems []T, eq equality.Equality[T]) int

IndexOf returns the index of the first occurrence of elem in the slice elems or -1 if elem is not in the slice.

func Map added in v1.3.0

func Map[A, B any](s []A, f func(A) B) []B

Map maps a function over the slice

Example
package main

import (
	"fmt"

	"github.com/peterzeller/go-fun/slice"
)

func main() {
	l1 := []int{1, 2, 3}
	l2 := slice.Map(l1, func(x int) string { return fmt.Sprintf("%dx", x) })
	fmt.Printf("l2 = %#v\n", l2)
}
Output:

l2 = []string{"1x", "2x", "3x"}

func PrefixOf

func PrefixOf[T any](a, b []T, eq equality.Equality[T]) bool

PrefixOf checks whether a is a prefix of b

func RemoveAll

func RemoveAll[T any](s []T, elem T, eq equality.Equality[T]) []T

RemoveAll removes all occurrences of the element from the slice and returns the modified slice.

Example
package main

import (
	"fmt"

	"github.com/peterzeller/go-fun/equality"
	"github.com/peterzeller/go-fun/slice"
)

func main() {
	l := []int{4, 13, 7, 13, 13, 8}
	r := slice.RemoveAll(l, 13, equality.Default[int]())
	fmt.Printf("r = %#v\n", r)
}
Output:

r = []int{4, 7, 8}

func RemoveAt

func RemoveAt[T any](s []T, index int) []T

RemoveAt removes the element at the given index from the slice and returns the modified slice.

Example
package main

import (
	"fmt"

	"github.com/peterzeller/go-fun/slice"
)

func main() {
	l := []int{4, 13, 7, 8}
	r := slice.RemoveAt(l, 2)
	fmt.Printf("r = %#v\n", r)
}
Output:

r = []int{4, 13, 8}

func RemoveFirst

func RemoveFirst[T any](s []T, elem T, eq equality.Equality[T]) []T

RemoveFirst removes the first occurrence of an element from the slice and returns the modified slice.

Example
package main

import (
	"fmt"

	"github.com/peterzeller/go-fun/equality"
	"github.com/peterzeller/go-fun/slice"
)

func main() {
	l := []int{4, 13, 7, 13, 13, 8}
	r := slice.RemoveFirst(l, 13, equality.Default[int]())
	fmt.Printf("r = %#v\n", r)
}
Output:

r = []int{4, 7, 13, 13, 8}

func Shuffle added in v1.3.0

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

Shuffle puts a slice into random order

Types

This section is empty.

Jump to

Keyboard shortcuts

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