lambda

package
v1.15.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2023 License: Apache-2.0, MIT Imports: 5 Imported by: 0

README

lambda

overview

lambda is a lambda expression for go,lets you extract elements from array by lambda expression

Installation

go get github.com/favar/lambda

Getting Started

use LambdaArray returns Array interface
sa := []int{1,2,3,4,5,6,7,8,9}
arr := LambdaArray(sa) // return Array<int>
interface Array
type Array interface {
	IsSlice() bool
	Join(options JoinOptions) string
	Filter(express interface{}) Array
	Sort(express interface{}) Array
	SortMT(express interface{}) Array
	Map(express interface{}) Array
	Append(elements ...interface{}) Array
	Max(express interface{}) interface{}
	Min(express interface{}) interface{}
	Any(express interface{}) bool
	All(express interface{}) bool
	Count(express interface{}) int
	First(express interface{}) (interface{}, error)
	Last(express interface{}) (interface{}, error)
	index(i int) (interface{}, error)
	Take(skip, count int) Array
	Sum(express interface{}) interface{}
	Average(express interface{}) float64
	Contains(express interface{}) bool
	Pointer() interface{}
}

Usage

define test struct

type user struct {
	name string
	age  int
}
Join

array join into string

type JoinOptions struct {
	Symbol  string // split string,default `,`
    express interface{} // express match func(ele TElement) string
}
Join(options JoinOptions) string
arr := []int{1,2,3,4,5}
str1 := LambdaArray(arr).Join(JoinOptions{
	express: func(e int) string { return strconv.Itoa(e) },
})
fmt.Println(str1) // 1,2,3,4,5 default `,`

str2 := LambdaArray(arr).Join(JoinOptions{
    express: func(e int) string { return strconv.Itoa(e) },
    Symbol:  "|",
})
fmt.Println(str2) // 1|2|3|4|5

 
Filter

array filter

Filter(express interface{}) Array // express match func(ele TElement) bool
arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
larr := LambdaArray(arr)
ret1 := larr.Filter(func(ele int) bool { return ele > 5 }).Pointer().([]int)
fmt.Println(ret1) // [6 7 8 9 10]

ret2 := larr.Filter(func(ele int) bool { return ele%2 == 0 }).Pointer().([]int)
fmt.Println(ret2) // [2 4 6 8 10]

ret3 := LambdaArray(users).Filter(func(u user) bool { return u.age < 30 }).Pointer().([]user)
fmt.Println(ret3) // [{Abraham 20} {Edith 25} {Anthony 26}]
Sort

quick sort

Sort(express interface{}) Array // express match func(e1, e2 TElement) bool
arr := []int{1, 3, 8, 6, 12, 5, 9}
// order by asc
ret1 := LambdaArray(arr).Sort(func(a, b int) bool { return a < b }).Pointer().([]int)
// order by desc
ret2 := LambdaArray(arr).Sort(func(a, b int) bool { return a > b }).Pointer().([]int)

fmt.Println(ret1) // [1 3 5 6 8 9 12]
fmt.Println(ret2) // [12 9 8 6 5 3 1]

users := []user{
    {"Abraham", 20},
    {"Edith", 25},
    {"Charles", 40},
    {"Anthony", 26},
    {"Abel", 33},
}
// order by user.age asc
ret3 := LambdaArray(users).Sort(func(a, b user) bool { return a.age < b.age }).Pointer().([]user)
fmt.Println(ret3) // [{Abraham 20} {Edith 25} {Anthony 26} {Abel 33} {Charles 40}]
SortMT

sort by quick multithreading

usage like Sort

Map

.map to new array

Map(express interface{}) Array // express match func(ele TElement) TOut
arr := LambdaArray([]int{1, 2, 3, 4, 5})
users := arr.Map(func(i int) user {
    return user{name: "un:" + strconv.Itoa(i), age: i}
}).Pointer().([]user)
fmt.Println(users) // [{un:1 1} {un:2 2} {un:3 3} {un:4 4} {un:5 5}]

Append

.append element

Append(elements ...interface{}) Array // each of elements type must be TElmenent
arr := LambdaArray([]int{1, 2, 3})
arr.Append(4)
fmt.Println(arr.Pointer().([]int)) // [1 2 3 4]
arr.Append(5, 6)
fmt.Println(arr.Pointer().([]int)) // [1 2 3 4 5 6]
Max

.maximum element of array

