lambda

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 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().ToIntSlice()
	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().ToIntSlice()
	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().ToIntSlice()
	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().ToIntSlice()
	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
	}).ToIntSlice()
	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
	}).ToIntSlice()
	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).ToIntListSlice()
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().ToIntSlice()
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
}).ToIntSlice()
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().ToIntSlice()
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
}).ToIntSlice()
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().ToIntSlice()
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) ToBool added in v0.7.0

func (r *Object) ToBool() (bool, error)

func (*Object) ToBoolArray added in v0.7.0

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

func (*Object) ToBoolSlice added in v0.7.0

func (r *Object) ToBoolSlice() ([]bool, error)

func (*Object) ToComplex128 added in v0.7.0

func (r *Object) ToComplex128() (complex128, error)

func (*Object) ToComplex128Array added in v0.7.0

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

func (*Object) ToComplex128Slice added in v0.7.0

func (r *Object) ToComplex128Slice() ([]complex128, error)

func (*Object) ToComplex64 added in v0.7.0

func (r *Object) ToComplex64() (complex64, error)

func (*Object) ToComplex64Array added in v0.7.0

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

func (*Object) ToComplex64Slice added in v0.7.0

func (r *Object) ToComplex64Slice() ([]complex64, error)

func (*Object) ToFloat32 added in v0.7.0

func (r *Object) ToFloat32() (float32, error)

func (*Object) ToFloat32Array added in v0.7.0

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

func (*Object) ToFloat32Slice added in v0.7.0

func (r *Object) ToFloat32Slice() ([]float32, error)

func (*Object) ToFloat64 added in v0.7.0

func (r *Object) ToFloat64() (float64, error)

func (*Object) ToFloat64Array added in v0.7.0

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

func (*Object) ToFloat64Slice added in v0.7.0

func (r *Object) ToFloat64Slice() ([]float64, error)

func (*Object) ToInt added in v0.5.0

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

func (*Object) ToInt16 added in v0.7.0

func (r *Object) ToInt16() (int16, error)

func (*Object) ToInt16Array added in v0.7.0

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

func (*Object) ToInt16Slice added in v0.7.0

func (r *Object) ToInt16Slice() ([]int16, error)

func (*Object) ToInt32 added in v0.7.0

func (r *Object) ToInt32() (int32, error)

func (*Object) ToInt32Array added in v0.7.0

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

func (*Object) ToInt32Slice added in v0.7.0

func (r *Object) ToInt32Slice() ([]int32, error)

func (*Object) ToInt64 added in v0.7.0

func (r *Object) ToInt64() (int64, error)

func (*Object) ToInt64Array added in v0.7.0

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

func (*Object) ToInt64Slice added in v0.7.0

func (r *Object) ToInt64Slice() ([]int64, error)

func (*Object) ToInt8 added in v0.7.0

func (r *Object) ToInt8() (int8, error)

func (*Object) ToInt8Array added in v0.7.0

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

func (*Object) ToInt8Slice added in v0.7.0

func (r *Object) ToInt8Slice() ([]int8, error)

func (*Object) ToIntArray added in v0.7.0

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

func (*Object) ToIntListSlice added in v0.7.0

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

func (*Object) ToIntSlice added in v0.7.0

func (r *Object) ToIntSlice() ([]int, error)

func (*Object) ToInterfaceSlice added in v0.7.0

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

func (*Object) ToList

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

func (*Object) ToMapInt2Float64Slice added in v0.7.0

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

func (*Object) ToString added in v0.7.0

func (r *Object) ToString() (string, error)

func (*Object) ToStringArray added in v0.7.0

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

func (*Object) ToStringSlice added in v0.7.0

func (r *Object) ToStringSlice() ([]string, error)

func (*Object) ToUint added in v0.7.0

func (r *Object) ToUint() (uint, error)

func (*Object) ToUint16 added in v0.7.0

func (r *Object) ToUint16() (uint16, error)

func (*Object) ToUint16Array added in v0.7.0

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

func (*Object) ToUint16Slice added in v0.7.0

func (r *Object) ToUint16Slice() ([]uint16, error)

func (*Object) ToUint32 added in v0.7.0

func (r *Object) ToUint32() (uint32, error)

func (*Object) ToUint32Array added in v0.7.0

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

func (*Object) ToUint32Slice added in v0.7.0

func (r *Object) ToUint32Slice() ([]uint32, error)

func (*Object) ToUint64 added in v0.7.0

func (r *Object) ToUint64() (uint64, error)

func (*Object) ToUint64Array added in v0.7.0

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

func (*Object) ToUint64Slice added in v0.7.0

func (r *Object) ToUint64Slice() ([]uint64, error)

func (*Object) ToUint8 added in v0.7.0

func (r *Object) ToUint8() (uint8, error)

func (*Object) ToUint8Array added in v0.7.0

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

func (*Object) ToUint8Slice added in v0.7.0

func (r *Object) ToUint8Slice() ([]uint8, error)

func (*Object) ToUintArray added in v0.7.0

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

func (*Object) ToUintSlice added in v0.7.0

func (r *Object) ToUintSlice() ([]uint, 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().ToIntSlice()
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