preface

Just two days ago, Go released Beta 1 version 1.18, which officially supports generics, making it possible to implement a generic Slice library, so I immediately tried to encapsulate commonly used Slice operations.

If you need to understand the basic syntax of Go generics, you can read this article: Go Generics Quick Start.

The following code requires Go version 1.18 Beta 1 and above

ForEach

Execute the action function on each element

func ForEach[T any](slice []T, action func(T)) {
   for _, item := range slice {
      action(item)
   }
}
Copy the code

Map

Convert a T1 slice to a T2 slice

func Map[T1 any.T2 any](slice []T1, mapper func(T1) T2) []T2 {
   mapped := make([]T2, 0.len(slice))
   for _, item := range slice {
      mapped = append(mapped, mapper(item))
   }
   return mapped
}
Copy the code

Filter

Filter elements that do not meet the criteria

func Filter[T any](slice []T, condition func(T) bool) []T {
   var filtered []T
   for _, item := range slice {
      if condition(item) {
         filtered = append(filtered, item)
      }
   }
   return filtered
}
Copy the code

Reduce

Convert slice to a value

func Reduce[T any](slice []T, reduce func(cur T, item T) T.init T) T {
   for i := 0; i < len(slice); i++ {
      init = reduce(init, slice[i])
   }
   return init
}
Copy the code

Every

Whether each element satisfies the condition

func Every[T any](slice []T, condition func(T) bool) bool {
   for _, item := range slice {
      if! condition(item) {return false}}return true
}
Copy the code

Find

Returns the first element that meets the condition

func Find[T any](slice []T, condition func(T) bool) (T, bool) {
   for _, item := range slice {
      if condition(item) {
         return item, true}}var t T
   return t, false
}
Copy the code

FindIndex

Returns the index of the first element that meets the condition

func FindIndex[T any](slice []T, condition func(T) bool) int {
   for i, item := range slice {
      if condition(item) {
         return i
      }
   }
   return - 1
}
Copy the code

IndexOf

Returns the index of the first element whose value is equal

func IndexOf[T comparable](slice []T, item T) int {
   for i := 0; i < len(slice); i++ {
      if slice[i] == item {
         return i
      }
   }
   return - 1
}
Copy the code

LastIndexOf

Returns the index of the last element whose value is equal

func LastIndexOf[T comparable](slice []T, item T) int {
   for i := len(slice) - 1; i >= 0; i-- {
      if slice[i] == item {
         return i
      }
   }
   return - 1
}
Copy the code

ReduceRight

Convert slice to a value, starting at the right

func ReduceRight[T any](slice []T, reduce func(cur T, item T) T.init T) T {
   for i := len(slice) - 1; i >= 0; i-- {
      init = reduce(init, slice[i])
   }
   return init
}
Copy the code

Reverse

Reverse slice

func Reverse[T any](slice []T) []T {
   reversed := make([]T, 0.len(slice))
   for i := len(slice) - 1; i >= 0; i-- {
      reversed = append(reversed, slice[i])
   }
   return reversed
}
Copy the code

Shift

Remove the first element

func Shift[T any](slice []T) ([]T, T) {
   t := slice[0]
   return slice[1:], t
}
Copy the code

Shuffle

Disrupted slice

func Shuffle[T any](slice []T) {
   for i := 0; i < len(slice); i++ {
      rand.Shuffle(len(slice), func(i, j int) {
         slice[i], slice[j] = slice[j], slice[i]
      })
   }
}
Copy the code

Some

Is there any element that satisfies this condition

func Some[T any](slice []T, condition func(T) bool) bool {
   for _, item := range slice {
      if condition(item) {
         return true}}return false
}
Copy the code

Unshift

Add elements to the Slice header

func Unshift[T any](slice []T, items ... T) []T {
   return append(items, slice...)
}
Copy the code

The complete code

Github:github.com/jiaxwu/slic…

You can also run the go get github.com/jiaxwu/slices command to import