Documentation ¶
Overview ¶
Package slices provides helpful functions for searching, sorting and manipulating slices.
Index ¶
- func All[S ~[]T, T any](vals S, comp func(T) bool) bool
- func Any[S ~[]T, T any](vals S, comp func(T) bool) bool
- func Chunk[T any](vals []T, size int) [][]T
- func Convert[To any, S ~[]From, From any](from S) ([]To, error)
- func Count[S ~[]T, T any](vals S, comp func(T) bool) int
- func Filter[S ~[]T, T any](vals S, comp func(T) bool) S
- func Includes[S ~[]T, T any](vals S, target T) bool
- func IndexOf[S ~[]T, T any](vals S, target T) int
- func IsIdentical[S2 ~[]T, S1 ~[]T, T any](lhs S1, rhs S2) bool
- func IsUnique[T comparable](vals []T) bool
- func Join[S ~[]T, T any](vals S, delimiter string) string
- func Map[T any, U any](vals []T, mapper func(T) U) []U
- func MustConvert[To any, S ~[]From, From any](from S) []To
- func Reverse[S ~[]T, T any](t S)
- func Sort[S ~[]T, T constraints.Ordered](vals S)
- func Transform[S ~[]T, T any](vals S, transform func(T) T)
- func Unique[S ~[]T, T comparable](vals S) S
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶
All returns true if every element in a slice satisfies the given condition. Defaults to true for an empty slice.
Example ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/slices" ) func main() { greaterThanFive := func(i int) bool { return i > 5 } arr := []int{1, 2, 3} arr2 := []int{7, 8, 9} all := slices.All(arr, greaterThanFive) fmt.Printf("arr all > 5: %v\n", all) all = slices.All(arr2, greaterThanFive) fmt.Printf("arr2 all > 5: %v\n", all) }
Output: arr all > 5: false arr2 all > 5: true
func Any ¶
Any returns true if at least one element in a slice satisfies the given condition. Defaults to false for an empty slice.
Example ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/slices" ) func main() { isEven := func(i int) bool { return i%2 == 0 } oddArr := []int{1, 3, 5, 7} evenArr := []int{1, 2, 9} any := slices.Any(oddArr, isEven) fmt.Printf("oddArr any even: %v\n", any) any = slices.Any(evenArr, isEven) fmt.Printf("evenArr any even: %v\n", any) }
Output: oddArr any even: false evenArr any even: true
func Chunk ¶
Chunk divides the given slice into multiple new slices, each at most having lengths of the given size. The last chunk may have a smaller length if the length of vals is not evenly divisible by size.
Example ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/slices" ) func main() { strArr := []string{"a", "b", "c", "d", "e"} strChunks := slices.Chunk(strArr, 5) fmt.Printf("str arr chunks: %v\n", strChunks) intArr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} intChunks := slices.Chunk(intArr, 3) fmt.Printf("int arr chunks: %v\n", intChunks) }
Output: str arr chunks: [[a b c d e]] int arr chunks: [[1 2 3] [4 5 6] [7 8 9] [10]]
func Convert ¶
Convert returns an array of objects of type To from an array of objects of type From. If any conversion fails, this will return an error.
This function, along with the non-failing `MustConvert` equivalent, are useful for converting one array type []T to an array of another type []U, which requires manual iteration in Go. This provides a more convenient mechanism for casting between arrays of interfaces and concrete types, provided all elements can safely be casted to.
Note: this function only works with language-level type-casts (e.g. `t.(U)`). For converting between concrete struct types, use `Map`.
Example (Bad_conversion) ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/slices" ) func main() { arr := []any{1, 2, 3} intArr, err := slices.Convert[string](arr) if err != nil { fmt.Printf("Unable to convert slice!") } else { fmt.Printf("int array: %v", intArr) } }
Output: Unable to convert slice!
Example (Good_conversion) ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/slices" ) func main() { arr := []any{1, 2, 3} intArr, err := slices.Convert[int](arr) if err != nil { fmt.Printf("Unable to convert slice!") } else { fmt.Printf("int array: %v", intArr) } }
Output: int array: [1 2 3]
func Count ¶
Count returns the number of elements in a slice that match the given condition.
Example ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/slices" ) func main() { isNegative := func(i int) bool { return i < 0 } arr := []int{1, 3, -9, -5, 6} countNeg := slices.Count(arr, isNegative) fmt.Printf("count of negative nums: %v\n", countNeg) }
Output: count of negative nums: 2
func Filter ¶
Filter returns a subset of the original slice, consisting of all the elements in the original slice which satisfy the given condition.
Example ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/slices" ) func main() { isNegative := func(i int) bool { return i < 0 } arr := []int{1, 3, -9, -5, 6} filtered := slices.Filter(arr, isNegative) fmt.Printf("filtered arr: %v\n", filtered) }
Output: filtered arr: [-9 -5]
func Includes ¶
Includes returns true if a slice contains the target value.
Example ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/slices" ) func main() { arr := []string{"a", "c", "e"} includesB := slices.Includes(arr, "b") fmt.Printf("arr includes b: %v\n", includesB) includesA := slices.Includes(arr, "a") fmt.Printf("arr includes a: %v\n", includesA) }
Output: arr includes b: false arr includes a: true
func IndexOf ¶
IndexOf returns the index of the target value in a slice. Returns the first index found, and -1 if the value is not found.
Example ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/slices" ) func main() { letters := []string{"w", "o", "o", "a", "h"} indexM := slices.IndexOf(letters, "m") fmt.Printf("index of m: %v\n", indexM) indexO := slices.IndexOf(letters, "o") fmt.Printf("index of o: %v\n", indexO) }
Output: index of m: -1 index of o: 1
func IsIdentical ¶
IsIdentical checks if two slices refer to the same underlying slice object.
func IsUnique ¶
func IsUnique[T comparable](vals []T) bool
IsUnique returns true if all elements of a slice are unique.
Example ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/slices" ) func main() { strArr := []string{"hello", "world"} intArr := []int{1, 2, 3, 2, 1} strUnique := slices.IsUnique(strArr) fmt.Printf("str arr unique: %v\n", strUnique) intUnique := slices.IsUnique(intArr) fmt.Printf("int arr unique: %v\n", intUnique) }
Output: str arr unique: true int arr unique: false
func Join ¶
Join returns a string consisting of all elements in a slice, separated by the given delimiter.
Example ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/slices" ) func main() { arr := []int{0, 0} joined := slices.Join(arr, ".") fmt.Printf("joined string: %v\n", joined) }
Output: joined string: 0.0
func Map ¶
Map returns a new slice with the elements of the original slice transformed according to the provided function.
Example ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/slices" ) func main() { arr := []int{1, 2, 3} mapper := func(i int) string { return fmt.Sprintf("%v_%v", i, i) } mapped := slices.Map(arr, mapper) fmt.Printf("mapped arr: %v\n", mapped) }
Output: mapped arr: [1_1 2_2 3_3]
func MustConvert ¶
MustConvert returns an array of To objects by converting every element in From to To. This will panic on failure to convert.
This function, along with the failing `Convert` equivalent, are useful for converting one array type []T to an array of another type []U, which requires manual iteration in Go. This provides a more convenient mechanism for casting between arrays of interfaces and concrete types, provided all elements can safely be casted to.
This function in particular should only be used if the 'To' type is guaranteed to always be convertible -- such as converting a concrete type to its base interface, or to any.
Example ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/slices" ) func main() { // Easy mechanism to cast arrays to interfaces and back arr := []any{1, 2, 3} intArr := slices.MustConvert[int](arr) fmt.Printf("int array: %v", intArr) }
Output: int array: [1 2 3]
func Reverse ¶
func Reverse[S ~[]T, T any](t S)
Reverse performs an in-place reversal of the elements in a slice. Performance testing has not been done to compare to alternatives.
Example ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/slices" ) func main() { arr := []int{1, 2, 3, 3, 4} slices.Reverse(arr) fmt.Printf("reversed arr: %v\n", arr) }
Output: reversed arr: [4 3 3 2 1]
func Sort ¶
func Sort[S ~[]T, T constraints.Ordered](vals S)
Sort performs an in-place sort of a slice. Performance testing has not been done to compare to alternatives.
Example ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/slices" ) func main() { arr := []int{1, -2, 3, -4, 5} slices.Sort(arr) fmt.Printf("sorted arr: %v\n", arr) }
Output: sorted arr: [-4 -2 1 3 5]
func Transform ¶
func Transform[S ~[]T, T any](vals S, transform func(T) T)
Transform performs an in-place transformation of a slice.
Example ¶
package main import ( "fmt" "strings" "github.com/verily-src/fhirpath-go/internal/slices" ) func main() { arr := []string{"\t hello\n", "\t world\n"} slices.Transform(arr, strings.TrimSpace) fmt.Printf("transformed arr: %v\n", arr) }
Output: transformed arr: [hello world]
func Unique ¶
func Unique[S ~[]T, T comparable](vals S) S
Unique returns a new slice of the unique elements in a given slice. It keeps the first instance of any duplicate values and ignores any subsequent instances of the value.
Example ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/slices" ) func main() { arr := []int{1, 2, 3, 2, 1} uniqueArr := slices.Unique(arr) fmt.Printf("unique arr: %v\n", uniqueArr) }
Output: unique arr: [1 2 3]
Types ¶
This section is empty.