Documentation
¶
Index ¶
- Variables
- type Object
- func (r *Object) Chunk(size int) *Object
- func (r *Object) Compact() *Object
- func (r *Object) EachList(f func(idx int, obj interface{})) error
- func (r *Object) FilterList(f func(idx int, obj interface{}) bool) *Object
- func (r *Object) Flatten() *Object
- func (r *Object) GroupByArray(f func(idx int, obj interface{}) interface{}) *Object
- func (r *Object) IndexOf(obj interface{}) *Object
- func (r *Object) MapArrayAsync(f func(idx int, obj interface{}) interface{}) *Object
- func (r *Object) MapArrayAsyncWithErr(f func(idx int, obj interface{}) (interface{}, error)) *Object
- func (r *Object) MapList(f func(idx int, obj interface{}) interface{}) *Object
- func (r *Object) MapListErr(f func(idx int, obj interface{}) (interface{}, error)) *Object
- func (r *Object) Reverse() *Object
- func (r *Object) ToBool() (bool, error)
- func (r *Object) ToBoolArray() (interface{}, error)
- func (r *Object) ToBoolSlice() ([]bool, error)
- func (r *Object) ToComplex128() (complex128, error)
- func (r *Object) ToComplex128Array() (interface{}, error)
- func (r *Object) ToComplex128Slice() ([]complex128, error)
- func (r *Object) ToComplex64() (complex64, error)
- func (r *Object) ToComplex64Array() (interface{}, error)
- func (r *Object) ToComplex64Slice() ([]complex64, error)
- func (r *Object) ToError() error
- func (r *Object) ToExpectType(expectVal interface{}) (interface{}, error)
- func (r *Object) ToFloat32() (float32, error)
- func (r *Object) ToFloat32Array() (interface{}, error)
- func (r *Object) ToFloat32Slice() ([]float32, error)
- func (r *Object) ToFloat64() (float64, error)
- func (r *Object) ToFloat64Array() (interface{}, error)
- func (r *Object) ToFloat64Slice() ([]float64, error)
- func (r *Object) ToInt() (int, error)
- func (r *Object) ToInt16() (int16, error)
- func (r *Object) ToInt16Array() (interface{}, error)
- func (r *Object) ToInt16Slice() ([]int16, error)
- func (r *Object) ToInt32() (int32, error)
- func (r *Object) ToInt32Array() (interface{}, error)
- func (r *Object) ToInt32Slice() ([]int32, error)
- func (r *Object) ToInt64() (int64, error)
- func (r *Object) ToInt64Array() (interface{}, error)
- func (r *Object) ToInt64Slice() ([]int64, error)
- func (r *Object) ToInt8() (int8, error)
- func (r *Object) ToInt8Array() (interface{}, error)
- func (r *Object) ToInt8Slice() ([]int8, error)
- func (r *Object) ToIntArray() (interface{}, error)
- func (r *Object) ToIntSlice() ([]int, error)
- func (r *Object) ToInterfaceSlice() (res []interface{}, err error)
- func (r *Object) ToJoin(sep string) (string, error)
- func (r *Object) ToObj() (interface{}, error)
- func (r *Object) ToObject(resp interface{}) (err error)
- func (r *Object) ToString() (string, error)
- func (r *Object) ToStringArray() (interface{}, error)
- func (r *Object) ToStringSlice() ([]string, error)
- func (r *Object) ToUint() (uint, error)
- func (r *Object) ToUint16() (uint16, error)
- func (r *Object) ToUint16Array() (interface{}, error)
- func (r *Object) ToUint16Slice() ([]uint16, error)
- func (r *Object) ToUint32() (uint32, error)
- func (r *Object) ToUint32Array() (interface{}, error)
- func (r *Object) ToUint32Slice() ([]uint32, error)
- func (r *Object) ToUint64() (uint64, error)
- func (r *Object) ToUint64Array() (interface{}, error)
- func (r *Object) ToUint64Slice() ([]uint64, error)
- func (r *Object) ToUint8() (uint8, error)
- func (r *Object) ToUint8Array() (interface{}, error)
- func (r *Object) ToUint8Slice() ([]uint8, error)
- func (r *Object) ToUintArray() (interface{}, error)
- func (r *Object) ToUintSlice() ([]uint, error)
- func (r *Object) ToUintptr() (uintptr, error)
- func (r *Object) ToUintptrArray() (interface{}, error)
- func (r *Object) ToUintptrSlice() ([]uintptr, error)
- func (r *Object) Transfer(f func(obj interface{}) interface{}) *Object
- func (r *Object) Uniq() *Object
- func (r *Object) WithErr(err error) *Object
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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
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
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
EachList Traverse the array or slice, and use the callback function to process each element
func (*Object) FilterList ¶ added in v0.8.0
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
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 (*Object) MapArrayAsync ¶ added in v0.6.0
func (*Object) MapArrayAsyncWithErr ¶ added in v0.6.0
func (*Object) MapList ¶ added in v0.8.0
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 (*Object) Reverse ¶ added in v0.5.0
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) ToBoolArray ¶ added in v0.7.0
func (*Object) ToBoolSlice ¶ added in v0.7.0
func (*Object) ToComplex128 ¶ added in v0.7.0
func (r *Object) ToComplex128() (complex128, error)
func (*Object) ToComplex128Array ¶ added in v0.7.0
func (*Object) ToComplex128Slice ¶ added in v0.7.0
func (r *Object) ToComplex128Slice() ([]complex128, error)
func (*Object) ToComplex64 ¶ added in v0.7.0
func (*Object) ToComplex64Array ¶ added in v0.7.0
func (*Object) ToComplex64Slice ¶ added in v0.7.0
func (*Object) ToExpectType ¶ added in v0.8.0
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) ToFloat32Array ¶ added in v0.7.0
func (*Object) ToFloat32Slice ¶ added in v0.7.0
func (*Object) ToFloat64Array ¶ added in v0.7.0
func (*Object) ToFloat64Slice ¶ added in v0.7.0
func (*Object) ToInt16Array ¶ added in v0.7.0
func (*Object) ToInt16Slice ¶ added in v0.7.0
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}