los

package
v1.0.54 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2024 License: MIT Imports: 6 Imported by: 13

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Arrange added in v1.0.28

func Arrange[T any, O constraints.Ordered](elems []T, getKey func(T) O) []T

Arrange 整理成员执行顺序,key相等的避免同时执行.

Example

ExampleArrange is an example function.

package main

import (
	"fmt"

	"github.com/xuender/kit/los"
)

func main() {
	fmt.Println(los.Arrange(
		[]int{1, 1, 2, 2, 2, 3},
		func(num int) int { return num }),
	)

}
Output:

[2 1 3 2 1 2]

func Delete added in v1.0.54

func Delete[S ~[]E, E any](slice S, idxs ...int) S
Example
package main

import (
	"fmt"

	"github.com/xuender/kit/los"
)

func main() {
	fmt.Println(los.Delete([]int{0, 1, 2, 3, 4}))
	fmt.Println(los.Delete([]int{0, 1, 2, 3, 4}, 1))
	fmt.Println(los.Delete([]int{0, 1, 2, 3, 4}, 3, 1))
	fmt.Println(los.Delete([]int{0, 1, 2, 3, 4}, 3, 4))
	fmt.Println(los.Delete([]int{0, 1, 2, 3, 4}, 2, 3))
	fmt.Println(los.Delete([]int{0, 1, 2, 3, 4}, 0, 1))
	fmt.Println(los.Delete([]int{0, 1, 2, 3, 4}, 0, 1, 2, 3, 4))

}
Output:

[0 1 2 3 4]
[0 2 3 4]
[0 2 4]
[0 1 2]
[0 1 4]
[2 3 4]
[]

func DeleteBy added in v1.0.54

func DeleteBy[S ~[]E, E comparable](slice S, elems ...E) S
Example
package main

import (
	"fmt"

	"github.com/xuender/kit/los"
)

func main() {
	fmt.Println(los.DeleteBy([]int{0, 1, 2, 3, 4}, 1))

}
Output:

[0 2 3 4]

func DeleteFunc added in v1.0.54

func DeleteFunc[S ~[]E, E any](slice S, del func(E) bool) S
Example
package main

import (
	"fmt"

	"github.com/xuender/kit/los"
)

func main() {
	fmt.Println(los.DeleteFunc([]int{0, 1, 2, 3, 4}, func(num int) bool { return num == 1 }))

}
Output:

[0 2 3 4]

func EveryNil added in v1.0.11

func EveryNil(elems ...any) bool
Example
package main

import (
	"fmt"

	"github.com/xuender/kit/los"
)

func main() {
	fmt.Println(los.EveryNil(nil, nil))
	fmt.Println(los.EveryNil(nil, 1))

}
Output:

true
false

func Filter added in v1.0.54

func Filter[S ~[]V, V any](slice S, predicate func(item V) bool) S
Example
package main

import (
	"fmt"

	"github.com/xuender/kit/los"
)

func main() {
	fmt.Println(los.Filter([]int{1, 2, 3, 4}, func(num int) bool { return num%2 == 0 }))

}
Output:

[2 4]

func IndexAll added in v1.0.54

func IndexAll[S ~[]E, E comparable](slice S, elem E) []int
Example
package main

import (
	"fmt"

	"github.com/xuender/kit/los"
)

func main() {
	fmt.Println(los.IndexAll([]int{1, 2, 3, 4, 3, 2, 1}, 1))
	fmt.Println(los.IndexAll([]int{1, 2, 3, 4, 3, 2, 1}, 2))
	fmt.Println(los.IndexAll([]int{1, 2, 3, 4, 3, 2, 1}, 4))
	fmt.Println(los.IndexAll([]int{1, 2, 3, 4, 3, 2, 1}, 5))

}
Output:

[0 6]
[1 5]
[3]
[]

func IndexAllFunc added in v1.0.54

func IndexAllFunc[S ~[]E, E any](slice S, predicate func(E) bool) []int
Example
package main

import (
	"fmt"

	"github.com/xuender/kit/los"
)

func main() {
	fmt.Println(los.IndexAllFunc([]int{1, 2, 3, 4, 3, 2, 1}, func(num int) bool { return num == 1 }))
	fmt.Println(los.IndexAllFunc([]int{1, 2, 3, 4, 3, 2, 1}, func(num int) bool { return num == 2 }))
	fmt.Println(los.IndexAllFunc([]int{1, 2, 3, 4, 3, 2, 1}, func(num int) bool { return num == 4 }))
	fmt.Println(los.IndexAllFunc([]int{1, 2, 3, 4, 3, 2, 1}, func(num int) bool { return num == 5 }))

}
Output:

[0 6]
[1 5]
[3]
[]

func IndexOf added in v1.0.31

func IndexOf[T comparable](slice, sub []T) int
Example
package main

import (
	"fmt"

	"github.com/xuender/kit/los"
)

func main() {
	slice := []int{1, 2, 1, 2}

	fmt.Println(los.IndexOf(slice, []int{2, 1}))
	fmt.Println(los.IndexOf(slice, []int{1, 2, 1}))
	fmt.Println(los.IndexOf(slice, []int{2, 1, 1}))
	fmt.Println(los.IndexOf(slice, []int{1, 2, 1, 2, 1}))
	fmt.Println(los.IndexOf(slice, []int{1, 2}))

}
Output:

