lmd

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2024 License: MIT Imports: 2 Imported by: 0

README

golib

this is a go lib.


type TestA struct {
	Name  string
	Age   int
	IsMan bool
}
type TestAOutput struct {
	Name string
	Age  int
}

func main() {

	//if
	a0:=1
	a1:=2
	fmt.Println(lmd.IF(a0>a1,a0,a1))
	fmt.Println(lmd.IfFunc(a0>a1,func(){fmt.Println(a0)},func(){fmt.Println(a1)}))


	//query
	a := []int{1, 4, 3, 2, 5, 7, 9, 12, 45, 77, 98}
	b := New[int](a).Where(func(e *int) bool { return *e%2 == 0 }).ToList()
	f := New[Test](d).Where(func(e *Test) bool { return strings.Contains(e.Name, "A") }).ToList()


	ds := []TestA{
		{
			Name:  "yjh1",
			Age:   18,
			IsMan: true,
		}, {
			Name:  "yjh2",
			Age:   19,
			IsMan: false,
		}, {
			Name:  "yjh3",
			Age:   20,
			IsMan: true,
		},
	}

	ds1 := list.New2[TestA, string](ds).Where(func(e *TestA) bool { return e.Age > 18 }).Select(func(e *TestA) string { return e.Name })
	fmt.Println(ds1)

	ds2 := list.New2[TestA, TestAOutput](ds).Where(func(e *TestA) bool { return e.IsMan }).Select(func(e *TestA) TestAOutput {
		return TestAOutput{
			e.Name, e.Age,
		}
	})
	fmt.Println(ds2)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Each

func Each[T any](t []T, fun func(t *T))

list foreach no break

func EachIndex

func EachIndex[T any](t []T, fun func(t *T))

list foreach no break

func Exists

func Exists[T any](ts []T, fun func(t *T) bool) bool

list exists

func Find

func Find[T any](ts []T, fun func(t *T) bool) *T

list find one

func Foreach

func Foreach[T any](t []T, fun func(t *T) bool)

list foreach with break

func ForeachIndex

func ForeachIndex[T any](t []T, fun func(t *T, i int) bool)

list foreach with break

func IF

func IF[T any](con bool, t1 T, t2 T) T

if example IF(a>b,a,b)

func IfFunc

func IfFunc(con bool, fun1 func(), fun2 func())

if fun

func IfFuncT

func IfFuncT[T any](con bool, fun1 func() T, fun2 func() T) T

if fun example e:=IfFunc(a>b,func(){return c;},func(){return d})

func In

func In[T InT](vs []T, v T) bool

list contains

func Join

func Join[T any](ts []T, fun func(t *T) (bool, string), sep string) string

func JoinSkipEmpty

func JoinSkipEmpty[T any](ts []T, fun func(t *T) (bool, string), sep string) string

func NotIn

func NotIn[T InT](vs []T, v T) bool

notin

func OrderBy

func OrderBy[T any](vs []T, fun func(t1 *T, t2 *T) bool) []T

order by

func Search[T any](t []T, fun func(t *T) bool) []T

list search

func SearchIndex

func SearchIndex[T any](t []T, fun func(t *T) bool) []int

list search index

func SearchStr

func SearchStr[T any](t []T, fun func(t *T) (bool, string)) []string

list search str

func SearchStrSkipEmpty

func SearchStrSkipEmpty[T any](t []T, fun func(t *T) (bool, string)) []string

list search str with skip empty string

func Select

func Select[T any, M any](d []T, fun func(e *T) M) []M

from []T to []M

func Walk

func Walk[T any](t []T, fun func(t *T) error) error

list walk with break

func WalkIndex

func WalkIndex[T any](t []T, fun func(t *T, i int) error) error

list walk with break

Types

type IQueryable

type IQueryable[T any] interface {
	Where(fun func(e *T) bool) IQueryable[T]
	ToList() []T
	OrderBy(fun func(a *T, b *T) bool) IQueryable[T]
	FirstOrDefault() *T
	LastOrDefault() *T
	Concat(t []T) IQueryable[T]
	Take(count int) []T

	SelectString(fun func(e *T) string) []string
	SelectInt(fun func(e *T) int) []int
	SelectInt16(fun func(e *T) int16) []int16
	SelectInt32(fun func(e *T) int32) []int32
	SelectInt64(fun func(e *T) int64) []int64
	SelectAny(fun func(e *T) any) []any
}

func New

func New[T any](d []T) IQueryable[T]

type IQueryable2

type IQueryable2[T any, M any] interface {
	Where(fun func(e *T) bool) IQueryable2[T, M]
	ToList() []T
	OrderBy(fun func(a *T, b *T) bool) IQueryable2[T, M]
	FirstOrDefault() *T
	LastOrDefault() *T
	Concat(t []T) IQueryable2[T, M]
	Take(count int) []T
	SelectString(fun func(e *T) string) []string
	SelectInt(fun func(e *T) int) []int
	SelectInt16(fun func(e *T) int16) []int16
	SelectInt32(fun func(e *T) int32) []int32
	SelectInt64(fun func(e *T) int64) []int64
	Select(fun func(e *T) M) []M
}

func New2

func New2[T any, M any](d []T) IQueryable2[T, M]

type InT

type InT interface {
	~int | ~int32 | ~int64 | ~float32 | ~float64 | ~string | ~int8 | ~int16 | ~byte | ~bool
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