lambda

package module
v0.8.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

A simple example
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] <nil>
	// [[0 2 4] [6 8]] <nil>
	// [0 2 4 6 8] <nil>
	// [2 4 6 8] <nil>
	// [{name-2} {name-4} {name-6} {name-8}] <nil>
}
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_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: <nil>
	// res: [2 3 4]
}
FilterArray
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: <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
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] <nil>
[[0 2 4] [6 8]] <nil>
[0 2 4 6 8] <nil>
[2 4 6 8] <nil>
[{name-2} {name-4} {name-6} {name-8}] <nil>

func (*Object) Chunk added in v0.5.0

func (r *Object) Chunk(size int) *Object

Chunk Split the list into shorter length lists

Chunk [1, 2, 3, 4, 5] with length 2, return [[1 2] [3 4] [5]] If the length of the last group is less than length, then the last group will be used as an element alone

Example
// Split the list into shorter length lists
res, err := lambda.New([]int{1, 2, 3, 4, 5}).Chunk(2).ToExpectType([][]int{})
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

Compact Remove 0-valued elements from the list

Compact [0, 1, false, true, "", "str"], will return [1, true, "str"]

Example
// Remove 0-valued elements from the list
res, err := lambda.New([]interface{}{0, 1, false, true, "", "str"}).Compact().ToInterfaceSlice()
fmt.Println("err:", err)
fmt.Println("res:", res)
Output:

err: <nil>
res: [1 true str]

func (*Object) EachList added in v0.8.0

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

EachList Traverse the array or slice, and use the callback function to process each element

func (*Object) FilterList added in v0.8.0

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

FilterList Traverse each element in the array or slice, use the filter callback function to call this element, the new object will return all the elements that make the filter function return true.

Use "is even" to call the filter function, then [1, 2, 3, 4] will return [2 4]

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}).FilterList(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) 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) MapList added in v0.8.0

func (r *Object) MapList(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}).MapList(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) MapListErr added in v0.8.0

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

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) 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) ToError added in v0.8.0

func (r *Object) ToError() error

func (*Object) ToExpectType added in v0.8.0

func (r *Object) ToExpectType(expectVal interface{}) (interface{}, error)
Example
// convert to int16 slice
res, err := lambda.New([]int{1, 2, 3}).MapList(func(idx int, obj interface{}) interface{} {
	return int16(obj.(int))
}).ToExpectType([3]int16{})
fmt.Println("err:", err)
fmt.Printf("res: %#v\n", res)
Output:

err: <nil>
res: [3]int16{1, 2, 3}

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)
Example
// convert to int16 slice
res, err := lambda.New([]int{1, 2, 3}).MapList(func(idx int, obj interface{}) interface{} {
	return int16(obj.(int))
}).ToInt16Slice()
fmt.Println("err:", err)
fmt.Printf("res: %#v\n", res)
Output:

err: <nil>
res: []int16{1, 2, 3}

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) 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) ToJoin added in v0.8.0

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

func (*Object) ToObj added in v0.8.0

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

func (*Object) ToObject added in v0.8.0

func (r *Object) ToObject(resp interface{}) (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) ToUintptr added in v0.8.0

func (r *Object) ToUintptr() (uintptr, error)

func (*Object) ToUintptrArray added in v0.8.0

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

func (*Object) ToUintptrSlice added in v0.8.0

func (r *Object) ToUintptrSlice() ([]uintptr, 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

WithErr Set error to the object

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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