1
0
-1
-1
0

func LastIndexOf added in v1.0.31

func LastIndexOf[T comparable](slice, sub []T) int
Example
package main

import (
	"fmt"

	"github.com/xuender/kit/los"
)

func main() {
	slice := []int{1, 2, 1, 2}

	fmt.Println(los.LastIndexOf(slice, []int{2, 1}))
	fmt.Println(los.LastIndexOf(slice, []int{1, 2, 1}))
	fmt.Println(los.LastIndexOf(slice, []int{2, 1, 1}))
	fmt.Println(los.LastIndexOf(slice, []int{1, 2, 1, 2, 1}))
	fmt.Println(los.LastIndexOf(slice, []int{1, 2}))

}
Output:

1
0
-1
-1
2

func Map added in v1.0.51

func Map[S ~[]T, T any, R any](slice S, iteratee func(item T) R) []R
Example
package main

import (
	"fmt"

	"github.com/xuender/kit/los"
)

func main() {
	fmt.Println(los.Map([]int{1, 2, 3, 4}, func(item int) int { return item * 2 }))

}
Output:

[2 4 6 8]

func Must added in v1.0.39

func Must[T any](val T, err error, messageArgs ...any) T

func Must0 added in v1.0.39

func Must0(err error, messageArgs ...any)

func Must1 added in v1.0.39

func Must1[T any](val T, err error, messageArgs ...any) T

func Must2 added in v1.0.39

func Must2[T1, T2 any](val1 T1, val2 T2, err error, messageArgs ...any) (T1, T2)

func Must3 added in v1.0.39

func Must3[T1, T2, T3 any](val1 T1, val2 T2, val3 T3, err error, messageArgs ...any) (T1, T2, T3)

func Must4 added in v1.0.39

func Must4[T1, T2, T3, T4 any](val1 T1, val2 T2, val3 T3, val4 T4, err error, messageArgs ...any) (T1, T2, T3, T4)

func Must5 added in v1.0.39

func Must5[T1, T2, T3, T4, T5 any](val1 T1, val2 T2, val3 T3, val4 T4, val5 T5,
	err error, messageArgs ...any,
) (T1, T2, T3, T4, T5)

func Must6 added in v1.0.39

func Must6[T1, T2, T3, T4, T5, T6 any](val1 T1, val2 T2, val3 T3, val4 T4, val5 T5, val6 T6,
	err error, messageArgs ...any,
) (T1, T2, T3, T4, T5, T6)

func Range added in v1.0.54

func Range[T constraints.Integer](elementNum uint) []T
Example
package main

import (
	"fmt"

	"github.com/xuender/kit/los"
)

func main() {
	fmt.Println(los.Range[uint32](3))

}
Output:

[0 1 2]

func SampleBool added in v1.0.27

func SampleBool() bool
Example
package main

import (
	"fmt"
	"math/rand"

	"github.com/xuender/kit/los"
)

func main() {
	rand.Seed(3)
	fmt.Println(los.SampleBool())
	fmt.Println(los.SampleBool())

}
Output:

false
true

func SomeNil added in v1.0.11

func SomeNil(elems ...any) bool
Example
package main

import (
	"fmt"

	"github.com/xuender/kit/los"
)

func main() {
	fmt.Println(los.SomeNil(nil, 1))
	fmt.Println(los.SomeNil(1, 2))

}
Output:

true
false

func Split

func Split[T comparable](collection []T, isSeparator func(T, int) bool) [][]T

Split 切片分解.

Example

ExampleSplit is an example function.

package main

import (
	"fmt"

	"github.com/xuender/kit/los"
)

func main() {
	fmt.Println(los.Split(
		[]int{1, 0, 2, 3, 0, 4, 0, 5},
		func(num, _ int) bool { return num == 0 },
	))

}
Output:

[[1] [2 3] [4] [5]]

func SplitStr

func SplitStr(str string, separators ...rune) []string

SplitStr 字符串根据分隔符分解.

Example

ExampleSplitStr is an example function.

package main

import (
	"fmt"

	"github.com/xuender/kit/los"
)

func main() {
	fmt.Println(los.SplitStr("123,456", ','))
	fmt.Println(los.SplitStr("a-b_c", '-', '_'))

}
Output:

[123 456]
[a b c]

Types

type Sunday added in v1.0.35

type Sunday[C comparable] struct {
	// contains filtered or unexported fields
}

Sunday 算法数组查找.

func NewSunday added in v1.0.35

func NewSunday[C comparable](sub []C) *Sunday[C]

NewSunday 新建 Sunday 算法.

func (Sunday[C]) IndexOf added in v1.0.35

func (p Sunday[C]) IndexOf(slice []C) int

IndexOf 查找位置.

Example
package main

import (
	"fmt"

	"github.com/xuender/kit/los"
)

func main() {
	sub := []rune("abcab")
	sunday := los.NewSunday(sub)
	fmt.Println(sunday.IndexOf([]rune("abcaabcab")))

}
Output:

4

Jump to

Keyboard shortcuts

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