arrayOperations

Small library for performing union, intersect, difference and distinct operations on slices in goLang

  • 所有者: adam-hanna/arrayOperations
  • 平台:
  • 許可證: MIT License
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

Build Status Coverage Status Go Report Card GoDoc

arrayOperations

Small library for performing union, intersect, difference and distinct operations on slices in goLang

I don't promise that these are optimized (not even close!), but they work :)

Quickstart

var a = []int{1, 1, 2, 3}
var b = []int{2, 4}

z, ok := Difference(a, b)
if !ok {
	fmt.Println("Cannot find difference")
}

slice, ok := z.Interface().([]int)
if !ok {
	fmt.Println("Cannot convert to slice")
}
fmt.Println(slice, reflect.TypeOf(slice)) // [1, 3, 4] []int

API

Difference

func Difference(arrs ...interface{}) (reflect.Value, bool)

Difference returns a slice of values that are only present in one of the input slices

[1, 2, 2, 4, 6] & [2, 4, 5] >> [1, 5, 6]

[1, 1, 3, 4, 5, 6] >> [1, 3, 4, 5, 6]

// EXAMPLE
var a = []int{1, 1, 2, 3}
var b = []int{2, 4}

z, ok := Difference(a, b)
if !ok {
    fmt.Println("Cannot find difference")
}

slice, ok := z.Interface().([]int)
if !ok {
    fmt.Println("Cannot convert to slice")
}
fmt.Println(slice, reflect.TypeOf(slice)) // [1, 3, 4] []int

Distinct

func Distinct(arr interface{}) (reflect.Value, bool)

Distinct returns the unique vals of a slice

[1, 1, 2, 3] >> [1, 2, 3]

// EXAMPLE
var a = []int{1, 1, 2, 3}

z, ok := Distinct(a)
if !ok {
    fmt.Println("Cannot find distinct")
}

slice, ok := z.Interface().([]int)
if !ok {
    fmt.Println("Cannot convert to slice")
}
fmt.Println(slice, reflect.TypeOf(slice)) // [1, 2, 3] []int

Intersect

func Intersect(arrs ...interface{}) (reflect.Value, bool)

Intersect returns a slice of values that are present in all of the input slices

[1, 1, 3, 4, 5, 6] & [2, 3, 6] >> [3, 6]

[1, 1, 3, 4, 5, 6] >> [1, 3, 4, 5, 6]

// EXAMPLE
var a = []int{1, 1, 2, 3}
var b = []int{2, 4}

z, ok := Intersect(a, b)
if !ok {
    fmt.Println("Cannot find intersect")
}

slice, ok := z.Interface().([]int)
if !ok {
    fmt.Println("Cannot convert to slice")
}
fmt.Println(slice, reflect.TypeOf(slice)) // [2] []int

Union

func Union(arrs ...interface{}) (reflect.Value, bool)

Union returns a slice that contains the unique values of all the input slices

[1, 2, 2, 4, 6] & [2, 4, 5] >> [1, 2, 4, 5, 6]

[1, 1, 3, 4, 5, 6] >> [1, 3, 4, 5, 6]

// EXAMPLE
var a = []int{1, 1, 2, 3}
var b = []int{2, 4}

z, ok := Union(a, b)
if !ok {
    fmt.Println("Cannot find union")
}

slice, ok := z.Interface().([]int)
if !ok {
    fmt.Println("Cannot convert to slice")
}
fmt.Println(slice, reflect.TypeOf(slice)) // [1, 2, 3, 4] []int

License

MIT

主要指標

概覽
名稱與所有者adam-hanna/arrayOperations
主編程語言Go
編程語言Go (語言數: 1)
平台
許可證MIT License
所有者活动
創建於2015-03-26 19:26:45
推送於2022-08-05 07:22:52
最后一次提交2022-03-23 11:02:55
發布數12
最新版本名稱v1.0.1 (發布於 2022-03-23 11:02:57)
第一版名稱v0.2.0 (發布於 2017-04-12 16:36:34)
用户参与
星數89
關注者數4
派生數16
提交數69
已啟用問題?
問題數2
打開的問題數0
拉請求數2
打開的拉請求數0
關閉的拉請求數0
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?