...

Source file src/github.com/thoas/go-funk/subset.go

Documentation: github.com/thoas/go-funk

     1  package funk
     2  
     3  import (
     4  	"reflect"
     5  )
     6  
     7  // Subset returns true if collection x is a subset of y.
     8  func Subset(x interface{}, y interface{}) bool {
     9  	if !IsCollection(x) {
    10  		panic("First parameter must be a collection")
    11  	}
    12  	if !IsCollection(y) {
    13  		panic("Second parameter must be a collection")
    14  	}
    15  
    16  	xValue := reflect.ValueOf(x)
    17  	xType := xValue.Type()
    18  
    19  	yValue := reflect.ValueOf(y)
    20  	yType := yValue.Type()
    21  
    22  	if NotEqual(xType, yType) {
    23  		panic("Parameters must have the same type")
    24  	}
    25  
    26  	if xValue.Len() == 0 {
    27  		return true
    28  	}
    29  
    30  	if yValue.Len() == 0 || yValue.Len() < xValue.Len() {
    31  		return false
    32  	}
    33  
    34  	for i := 0; i < xValue.Len(); i++ {
    35  		if !Contains(yValue.Interface(), xValue.Index(i).Interface()) {
    36  			return false
    37  		}
    38  	}
    39  
    40      return true
    41  }
    42  

View as plain text