Max(express interface{}) interface{}
users := []user{
    {"Abraham", 20},
    {"Edith", 25},
    {"Charles", 40},
    {"Anthony", 26},
    {"Abel", 33},
}
eldest := LambdaArray(users).Max(func(u user) int { return u.age }).(user)
fmt.Println(eldest.name + " is the eldest") // Charles is the eldest

want := []int{1, 5, 6, 3, 8, 9, 3, 12, 56, 186, 4, 9, 14}
var iArr = LambdaArray(want)
ret := iArr.Max(nil).(int)
fmt.Println(ret) // 186
Min

.minimum element of array

Min(express interface{}) interface{}
users := []user{
    {"Abraham", 20},
    {"Edith", 25},
    {"Charles", 40},
    {"Anthony", 26},
    {"Abel", 33},
}
eldest := LambdaArray(users).Min(func(u user) int { return u.age }).(user)
fmt.Println(eldest.name + " is the eldest") // Abraham is the Charles

want := []int{1, 5, 6, 3, 8, 9, 3, 12, 56, 186, 4, 9, 14}
var iArr = LambdaArray(want)
ret := iArr.Min(nil).(int)
fmt.Println(ret) // 1
Any

.Determines whether the Array contains any elements

Any(express interface{}) bool
us := []user{
   {"Abraham", 20},
   {"Edith", 25},
   {"Charles", 40},
   {"Anthony", 26},
   {"Abel", 33},
}
ret1 := LambdaArray(us).Any(func(u user) bool { return u.age > 30 })
fmt.Println(ret1) // true
ret2 := LambdaArray(us).Any(func(u user) bool { return u.age < 0 })
fmt.Println(ret2) // false
All

Determines whether the condition is satisfied for all elements in the Array

All(express interface{}) bool
us := []user{
   {"Abraham", 20},
   {"Edith", 25},
   {"Charles", 40},
   {"Anthony", 26},
   {"Abel", 33},
}
ret1 := LambdaArray(us).All(func(u user) bool { return u.age > 30 })
fmt.Println(ret1) // false
ret2 := LambdaArray(us).All(func(u user) bool { return u.age > 10 })
fmt.Println(ret2) // true
Count

Returns a number indicating how many elements in the specified Array satisfy the condition

Count(express interface{}) int
us := []user{
    {"Abraham", 20},
    {"Edith", 25},
    {"Charles", 40},
    {"Anthony", 26},
    {"Abel", 33},
}
ret1 := LambdaArray(us).Count(func(u user) bool { return u.age > 30 })
fmt.Println(ret1) // 2
ret2 := LambdaArray(us).Count(func(u user) bool { return u.age > 20 })
fmt.Println(ret2) // 4
First

Returns the first element of an Array that satisfies the condition

First(express interface{}) (interface{}, error)
us := []user{
    {"Abraham", 20},
    {"Edith", 25},
    {"Charles", 40},
    {"Anthony", 26},
    {"Abel", 33},
}
arr := LambdaArray(us)
if u, err := arr.First(func(u user) bool { return u.name == "Charles" }); err == nil {
    fmt.Println(u, " found")
} else {
    fmt.Println("not found")
}
// {Charles 40}  found
if u, err := arr.First(func(u user) bool { return u.name == "jack" }); err == nil {
    fmt.Println(u, " found")
} else {
    fmt.Println("not found")
}
// not found

Last

Returns the last element of an Array that satisfies the condition

Last(express interface{}) (interface{}, error)
us := []user{
    {"Abraham", 20},
    {"Edith", 25},
    {"Charles", 40},
    {"Anthony", 26},
    {"Abel", 33},
}
arr := LambdaArray(us)
if u, err := arr.Last(func(u user) bool { return u.name == "Anthony" }); err == nil {
    fmt.Println(u, " found")
} else {
    fmt.Println("not found")
}
// {Anthony 26}  found
if u, err := arr.Last(func(u user) bool { return u.age > 35 }); err == nil {
    fmt.Println(u, " found")
} else {
    fmt.Println("not found")
}
// {Charles 40}  found
Index

Returns the zero based index of the first occurrence in an Array

Index(i int) (interface{}, error)
if element, err := LambdaArray([]int{1, 2, 3, 4, 5}).Index(3); err == nil {
    fmt.Println(element)
} else {
    fmt.Println(err)
}
// 4
if element, err := LambdaArray([]int{1, 2, 3, 4, 5}).Index(10); err == nil {
    fmt.Println(element)
} else {
    fmt.Println(err)
}
// 10 out of range
Take

