lambda

package module
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 26, 2021 License: Apache-2.0 Imports: 7 Imported by: 1

README

go-lambda

codecov go report card test status Apache-2.0 license Go.Dev reference Go project version

Process data like functional programming

Install

go get github.com/chyroc/go-lambda

Usage

New
func ExampleNew() {
	// new with string list
	lambda.New([]string{"1", "2"})

	// new with struct
	lambda.New([]exampleItem{{Name: "Tom"}, {Name: "Harry"}})
}
Chunk
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: <nil>
	// res: [[1 2] [3 4] [5]]
}
Compact
func ExampleObject_Compact() {
	// Remove 0-valued elements from the list
	res, err := lambda.New([]int{0, 1, 2, 1, 0, 2}).Compact().ToIntList()
	fmt.Println("err:", err)
	fmt.Println("res:", res)
	// output:
	// err: <nil>
	// res: [1 2 1 2]
}
Flatten
func ExampleObject_Flatten() {
	// Flatten the list
	res, err := lambda.New([][]int{{1, 2}, {2, 3}, {4}}).Flatten().ToIntList()
	fmt.Println("err:", err)
	fmt.Println("res:", res)
	// output:
	// err: <nil>
	// res: [1 2 2 3 4]
}
Reverse
func ExampleObject_Reverse() {
	// Reverse list
	res, err := lambda.New([]int{1, 2, 3, 4}).Reverse().ToIntList()
	fmt.Println("err:", err)
	fmt.Println("res:", res)
	// output:
	// err: <nil>
	// res: [4 3 2 1]
}
Uniq
func ExampleObject_Uniq() {
	// Remove duplicate elements in the list
	res, err := lambda.New([]int{1, 2, 1, 3, 2, 3, 4}).Uniq().ToIntList()
	fmt.Println("err:", err)
	fmt.Println("res:", res)
	// output:
	// err: <nil>
	// res: [1 2 3 4]
}
MapArray
func ExampleObject_MapArray() {
	// 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
	}).ToIntList()
	fmt.Println("err:", err)
	fmt.Println("res:", res)
	// output:
	// err: <nil>
	// res: [2 3 4]
}
FilterArray
func ExampleObject_FilterArray() {
	// 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
	}).ToIntList()
	fmt.Println("err:", err)
	fmt.Println("res:", res)
	// output:
	// err: <nil>
	// res: [2 4]
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrBreak = fmt.Errorf("break range")

Functions

This section is empty.

Types

type Object

type Object struct {
	// contains filtered or unexported fields
}

func New

func New(obj interface{}) *Object
Example
// new with string list
lambda.New([]string{"1", "2"})

// new with struct
lambda.New([]exampleItem{{Name: "Tom"}, {Name: "Harry"}})

func (*Object) Chunk added in v0.5.0

func (r *Object) Chunk(size int) *Object
Example
// 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: <nil>
res: [[1 2] [3 4] [5]]

func (*Object) Compact added in v0.5.0

func (r *Object) Compact() *Object
Example
// Remove 0-valued elements from the list
res, err := lambda.New([]int{0, 1, 2, 1, 0, 2}).Compact().ToIntList()
fmt.Println("err:", err)
fmt.Println("res:", res)
Output:

err: <nil>
res: [1 2 1 2]

func (*Object) EachArray added in v0.5.0

func (r *Object) EachArray(f func(idx int, obj interface{})) error

func (*Object) Error

func (r *Object) Error() error

func (*Object) FilterArray added in v0.5.0

func (r *Object) FilterArray(f func(idx int, obj interface{}) bool) *Object
Example
// 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
}).ToIntList()
fmt.Println("err:", err)
fmt.Println("res:", res)
Output:

err: <nil>
res: [2 4]

func (*Object) Flatten added in v0.4.0

func (r *Object) Flatten() *Object
Example
// Flatten the list
res, err := lambda.New([][]int{{1, 2}, {2, 3}, {4}}).Flatten().ToIntList()
fmt.Println("err:", err)
fmt.Println("res:", res)
Output:

err: <nil>
res: [1 2 2 3 4]

func (*Object) GroupByArray added in v0.5.0

func (r *Object) GroupByArray(f func(idx int, obj interface{}) interface{}) *Object

func (*Object) IndexOf added in v0.5.0

func (r *Object) IndexOf(obj interface{}) *Object

func (*Object) Join

func (r *Object) Join(sep string) (string, error)

func (*Object) MapArray added in v0.5.0

func (r *Object) MapArray(f func(idx int, obj interface{}) interface{}) *Object
Example
// 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
}).ToIntList()
fmt.Println("err:", err)
fmt.Println("res:", res)
Output:

err: <nil>
res: [2 3 4]

func (*Object) MapArrayAsync added in v0.6.0

func (r *Object) MapArrayAsync(f func(idx int, obj interface{}) interface{}) *Object

func (*Object) MapArrayAsyncWithErr added in v0.6.0

func (r *Object) MapArrayAsyncWithErr(f func(idx int, obj interface{}) (interface{}, error)) *Object

func (*Object) MapArrayWithErr added in v0.6.0

func (r *Object) MapArrayWithErr(f func(idx int, obj interface{}) (interface{}, error)) *Object

func (*Object) Obj

func (r *Object) Obj() (interface{}, error)

func (*Object) Reverse added in v0.5.0

func (r *Object) Reverse() *Object
Example
// Reverse list
res, err := lambda.New([]int{1, 2, 3, 4}).Reverse().ToIntList()
fmt.Println("err:", err)
fmt.Println("res:", res)
Output:

err: <nil>
res: [4 3 2 1]

func (*Object) String

func (r *Object) String() (res string, err error)

func (*Object) ToBoolList added in v0.5.0

func (r *Object) ToBoolList() (res []bool, err error)

func (*Object) ToInt added in v0.5.0

func (r *Object) ToInt() (res int, err error)

func (*Object) ToIntList added in v0.5.0

func (r *Object) ToIntList() (res []int, err error)

func (*Object) ToIntListList added in v0.5.0

func (r *Object) ToIntListList() (res [][]int, err error)

func (*Object) ToInterfaceList added in v0.5.0

func (r *Object) ToInterfaceList() (res []interface{}, err error)

func (*Object) ToList

func (r *Object) ToList(resp interface{}) (err error)

func (*Object) ToMapInt2Float64List added in v0.5.0

func (r *Object) ToMapInt2Float64List() (res map[int][]float64, err error)

func (*Object) ToStringList added in v0.5.0

func (r *Object) ToStringList() (res []string, err error)

func (*Object) Transfer

func (r *Object) Transfer(f func(obj interface{}) interface{}) *Object

func (*Object) Uniq added in v0.5.0

func (r *Object) Uniq() *Object
Example
// Remove duplicate elements in the list
res, err := lambda.New([]int{1, 2, 1, 3, 2, 3, 4}).Uniq().ToIntList()
fmt.Println("err:", err)
fmt.Println("res:", res)
Output:

err: <nil>
res: [1 2 3 4]

func (*Object) WithErr added in v0.5.0

func (r *Object) WithErr(err error) *Object

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL