...

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

Documentation: github.com/thoas/go-funk

     1  package funk
     2  
     3  import (
     4  	"math/rand"
     5  )
     6  
     7  // InBools is an alias of ContainsBool, returns true if a bool is present in a iteratee.
     8  func InBools(s []bool, v bool) bool {
     9  	return ContainsBool(s, v)
    10  }
    11  
    12  // InInts is an alias of ContainsInt, returns true if an int is present in a iteratee.
    13  func InInts(s []int, v int) bool {
    14  	return ContainsInt(s, v)
    15  }
    16  
    17  // InInt32s is an alias of ContainsInt32, returns true if an int32 is present in a iteratee.
    18  func InInt32s(s []int32, v int32) bool {
    19  	return ContainsInt32(s, v)
    20  }
    21  
    22  // InInt64s is an alias of ContainsInt64, returns true if an int64 is present in a iteratee.
    23  func InInt64s(s []int64, v int64) bool {
    24  	return ContainsInt64(s, v)
    25  }
    26  
    27  // InUInts is an alias of ContainsUInt, returns true if an uint is present in a iteratee.
    28  func InUInts(s []uint, v uint) bool {
    29  	return ContainsUInt(s, v)
    30  }
    31  
    32  // InUInt32s is an alias of ContainsUInt32, returns true if an uint32 is present in a iteratee.
    33  func InUInt32s(s []uint32, v uint32) bool {
    34  	return ContainsUInt32(s, v)
    35  }
    36  
    37  // InUInt64s is an alias of ContainsUInt64, returns true if an uint64 is present in a iteratee.
    38  func InUInt64s(s []uint64, v uint64) bool {
    39  	return ContainsUInt64(s, v)
    40  }
    41  
    42  // InStrings is an alias of ContainsString, returns true if a string is present in a iteratee.
    43  func InStrings(s []string, v string) bool {
    44  	return ContainsString(s, v)
    45  }
    46  
    47  // InFloat32s is an alias of ContainsFloat32, returns true if a float32 is present in a iteratee.
    48  func InFloat32s(s []float32, v float32) bool {
    49  	return ContainsFloat32(s, v)
    50  }
    51  
    52  // InFloat64s is an alias of ContainsFloat64, returns true if a float64 is present in a iteratee.
    53  func InFloat64s(s []float64, v float64) bool {
    54  	return ContainsFloat64(s, v)
    55  }
    56  
    57  // FindFloat64 iterates over a collection of float64, returning an array of
    58  // all float64 elements predicate returns truthy for.
    59  func FindFloat64(s []float64, cb func(s float64) bool) (float64, bool) {
    60  	for _, i := range s {
    61  		result := cb(i)
    62  
    63  		if result {
    64  			return i, true
    65  		}
    66  	}
    67  
    68  	return 0.0, false
    69  }
    70  
    71  // FindFloat32 iterates over a collection of float32, returning the first
    72  // float32 element predicate returns truthy for.
    73  func FindFloat32(s []float32, cb func(s float32) bool) (float32, bool) {
    74  	for _, i := range s {
    75  		result := cb(i)
    76  
    77  		if result {
    78  			return i, true
    79  		}
    80  	}
    81  
    82  	return 0.0, false
    83  }
    84  
    85  // FindInt iterates over a collection of int, returning the first
    86  // int element predicate returns truthy for.
    87  func FindInt(s []int, cb func(s int) bool) (int, bool) {
    88  	for _, i := range s {
    89  		result := cb(i)
    90  
    91  		if result {
    92  			return i, true
    93  		}
    94  	}
    95  
    96  	return 0, false
    97  }
    98  
    99  // FindInt32 iterates over a collection of int32, returning the first
   100  // int32 element predicate returns truthy for.
   101  func FindInt32(s []int32, cb func(s int32) bool) (int32, bool) {
   102  	for _, i := range s {
   103  		result := cb(i)
   104  
   105  		if result {
   106  			return i, true
   107  		}
   108  	}
   109  
   110  	return 0, false
   111  }
   112  
   113  // FindInt64 iterates over a collection of int64, returning the first
   114  // int64 element predicate returns truthy for.
   115  func FindInt64(s []int64, cb func(s int64) bool) (int64, bool) {
   116  	for _, i := range s {
   117  		result := cb(i)
   118  
   119  		if result {
   120  			return i, true
   121  		}
   122  	}
   123  
   124  	return 0, false
   125  }
   126  
   127  // FindString iterates over a collection of string, returning the first
   128  // string element predicate returns truthy for.
   129  func FindString(s []string, cb func(s string) bool) (string, bool) {
   130  	for _, i := range s {
   131  		result := cb(i)
   132  
   133  		if result {
   134  			return i, true
   135  		}
   136  	}
   137  
   138  	return "", false
   139  }
   140  
   141  // FilterBool iterates over a collection of bool, returning an array of
   142  // all bool elements predicate returns truthy for.
   143  func FilterBool(s []bool, cb func(s bool) bool) []bool {
   144  	results := []bool{}
   145  
   146  	for _, i := range s {
   147  		result := cb(i)
   148  
   149  		if result {
   150  			results = append(results, i)
   151  		}
   152  	}
   153  
   154  	return results
   155  }
   156  
   157  // FilterFloat64 iterates over a collection of float64, returning an array of
   158  // all float64 elements predicate returns truthy for.
   159  func FilterFloat64(s []float64, cb func(s float64) bool) []float64 {
   160  	results := []float64{}
   161  
   162  	for _, i := range s {
   163  		result := cb(i)
   164  
   165  		if result {
   166  			results = append(results, i)
   167  		}
   168  	}
   169  
   170  	return results
   171  }
   172  
   173  // FilterFloat32 iterates over a collection of float32, returning an array of
   174  // all float32 elements predicate returns truthy for.
   175  func FilterFloat32(s []float32, cb func(s float32) bool) []float32 {
   176  	results := []float32{}
   177  
   178  	for _, i := range s {
   179  		result := cb(i)
   180  
   181  		if result {
   182  			results = append(results, i)
   183  		}
   184  	}
   185  
   186  	return results
   187  }
   188  
   189  // FilterInt iterates over a collection of int, returning an array of
   190  // all int elements predicate returns truthy for.
   191  func FilterInt(s []int, cb func(s int) bool) []int {
   192  	results := []int{}
   193  
   194  	for _, i := range s {
   195  		result := cb(i)
   196  
   197  		if result {
   198  			results = append(results, i)
   199  		}
   200  	}
   201  
   202  	return results
   203  }
   204  
   205  // FilterInt32 iterates over a collection of int32, returning an array of
   206  // all int32 elements predicate returns truthy for.
   207  func FilterInt32(s []int32, cb func(s int32) bool) []int32 {
   208  	results := []int32{}
   209  
   210  	for _, i := range s {
   211  		result := cb(i)
   212  
   213  		if result {
   214  			results = append(results, i)
   215  		}
   216  	}
   217  
   218  	return results
   219  }
   220  
   221  // FilterInt64 iterates over a collection of int64, returning an array of
   222  // all int64 elements predicate returns truthy for.
   223  func FilterInt64(s []int64, cb func(s int64) bool) []int64 {
   224  	results := []int64{}
   225  
   226  	for _, i := range s {
   227  		result := cb(i)
   228  
   229  		if result {
   230  			results = append(results, i)
   231  		}
   232  	}
   233  
   234  	return results
   235  }
   236  
   237  // FilterUInt iterates over a collection of uint, returning an array of
   238  // all uint elements predicate returns truthy for.
   239  func FilterUInt(s []uint, cb func(s uint) bool) []uint {
   240  	results := []uint{}
   241  
   242  	for _, i := range s {
   243  		result := cb(i)
   244  
   245  		if result {
   246  			results = append(results, i)
   247  		}
   248  	}
   249  
   250  	return results
   251  }
   252  
   253  // FilterUInt32 iterates over a collection of uint32, returning an array of
   254  // all uint32 elements predicate returns truthy for.
   255  func FilterUInt32(s []uint32, cb func(s uint32) bool) []uint32 {
   256  	results := []uint32{}
   257  
   258  	for _, i := range s {
   259  		result := cb(i)
   260  
   261  		if result {
   262  			results = append(results, i)
   263  		}
   264  	}
   265  
   266  	return results
   267  }
   268  
   269  // FilterUInt64 iterates over a collection of uint64, returning an array of
   270  // all uint64 elements predicate returns truthy for.
   271  func FilterUInt64(s []uint64, cb func(s uint64) bool) []uint64 {
   272  	results := []uint64{}
   273  
   274  	for _, i := range s {
   275  		result := cb(i)
   276  
   277  		if result {
   278  			results = append(results, i)
   279  		}
   280  	}
   281  
   282  	return results
   283  }
   284  
   285  // FilterString iterates over a collection of string, returning an array of
   286  // all string elements predicate returns truthy for.
   287  func FilterString(s []string, cb func(s string) bool) []string {
   288  	results := []string{}
   289  
   290  	for _, i := range s {
   291  		result := cb(i)
   292  
   293  		if result {
   294  			results = append(results, i)
   295  		}
   296  	}
   297  
   298  	return results
   299  }
   300  
   301  // ContainsBool returns true if a boolean is present in a iteratee.
   302  func ContainsBool(s []bool, v bool) bool {
   303  	for _, vv := range s {
   304  		if vv == v {
   305  			return true
   306  		}
   307  	}
   308  	return false
   309  }
   310  
   311  // ContainsInt returns true if an int is present in a iteratee.
   312  func ContainsInt(s []int, v int) bool {
   313  	for _, vv := range s {
   314  		if vv == v {
   315  			return true
   316  		}
   317  	}
   318  	return false
   319  }
   320  
   321  // ContainsInt32 returns true if an int32 is present in a iteratee.
   322  func ContainsInt32(s []int32, v int32) bool {
   323  	for _, vv := range s {
   324  		if vv == v {
   325  			return true
   326  		}
   327  	}
   328  	return false
   329  }
   330  
   331  // ContainsInt64 returns true if an int64 is present in a iteratee.
   332  func ContainsInt64(s []int64, v int64) bool {
   333  	for _, vv := range s {
   334  		if vv == v {
   335  			return true
   336  		}
   337  	}
   338  	return false
   339  }
   340  
   341  // ContainsUInt returns true if an uint is present in a iteratee.
   342  func ContainsUInt(s []uint, v uint) bool {
   343  	for _, vv := range s {
   344  		if vv == v {
   345  			return true
   346  		}
   347  	}
   348  	return false
   349  }
   350  
   351  // ContainsUInt32 returns true if an uint32 is present in a iteratee.
   352  func ContainsUInt32(s []uint32, v uint32) bool {
   353  	for _, vv := range s {
   354  		if vv == v {
   355  			return true
   356  		}
   357  	}
   358  	return false
   359  }
   360  
   361  // ContainsUInt64 returns true if an uint64 is present in a iteratee.
   362  func ContainsUInt64(s []uint64, v uint64) bool {
   363  	for _, vv := range s {
   364  		if vv == v {
   365  			return true
   366  		}
   367  	}
   368  	return false
   369  }
   370  
   371  // ContainsString returns true if a string is present in a iteratee.
   372  func ContainsString(s []string, v string) bool {
   373  	for _, vv := range s {
   374  		if vv == v {
   375  			return true
   376  		}
   377  	}
   378  	return false
   379  }
   380  
   381  // ContainsFloat32 returns true if a float32 is present in a iteratee.
   382  func ContainsFloat32(s []float32, v float32) bool {
   383  	for _, vv := range s {
   384  		if vv == v {
   385  			return true
   386  		}
   387  	}
   388  	return false
   389  }
   390  
   391  // ContainsFloat64 returns true if a float64 is present in a iteratee.
   392  func ContainsFloat64(s []float64, v float64) bool {
   393  	for _, vv := range s {
   394  		if vv == v {
   395  			return true
   396  		}
   397  	}
   398  	return false
   399  }
   400  
   401  // SumInt32 sums a int32 iteratee and returns the sum of all elements
   402  func SumInt32(s []int32) (sum int32) {
   403  	for _, v := range s {
   404  		sum += v
   405  	}
   406  	return
   407  }
   408  
   409  // SumInt64 sums a int64 iteratee and returns the sum of all elements
   410  func SumInt64(s []int64) (sum int64) {
   411  	for _, v := range s {
   412  		sum += v
   413  	}
   414  	return
   415  }
   416  
   417  // SumInt sums a int iteratee and returns the sum of all elements
   418  func SumInt(s []int) (sum int) {
   419  	for _, v := range s {
   420  		sum += v
   421  	}
   422  	return
   423  }
   424  
   425  // SumUInt32 sums a uint32 iteratee and returns the sum of all elements
   426  func SumUInt32(s []uint32) (sum uint32) {
   427  	for _, v := range s {
   428  		sum += v
   429  	}
   430  	return
   431  }
   432  
   433  // SumUInt64 sums a uint64 iteratee and returns the sum of all elements
   434  func SumUInt64(s []uint64) (sum uint64) {
   435  	for _, v := range s {
   436  		sum += v
   437  	}
   438  	return
   439  }
   440  
   441  // SumUInt sums a uint iteratee and returns the sum of all elements
   442  func SumUInt(s []uint) (sum uint) {
   443  	for _, v := range s {
   444  		sum += v
   445  	}
   446  	return
   447  }
   448  
   449  // SumFloat64 sums a float64 iteratee and returns the sum of all elements
   450  func SumFloat64(s []float64) (sum float64) {
   451  	for _, v := range s {
   452  		sum += v
   453  	}
   454  	return
   455  }
   456  
   457  // SumFloat32 sums a float32 iteratee and returns the sum of all elements
   458  func SumFloat32(s []float32) (sum float32) {
   459  	for _, v := range s {
   460  		sum += v
   461  	}
   462  	return
   463  }
   464  
   465  // ReverseBools reverses an array of bool
   466  func ReverseBools(s []bool) []bool {
   467  	for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
   468  		s[i], s[j] = s[j], s[i]
   469  	}
   470  	return s
   471  }
   472  
   473  // ReverseStrings reverses an array of string
   474  func ReverseStrings(s []string) []string {
   475  	for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
   476  		s[i], s[j] = s[j], s[i]
   477  	}
   478  	return s
   479  }
   480  
   481  // ReverseInt reverses an array of int
   482  func ReverseInt(s []int) []int {
   483  	for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
   484  		s[i], s[j] = s[j], s[i]
   485  	}
   486  	return s
   487  }
   488  
   489  // ReverseInt32 reverses an array of int32
   490  func ReverseInt32(s []int32) []int32 {
   491  	for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
   492  		s[i], s[j] = s[j], s[i]
   493  	}
   494  	return s
   495  }
   496  
   497  // ReverseInt64 reverses an array of int64
   498  func ReverseInt64(s []int64) []int64 {
   499  	for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
   500  		s[i], s[j] = s[j], s[i]
   501  	}
   502  	return s
   503  }
   504  
   505  // ReverseUInt reverses an array of int
   506  func ReverseUInt(s []uint) []uint {
   507  	for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
   508  		s[i], s[j] = s[j], s[i]
   509  	}
   510  	return s
   511  }
   512  
   513  // ReverseUInt32 reverses an array of uint32
   514  func ReverseUInt32(s []uint32) []uint32 {
   515  	for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
   516  		s[i], s[j] = s[j], s[i]
   517  	}
   518  	return s
   519  }
   520  
   521  // ReverseUInt64 reverses an array of uint64
   522  func ReverseUInt64(s []uint64) []uint64 {
   523  	for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
   524  		s[i], s[j] = s[j], s[i]
   525  	}
   526  	return s
   527  }
   528  
   529  // ReverseFloat64 reverses an array of float64
   530  func ReverseFloat64(s []float64) []float64 {
   531  	for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
   532  		s[i], s[j] = s[j], s[i]
   533  	}
   534  	return s
   535  }
   536  
   537  // ReverseFloat32 reverses an array of float32
   538  func ReverseFloat32(s []float32) []float32 {
   539  	for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
   540  		s[i], s[j] = s[j], s[i]
   541  	}
   542  	return s
   543  }
   544  
   545  // ReverseString reverses a string
   546  func ReverseString(s string) string {
   547  	r := []rune(s)
   548  	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
   549  		r[i], r[j] = r[j], r[i]
   550  	}
   551  	return string(r)
   552  }
   553  
   554  func indexOf(n int, f func(int) bool) int {
   555  	for i := 0; i < n; i++ {
   556  		if f(i) {
   557  			return i
   558  		}
   559  	}
   560  	return -1
   561  }
   562  
   563  // IndexOfBool gets the index at which the first occurrence of a bool value is found in array or return -1
   564  // if the value cannot be found
   565  func IndexOfBool(a []bool, x bool) int {
   566  	return indexOf(len(a), func(i int) bool { return a[i] == x })
   567  }
   568  
   569  // IndexOfInt gets the index at which the first occurrence of an int value is found in array or return -1
   570  // if the value cannot be found
   571  func IndexOfInt(a []int, x int) int {
   572  	return indexOf(len(a), func(i int) bool { return a[i] == x })
   573  }
   574  
   575  // IndexOfInt32 gets the index at which the first occurrence of an int32 value is found in array or return -1
   576  // if the value cannot be found
   577  func IndexOfInt32(a []int32, x int32) int {
   578  	return indexOf(len(a), func(i int) bool { return a[i] == x })
   579  }
   580  
   581  // IndexOfInt64 gets the index at which the first occurrence of an int64 value is found in array or return -1
   582  // if the value cannot be found
   583  func IndexOfInt64(a []int64, x int64) int {
   584  	return indexOf(len(a), func(i int) bool { return a[i] == x })
   585  }
   586  
   587  // IndexOfUInt gets the index at which the first occurrence of an uint value is found in array or return -1
   588  // if the value cannot be found
   589  func IndexOfUInt(a []uint, x uint) int {
   590  	return indexOf(len(a), func(i int) bool { return a[i] == x })
   591  }
   592  
   593  // IndexOfUInt32 gets the index at which the first occurrence of an uint32 value is found in array or return -1
   594  // if the value cannot be found
   595  func IndexOfUInt32(a []uint32, x uint32) int {
   596  	return indexOf(len(a), func(i int) bool { return a[i] == x })
   597  }
   598  
   599  // IndexOfUInt64 gets the index at which the first occurrence of an uint64 value is found in array or return -1
   600  // if the value cannot be found
   601  func IndexOfUInt64(a []uint64, x uint64) int {
   602  	return indexOf(len(a), func(i int) bool { return a[i] == x })
   603  }
   604  
   605  // IndexOfFloat64 gets the index at which the first occurrence of an float64 value is found in array or return -1
   606  // if the value cannot be found
   607  func IndexOfFloat64(a []float64, x float64) int {
   608  	return indexOf(len(a), func(i int) bool { return a[i] == x })
   609  }
   610  
   611  // IndexOfString gets the index at which the first occurrence of a string value is found in array or return -1
   612  // if the value cannot be found
   613  func IndexOfString(a []string, x string) int {
   614  	return indexOf(len(a), func(i int) bool { return a[i] == x })
   615  }
   616  
   617  func lastIndexOf(n int, f func(int) bool) int {
   618  	for i := n - 1; i >= 0; i-- {
   619  		if f(i) {
   620  			return i
   621  		}
   622  	}
   623  	return -1
   624  }
   625  
   626  // LastIndexOfBool gets the index at which the first occurrence of a bool value is found in array or return -1
   627  // if the value cannot be found
   628  func LastIndexOfBool(a []bool, x bool) int {
   629  	return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
   630  }
   631  
   632  // LastIndexOfInt gets the index at which the first occurrence of an int value is found in array or return -1
   633  // if the value cannot be found
   634  func LastIndexOfInt(a []int, x int) int {
   635  	return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
   636  }
   637  
   638  // LastIndexOfInt32 gets the index at which the first occurrence of an int32 value is found in array or return -1
   639  // if the value cannot be found
   640  func LastIndexOfInt32(a []int32, x int32) int {
   641  	return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
   642  }
   643  
   644  // LastIndexOfInt64 gets the index at which the first occurrence of an int64 value is found in array or return -1
   645  // if the value cannot be found
   646  func LastIndexOfInt64(a []int64, x int64) int {
   647  	return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
   648  }
   649  
   650  // LastIndexOfUInt gets the index at which the first occurrence of an uint value is found in array or return -1
   651  // if the value cannot be found
   652  func LastIndexOfUInt(a []uint, x uint) int {
   653  	return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
   654  }
   655  
   656  // LastIndexOfUInt32 gets the index at which the first occurrence of an uint32 value is found in array or return -1
   657  // if the value cannot be found
   658  func LastIndexOfUInt32(a []uint32, x uint32) int {
   659  	return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
   660  }
   661  
   662  // LastIndexOfUInt64 gets the index at which the first occurrence of an uint64 value is found in array or return -1
   663  // if the value cannot be found
   664  func LastIndexOfUInt64(a []uint64, x uint64) int {
   665  	return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
   666  }
   667  
   668  // LastIndexOfFloat64 gets the index at which the first occurrence of an float64 value is found in array or return -1
   669  // if the value cannot be found
   670  func LastIndexOfFloat64(a []float64, x float64) int {
   671  	return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
   672  }
   673  
   674  // LastIndexOfFloat32 gets the index at which the first occurrence of an float32 value is found in array or return -1
   675  // if the value cannot be found
   676  func LastIndexOfFloat32(a []float32, x float32) int {
   677  	return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
   678  }
   679  
   680  // LastIndexOfString gets the index at which the first occurrence of a string value is found in array or return -1
   681  // if the value cannot be found
   682  func LastIndexOfString(a []string, x string) int {
   683  	return lastIndexOf(len(a), func(i int) bool { return a[i] == x })
   684  }
   685  
   686  // UniqBool creates an array of bool with unique values.
   687  func UniqBool(a []bool) []bool {
   688  	results := []bool{}
   689  	for _, value := range a {
   690  		// If results is not empty, there is at most 1 value in it
   691  		if len(results) == 0 || results[0] != value {
   692  			results = append(results, value)
   693  		}
   694  		// At most 2 unique values
   695  		if len(results) == 2 {
   696  			break
   697  		}
   698  	}
   699  	return results
   700  }
   701  
   702  // UniqInt32 creates an array of int32 with unique values.
   703  func UniqInt32(a []int32) []int32 {
   704  	var (
   705  		length  = len(a)
   706  		seen    = make(map[int32]struct{}, length)
   707  		j       = 0
   708  		results = make([]int32, 0)
   709  	)
   710  
   711  	for i := 0; i < length; i++ {
   712  		v := a[i]
   713  
   714  		if _, ok := seen[v]; ok {
   715  			continue
   716  		}
   717  
   718  		seen[v] = struct{}{}
   719  		results = append(results, v)
   720  		j++
   721  	}
   722  
   723  	return results
   724  }
   725  
   726  // UniqInt64 creates an array of int64 with unique values.
   727  func UniqInt64(a []int64) []int64 {
   728  	var (
   729  		length  = len(a)
   730  		seen    = make(map[int64]struct{}, length)
   731  		results = make([]int64, 0)
   732  		j       = 0
   733  	)
   734  
   735  	for i := 0; i < length; i++ {
   736  		v := a[i]
   737  
   738  		if _, ok := seen[v]; ok {
   739  			continue
   740  		}
   741  
   742  		seen[v] = struct{}{}
   743  		results = append(results, v)
   744  		j++
   745  	}
   746  
   747  	return results
   748  }
   749  
   750  // UniqInt creates an array of int with unique values.
   751  func UniqInt(a []int) []int {
   752  	var (
   753  		length  = len(a)
   754  		seen    = make(map[int]struct{}, length)
   755  		results = make([]int, 0)
   756  		j       = 0
   757  	)
   758  
   759  	for i := 0; i < length; i++ {
   760  		v := a[i]
   761  
   762  		if _, ok := seen[v]; ok {
   763  			continue
   764  		}
   765  
   766  		seen[v] = struct{}{}
   767  		results = append(results, v)
   768  		j++
   769  	}
   770  
   771  	return results
   772  }
   773  
   774  // UniqUInt32 creates an array of uint32 with unique values.
   775  func UniqUInt32(a []uint32) []uint32 {
   776  	var (
   777  		length  = len(a)
   778  		seen    = make(map[uint32]struct{}, length)
   779  		j       = 0
   780  		results = make([]uint32, 0)
   781  	)
   782  
   783  	for i := 0; i < length; i++ {
   784  		v := a[i]
   785  
   786  		if _, ok := seen[v]; ok {
   787  			continue
   788  		}
   789  
   790  		seen[v] = struct{}{}
   791  		results = append(results, v)
   792  		j++
   793  	}
   794  
   795  	return results
   796  }
   797  
   798  // UniqUInt64 creates an array of uint64 with unique values.
   799  func UniqUInt64(a []uint64) []uint64 {
   800  	var (
   801  		length  = len(a)
   802  		seen    = make(map[uint64]struct{}, length)
   803  		j       = 0
   804  		results = make([]uint64, 0)
   805  	)
   806  
   807  	for i := 0; i < length; i++ {
   808  		v := a[i]
   809  
   810  		if _, ok := seen[v]; ok {
   811  			continue
   812  		}
   813  
   814  		seen[v] = struct{}{}
   815  		results = append(results, v)
   816  		j++
   817  	}
   818  
   819  	return results
   820  }
   821  
   822  // UniqUInt creates an array of uint with unique values.
   823  func UniqUInt(a []uint) []uint {
   824  	var (
   825  		length  = len(a)
   826  		seen    = make(map[uint]struct{}, length)
   827  		j       = 0
   828  		results = make([]uint, 0)
   829  	)
   830  
   831  	for i := 0; i < length; i++ {
   832  		v := a[i]
   833  
   834  		if _, ok := seen[v]; ok {
   835  			continue
   836  		}
   837  
   838  		seen[v] = struct{}{}
   839  		results = append(results, v)
   840  		j++
   841  	}
   842  
   843  	return results
   844  }
   845  
   846  // UniqString creates an array of string with unique values.
   847  func UniqString(a []string) []string {
   848  	var (
   849  		length  = len(a)
   850  		seen    = make(map[string]struct{}, length)
   851  		j       = 0
   852  		results = make([]string, 0)
   853  	)
   854  
   855  	for i := 0; i < length; i++ {
   856  		v := a[i]
   857  
   858  		if _, ok := seen[v]; ok {
   859  			continue
   860  		}
   861  
   862  		seen[v] = struct{}{}
   863  		results = append(results, v)
   864  		j++
   865  	}
   866  
   867  	return results
   868  }
   869  
   870  // UniqFloat64 creates an array of float64 with unique values.
   871  func UniqFloat64(a []float64) []float64 {
   872  	var (
   873  		length  = len(a)
   874  		seen    = make(map[float64]struct{}, length)
   875  		j       = 0
   876  		results = make([]float64, 0)
   877  	)
   878  
   879  	for i := 0; i < length; i++ {
   880  		v := a[i]
   881  
   882  		if _, ok := seen[v]; ok {
   883  			continue
   884  		}
   885  
   886  		seen[v] = struct{}{}
   887  		results = append(results, v)
   888  		j++
   889  	}
   890  
   891  	return results
   892  }
   893  
   894  // UniqFloat32 creates an array of float32 with unique values.
   895  func UniqFloat32(a []float32) []float32 {
   896  	var (
   897  		length  = len(a)
   898  		seen    = make(map[float32]struct{}, length)
   899  		j       = 0
   900  		results = make([]float32, 0)
   901  	)
   902  
   903  	for i := 0; i < length; i++ {
   904  		v := a[i]
   905  
   906  		if _, ok := seen[v]; ok {
   907  			continue
   908  		}
   909  
   910  		seen[v] = struct{}{}
   911  		results = append(results, v)
   912  		j++
   913  	}
   914  
   915  	return results
   916  }
   917  
   918  // ShuffleBool creates an array of bool shuffled values using Fisher–Yates algorithm
   919  func ShuffleBool(a []bool) []bool {
   920  	for i := range a {
   921  		j := rand.Intn(i + 1)
   922  		a[i], a[j] = a[j], a[i]
   923  	}
   924  
   925  	return a
   926  }
   927  
   928  // ShuffleInt creates an array of int shuffled values using Fisher–Yates algorithm
   929  func ShuffleInt(a []int) []int {
   930  	for i := range a {
   931  		j := rand.Intn(i + 1)
   932  		a[i], a[j] = a[j], a[i]
   933  	}
   934  
   935  	return a
   936  }
   937  
   938  // ShuffleInt32 creates an array of int32 shuffled values using Fisher–Yates algorithm
   939  func ShuffleInt32(a []int32) []int32 {
   940  	for i := range a {
   941  		j := rand.Intn(i + 1)
   942  		a[i], a[j] = a[j], a[i]
   943  	}
   944  
   945  	return a
   946  }
   947  
   948  // ShuffleInt64 creates an array of int64 shuffled values using Fisher–Yates algorithm
   949  func ShuffleInt64(a []int64) []int64 {
   950  	for i := range a {
   951  		j := rand.Intn(i + 1)
   952  		a[i], a[j] = a[j], a[i]
   953  	}
   954  
   955  	return a
   956  }
   957  
   958  // ShuffleUInt creates an array of int shuffled values using Fisher–Yates algorithm
   959  func ShuffleUInt(a []uint) []uint {
   960  	for i := range a {
   961  		j := rand.Intn(i + 1)
   962  		a[i], a[j] = a[j], a[i]
   963  	}
   964  
   965  	return a
   966  }
   967  
   968  // ShuffleUInt32 creates an array of uint32 shuffled values using Fisher–Yates algorithm
   969  func ShuffleUInt32(a []uint32) []uint32 {
   970  	for i := range a {
   971  		j := rand.Intn(i + 1)
   972  		a[i], a[j] = a[j], a[i]
   973  	}
   974  
   975  	return a
   976  }
   977  
   978  // ShuffleUInt64 creates an array of uint64 shuffled values using Fisher–Yates algorithm
   979  func ShuffleUInt64(a []uint64) []uint64 {
   980  	for i := range a {
   981  		j := rand.Intn(i + 1)
   982  		a[i], a[j] = a[j], a[i]
   983  	}
   984  
   985  	return a
   986  }
   987  
   988  // ShuffleString creates an array of string shuffled values using Fisher–Yates algorithm
   989  func ShuffleString(a []string) []string {
   990  	for i := range a {
   991  		j := rand.Intn(i + 1)
   992  		a[i], a[j] = a[j], a[i]
   993  	}
   994  
   995  	return a
   996  }
   997  
   998  // ShuffleFloat32 creates an array of float32 shuffled values using Fisher–Yates algorithm
   999  func ShuffleFloat32(a []float32) []float32 {
  1000  	for i := range a {
  1001  		j := rand.Intn(i + 1)
  1002  		a[i], a[j] = a[j], a[i]
  1003  	}
  1004  
  1005  	return a
  1006  }
  1007  
  1008  // ShuffleFloat64 creates an array of float64 shuffled values using Fisher–Yates algorithm
  1009  func ShuffleFloat64(a []float64) []float64 {
  1010  	for i := range a {
  1011  		j := rand.Intn(i + 1)
  1012  		a[i], a[j] = a[j], a[i]
  1013  	}
  1014  
  1015  	return a
  1016  }
  1017  
  1018  // DropBool creates a slice with `n` bools dropped from the beginning.
  1019  func DropBool(s []bool, n int) []bool {
  1020  	return s[n:]
  1021  }
  1022  
  1023  // DropString creates a slice with `n` strings dropped from the beginning.
  1024  func DropString(s []string, n int) []string {
  1025  	return s[n:]
  1026  }
  1027  
  1028  // DropInt creates a slice with `n` ints dropped from the beginning.
  1029  func DropInt(s []int, n int) []int {
  1030  	return s[n:]
  1031  }
  1032  
  1033  // DropInt32 creates a slice with `n` int32s dropped from the beginning.
  1034  func DropInt32(s []int32, n int) []int32 {
  1035  	return s[n:]
  1036  }
  1037  
  1038  // DropInt64 creates a slice with `n` int64s dropped from the beginning.
  1039  func DropInt64(s []int64, n int) []int64 {
  1040  	return s[n:]
  1041  }
  1042  
  1043  // DropUInt creates a slice with `n` ints dropped from the beginning.
  1044  func DropUInt(s []uint, n uint) []uint {
  1045  	return s[n:]
  1046  }
  1047  
  1048  // DropUInt32 creates a slice with `n` int32s dropped from the beginning.
  1049  func DropUInt32(s []uint32, n int) []uint32 {
  1050  	return s[n:]
  1051  }
  1052  
  1053  // DropUInt64 creates a slice with `n` int64s dropped from the beginning.
  1054  func DropUInt64(s []uint64, n int) []uint64 {
  1055  	return s[n:]
  1056  }
  1057  
  1058  // DropFloat32 creates a slice with `n` float32s dropped from the beginning.
  1059  func DropFloat32(s []float32, n int) []float32 {
  1060  	return s[n:]
  1061  }
  1062  
  1063  // DropFloat64 creates a slice with `n` float64s dropped from the beginning.
  1064  func DropFloat64(s []float64, n int) []float64 {
  1065  	return s[n:]
  1066  }
  1067  

View as plain text