take count elements start by skip

Take(skip, count int) Array
ret1 := LambdaArray([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}).Take(4, 10).Pointer().([]int)
fmt.Println(ret1) // [5 6 7 8 9 10]
ret2 := LambdaArray([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}).Take(10, 10).Pointer().([]int)
fmt.Println(ret2) // []
Sum

sum of the values returned by the expression

Sum(express interface{}) interface{}
us := []user{
    {"Abraham", 20},
    {"Edith", 25},
    {"Charles", 40},
    {"Anthony", 26},
    {"Abel", 33},
}
arr := LambdaArray(us)
fmt.Println("total user age is ", arr.Sum(func(u user) int { return u.age }))
// total user age is 144
Average

average of the values returned by the expression

Average(express interface{}) float64
us := []user{
    {"Abraham", 20},
    {"Edith", 25},
    {"Charles", 40},
    {"Anthony", 26},
    {"Abel", 33},
}
arr := LambdaArray(us)
fmt.Println("all user average age is", arr.Average(func(u user) int { return u.age }))
// all user average age is 28.8
Contains

Determines whether the array contains the specified element

Contains(express interface{}) bool
us := []user{
    {"Abraham", 20},
    {"Edith", 25},
    {"Charles", 40},
    {"Anthony", 26},
    {"Abel", 33},
}
arr2 := LambdaArray(us)
fmt.Println(arr2.Contains(func(u user) bool { return u.age > 25 })) //true

fmt.Println(LambdaArray([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}).Contains(9)) // true
fmt.Println(LambdaArray([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}).Contains(0)) // false
Pointer

array or slice pointer

Pointer() interface{}

Tutorial

Usage

Questions

Please let me know if you have any questions.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Add

type Add interface {
	Add(value reflect.Value)
	Value() interface{}
	SetZero()
}

func Adder

func Adder(t reflect.Type) Add

type Array

type Array interface {

	// return true when obj is slice
	IsSlice() bool

	// array join into string
	// eg
	// JoinOptions.express func(u user) string {return u.name }
	// JoinOptions.Symbol default `,`
	Join(options JoinOptions) string

	// array filter
	// eg: arr.Filter(func(ele int) bool{ return ele>10})
	Filter(express interface{}) Array

	// sort by quick
	// eg
	Sort(express interface{}) Array

	// sort by quick multithreading
	SortMT(express interface{}) Array

	// map to new array
	// express func(el T) T{ return T }
	Map(express interface{}) Array

	// append element
	Append(elements ...interface{}) Array

	// maximum of array
	// express eg: express func(ele TIn) TOut{ return TOut },TOut must be number Type or Compare
	Max(express interface{}) interface{}

	// minimum of array
	// express eg: express func(ele TIn) TOut{ return TOut },TOut must be number Type or Compare
	Min(express interface{}) interface{}

	// Determines whether the Array contains any elements
	Any(express interface{}) bool

	// Determines whether the condition is satisfied for all elements in the Array
	All(express interface{}) bool

	// Returns a number indicating how many elements in the specified Array satisfy the condition
	Count(express interface{}) int

	// Returns the first element of an Array that satisfies the condition
	First(express interface{}) (interface{}, error)

	// Returns the last element of an Array that satisfies the condition
	Last(express interface{}) (interface{}, error)

	// Returns the zero based index of the first occurrence in an Array
	Index(i int) (interface{}, error)

	// skip and Returns the elements
	Take(skip, count int) Array

	// sum of the values returned by the expression
	Sum(express interface{}) interface{}

	// average of the values returned by the expression
	Average(express interface{}) float64

	// Determines whether the array contains the specified element
	// number type use default comparator
	// other type can implements Compare
	Contains(express interface{}) bool

	// array or slice pointer
	// Array.Pointer().([]T or [n]T)
	Pointer() interface{}
}

func LambdaArray

func LambdaArray(source interface{}) Array

make Array from source(TIn[] type) source support array or slice type

type BasicCompare

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

func (*BasicCompare) CompareTo

func (p *BasicCompare) CompareTo(a interface{}) int

type Compare

type Compare interface {
	CompareTo(a interface{}) int
}

func BasicComparator

func BasicComparator(ele interface{}) (Compare, error)

type Equal

type Equal interface {
	Equals(obj interface{}) bool
}

type JoinOptions

type JoinOptions struct {
	Symbol string
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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