https://github.com/peterq/slice-chain
像 JavaScript 操作数组一样链式操作 golang 的slice
https://github.com/peterq/slice-chain
Last synced: 12 months ago
JSON representation
像 JavaScript 操作数组一样链式操作 golang 的slice
- Host: GitHub
- URL: https://github.com/peterq/slice-chain
- Owner: peterq
- Created: 2019-10-25T08:55:52.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2021-09-27T02:02:01.000Z (over 4 years ago)
- Last Synced: 2025-04-11T12:23:05.629Z (about 1 year ago)
- Language: Go
- Homepage:
- Size: 16.6 KB
- Stars: 3
- Watchers: 1
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
> chain style operation for golang's slice just like javascript array
support operation:
- Map:
- convert slice of any type A to slice of any type B
- Filter:
- delete elements of a slice you do not need
- Sort:
- sort a slice of any type on your rule
- Find:
- find the element in the slice you need
- Reverse:
- reverse the element
- Reduce:
- walk the slice reduce to a single value
> 像 JavaScript 操作数组一样链式操作 golang 的slice
支持的操作:
- Map:
- 从一种类型的切片转换成另一种类型的切片
- Filter:
- 过滤切片中不符合条件的元素, 返回新的切片
- Sort:
- 自定义规则对切片进行排序
- Find:
- 找出切片中符合条件的第一个元素, 返回该元素和该元素的索引
- Reverse:
- 颠倒切片元素先后顺序
- Reduce:
- 遍历数组, 归并成一个值
eg: run:
```go
package main
import (
"fmt"
"github.com/peterq/slice-chain"
"strconv"
"strings"
)
func main() {
var a = []string{"1", "2", "3", "4", "5"}
slice_chain.Collect(a).
Map(func(s string) int { i, _ := strconv.Atoi(s); return i }).
Filter(func(i int) bool { return i%2 == 0 }).
Map(func(i int) string { return fmt.Sprintf("%d * %d = %d", i, i, i*i) }).
SaveTo(&a)
println(strings.Join(a, "\n"))
}
```
got output:
```
2 * 2 = 4
4 * 4 = 16
```
> more usage you can find in the `chain_test.go` file
issue and pr are welcomed ;)
love golang, love china. may the world peace :)