File: subset.go

package info (click to toggle)
golang-github-thoas-go-funk 0.9.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 504 kB
  • sloc: makefile: 10
file content (41 lines) | stat: -rw-r--r-- 743 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package funk

import (
	"reflect"
)

// Subset returns true if collection x is a subset of y.
func Subset(x interface{}, y interface{}) bool {
	if !IsCollection(x) {
		panic("First parameter must be a collection")
	}
	if !IsCollection(y) {
		panic("Second parameter must be a collection")
	}

	xValue := reflect.ValueOf(x)
	xType := xValue.Type()

	yValue := reflect.ValueOf(y)
	yType := yValue.Type()

	if NotEqual(xType, yType) {
		panic("Parameters must have the same type")
	}

	if xValue.Len() == 0 {
		return true
	}

	if yValue.Len() == 0 || yValue.Len() < xValue.Len() {
		return false
	}

	for i := 0; i < xValue.Len(); i++ {
		if !Contains(yValue.Interface(), xValue.Index(i).Interface()) {
			return false
		}
	}

    return true
}