1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-12 00:32:27 +08:00

feat: add func ContainSubSlice

This commit is contained in:
dudaodong
2022-01-13 11:00:27 +08:00
parent fd271fe176
commit e31fb28003
4 changed files with 40 additions and 6 deletions

View File

@@ -17,7 +17,6 @@ import (
// Contain check if the value is in the iterable type or not
func Contain(iterableType interface{}, value interface{}) bool {
v := reflect.ValueOf(iterableType)
switch kind := reflect.TypeOf(iterableType).Kind(); kind {
@@ -47,6 +46,30 @@ func Contain(iterableType interface{}, value interface{}) bool {
return false
}
// ContainSubSlice check if the slice contain subslice or not
func ContainSubSlice(slice interface{}, subslice interface{}) bool {
super := sliceValue(slice)
sub := sliceValue(subslice)
if super.Type().Elem().Kind() != sub.Type().Elem().Kind() {
return false
}
unique := make(map[interface{}]bool)
for i := 0; i < super.Len(); i++ {
v := super.Index(i).Interface()
unique[v] = true
}
for i := 0; i < sub.Len(); i++ {
v := sub.Index(i).Interface()
if !unique[v] {
return false
}
}
return true
}
// Chunk creates an slice of elements split into groups the length of `size`.
func Chunk(slice []interface{}, size int) [][]interface{} {
var res [][]interface{}