https://github.com/chyroc/go-lambda
Process data like functional programming
https://github.com/chyroc/go-lambda
Last synced: 6 months ago
JSON representation
Process data like functional programming
- Host: GitHub
- URL: https://github.com/chyroc/go-lambda
- Owner: chyroc
- License: apache-2.0
- Created: 2021-09-23T05:16:33.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-05-19T03:58:14.000Z (over 2 years ago)
- Last Synced: 2025-01-29T08:23:18.684Z (11 months ago)
- Language: Go
- Size: 127 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-lambda
[](https://codecov.io/gh/chyroc/go-lambda)
[](https://goreportcard.com/report/github.com/chyroc/go-lambda)
[](https://github.com/chyroc/go-lambda/actions)
[](https://opensource.org/licenses/Apache-2.0)
[](https://pkg.go.dev/github.com/chyroc/go-lambda)
[](https://badge.fury.io/go/github.com%2Fchyroc%2Fgo-lambda)

Process data like functional programming
## Install
```shell
go get github.com/chyroc/go-lambda
```
## Usage
### A simple example
```go
func ExampleNew() {
obj := lambda.New([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
obj = obj.FilterList(func(idx int, obj interface{}) bool {
return obj.(int)%2 == 0
})
fmt.Println(obj.ToIntSlice())
obj = obj.Chunk(3)
fmt.Println(obj.ToExpectType([][]int{}))
obj = obj.Flatten()
fmt.Println(obj.ToIntSlice())
obj = obj.Compact()
fmt.Println(obj.ToIntSlice())
obj = obj.MapList(func(idx int, obj interface{}) interface{} {
return exampleItem{Name: fmt.Sprintf("name-%d", obj.(int))}
})
fmt.Println(obj.ToExpectType([]exampleItem{}))
// output:
// [0 2 4 6 8]
// [[0 2 4] [6 8]]
// [0 2 4 6 8]
// [2 4 6 8]
// [{name-2} {name-4} {name-6} {name-8}]
}
```
### Chunk
```go
func ExampleObject_Chunk() {
// Split the list into shorter length lists
res, err := lambda.New([]int{1, 2, 3, 4, 5}).Chunk(2).ToIntListList()
fmt.Println("err:", err)
fmt.Println("res:", res)
// output:
// err:
// res: [[1 2] [3 4] [5]]
}
```
### Compact
```go
func ExampleObject_Compact() {
// Remove 0-valued elements from the list
res, err := lambda.New([]int{0, 1, 2, 1, 0, 2}).Compact().ToIntSlice()
fmt.Println("err:", err)
fmt.Println("res:", res)
// output:
// err:
// res: [1 2 1 2]
}
```
### Flatten
```go
func ExampleObject_Flatten() {
// Flatten the list
res, err := lambda.New([][]int{{1, 2}, {2, 3}, {4}}).Flatten().ToIntSlice()
fmt.Println("err:", err)
fmt.Println("res:", res)
// output:
// err:
// res: [1 2 2 3 4]
}
```
### Reverse
```go
func ExampleObject_Reverse() {
// Reverse list
res, err := lambda.New([]int{1, 2, 3, 4}).Reverse().ToIntSlice()
fmt.Println("err:", err)
fmt.Println("res:", res)
// output:
// err:
// res: [4 3 2 1]
}
```
### Uniq
```go
func ExampleObject_Uniq() {
// Remove duplicate elements in the list
res, err := lambda.New([]int{1, 2, 1, 3, 2, 3, 4}).Uniq().ToIntSlice()
fmt.Println("err:", err)
fmt.Println("res:", res)
// output:
// err:
// res: [1 2 3 4]
}
```
### MapArray
```go
func ExampleObject_MapList() {
// Traverse the elements of the list, and after each element is processed, the returned elements form a new list
res, err := lambda.New([]int{1, 2, 3}).MapArray(func(idx int, obj interface{}) interface{} {
return obj.(int) + 1
}).ToIntSlice()
fmt.Println("err:", err)
fmt.Println("res:", res)
// output:
// err:
// res: [2 3 4]
}
```
### FilterArray
```go
func ExampleObject_FilterList() {
// Traverse the elements of the list, each element is added to a new list or not, and a new list is returned
res, err := lambda.New([]int{1, 2, 3, 4}).FilterArray(func(idx int, obj interface{}) bool {
return obj.(int)%2 == 0
}).ToIntSlice()
fmt.Println("err:", err)
fmt.Println("res:", res)
// output:
// err:
// res: [2 4]
}
```