Filter a array , with a function that return a boolean.
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
// filter
result := array.Filter(data, func(i int) bool {
return i%2 == 0
})
fmt.Println(result) // [2 4 6 8 10]
Find a element in the array , with a function that return a boolean.
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
// filter
result := array.Find(data, func(i int) bool {
return i%2 == 0
})
fmt.Println(result) // 2
Map a array , with a function that return a value.
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
// filter
result := array.Map(data, func(i int) int {
return i * 2
})
fmt.Println(result) // [2 4 6 8 10 12 14 16 18 20]
FlatMap a array , with a function that return a slice.
a := []int{1, 2, 3, 4, 5}
b := array.FlatMap(a, func(x int) []int { return []int{x, x * 2} })
fmt.Println(b) // [1 2 2 4 3 6 4 8 5 10]
Reduce a array , with a function that return a value.
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
// filter
result := array.Reduce(data, func(i, j int) int {
return i + j
})
fmt.Println(result) // 55
Check if any element in the array match the condition.
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
// filter
result := array.Any(data, func(i int) bool {
return i%2 == 0
})
fmt.Println(result) // true
Check if some element in the array match the condition.
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
// filter
result := array.Some(data, func(i int) bool {
return i%2 == 0
})
fmt.Println(result) // true
Check if every element in the array match the condition.
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
// filter
result := array.Every(data, func(i int) bool {
return i%2 == 0
})
fmt.Println(result) // false
Sum all elements in the array.
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
// filter
result := array.Sum(data)
fmt.Println(result) // 55
Get the max element in the array.
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
// filter
result := array.Max(data)
fmt.Println(result) // 10
Get the min element in the array.
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
// filter
result := array.Min(data)
fmt.Println(result) // 1
Get the product of all elements in the array.
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
// filter
result := array.Product(data)
fmt.Println(result) // 3628800
Reverse te order of the array.
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
// filter
result := array.Reverse(data)
fmt.Println(result) // [10 9 8 7 6 5 4 3 2 1]
Shuffle the array.
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
// filter
result := array.Shuffle(data)
fmt.Println(result) // [10 9 8 7 6 5 4 3 2 1]
Get unique elements in the array.
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
// filter
result := array.Unique(data)
fmt.Println(result) // [1 2 3 4 5 6 7 8 9 10]
Get union of two arrays.
a := []int{1, 2, 3, 4, 5}
b := []int{6, 7, 8, 9, 10}
// filter
result := array.Union(a, b)
fmt.Println(result) // [1 2 3 4 5 6 7 8 9 10]
Fill the array with a value.
a := []int{1, 2, 3, 4, 5}
b := array.Fill(a, 0)
fmt.Println(b) // [0 0 0 0 0]
Join the array with a separator.
a := []int{1, 2, 3, 4, 5}
b := array.Join(a, "-")
fmt.Println(b) // 1-2-3-4-5
Pop the last element of the array, and return the removed element and the new array.
a := []int{1, 2, 3, 4, 5}
b, c := array.Pop(a)
fmt.Println(b) // 5
fmt.Println(c) // [1 2 3 4]
Push an element to the array, and return the new array.
a := []int{1, 2, 3, 4, 5}
b := array.Push(a, 6)
fmt.Println(b) // [1 2 3 4 5 6]
Shift the first element of the array, and return the removed element and the new array.
a := []int{1, 2, 3, 4, 5}
b, c := array.Shift(a)
fmt.Println(b) // 1
fmt.Println(c) // [2 3 4 5]
Sort the array using the function.
a := []int{3, 2, 1, 5, 4}
b := array.Sort(a, func(i, j int) bool { return a[i] < a[j] })
fmt.Println(b) // [1 2 3 4 5]
type Itens struct {
Name string
Price float64
Description string
Qty int
}
itens := []Itens{
{"Item 1", 10.0, "Description 1", 1},
{"Item 2", 20.0, "Description 2", 2},
{"Item 3", 30.0, "Description 3", 3},
{"Item 4", 40.0, "Description 4", 10},
{"Item 4", 40.0, "Description 4", 15},
{"Item 4", 40.0, "Description 4", 25},
}
grouped := array.GroupBy(itens, func(item Itens) string {
return fmt.Sprintf("%s - %v", item.Name, item.Price)
})
fmt.Println(len(grouped)) // 4
You can chain the functions together.
data := array.Array[int]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
a := data.
Filter(func(i int) bool {
return i%2 == 0
}).
Map(func(i int) int {
return i * 2
}).
fmt.Println(a) // [4 8 12 16 20]