github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/easy/compat.go (about)

     1  package easy
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/jxskiss/gopkg/v2/easy/ezhttp"
     7  	"github.com/jxskiss/gopkg/v2/easy/ezmap"
     8  	"github.com/jxskiss/gopkg/v2/internal/constraints"
     9  )
    10  
    11  // -------- slice utilities -------- //
    12  
    13  // InInt32s tells whether the int32 value elem is in the slice.
    14  func InInt32s(slice []int32, elem int32) bool {
    15  	return Index(slice, elem) >= 0
    16  }
    17  
    18  // InInt64s tells whether the int64 value elem is in the slice.
    19  func InInt64s(slice []int64, elem int64) bool {
    20  	return Index(slice, elem) >= 0
    21  }
    22  
    23  // InStrings tells whether the string value elem is in the slice.
    24  func InStrings(slice []string, elem string) bool {
    25  	return Index(slice, elem) >= 0
    26  }
    27  
    28  // FilterInt32s iterates the given slice, it calls predicate(i) for i in
    29  // range [0, n), where n is the length of the slice.
    30  // It returns a new slice of elements for which predicate(i) returns true.
    31  //
    32  // Deprecated: the generic function Filter is favored over this.
    33  func FilterInt32s(slice []int32, predicate func(i int) bool) []int32 {
    34  	return Filter(func(i int, _ int32) bool { return predicate(i) }, slice)
    35  }
    36  
    37  // FilterInt64s iterates the given slice, it calls predicate(i) for i in
    38  // range [0, n), where n is the length of the slice.
    39  // It returns a new slice of elements for which predicate(i) returns true.
    40  //
    41  // Deprecated: the generic function Filter is favored over this.
    42  func FilterInt64s(slice []int64, predicate func(i int) bool) []int64 {
    43  	return Filter(func(i int, _ int64) bool { return predicate(i) }, slice)
    44  }
    45  
    46  // FilterStrings iterates the given slice, it calls predicate(i) for i in
    47  // range [0, n), where n is the length of the slice.
    48  // It returns a new slice of elements for which predicate(i) returns true.
    49  //
    50  // Deprecated: the generic function Filter is favored over this.
    51  func FilterStrings(slice []string, predicate func(i int) bool) []string {
    52  	return Filter(func(i int, _ string) bool { return predicate(i) }, slice)
    53  }
    54  
    55  // ReverseInt32s returns a new slice of the elements in reversed order.
    56  //
    57  // When inplace is true, the slice is reversed in place, it does not create
    58  // a new slice, but returns the original slice with reversed order.
    59  //
    60  // Deprecated: the generic function Reverse is favored over this.
    61  func ReverseInt32s(slice []int32, inplace bool) []int32 {
    62  	return Reverse(slice, inplace)
    63  }
    64  
    65  // ReverseInt64s returns a new slice of the elements in reversed order.
    66  //
    67  // When inplace is true, the slice is reversed in place, it does not create
    68  // a new slice, but returns the original slice with reversed order.
    69  //
    70  // Deprecated: the generic function Reverse is favored over this.
    71  func ReverseInt64s(slice []int64, inplace bool) []int64 {
    72  	return Reverse(slice, inplace)
    73  }
    74  
    75  // ReverseStrings returns a new slice of the elements in reversed order.
    76  //
    77  // When inplace is true, the slice is reversed in place, it does not create
    78  // a new slice, but returns the original slice with reversed order.
    79  //
    80  // Deprecated: the generic function Reverse is favored over this.
    81  func ReverseStrings(slice []string, inplace bool) []string {
    82  	return Reverse(slice, inplace)
    83  }
    84  
    85  // SplitSlice splits a large slice []T to batches, it returns a slice
    86  // of type [][]T whose elements are sub slices of slice.
    87  //
    88  // Deprecated: the generic function Split is favored over this.
    89  func SplitSlice[S ~[]E, E any](slice S, batch int) any {
    90  	return Split[S, E](slice, batch)
    91  }
    92  
    93  // SplitInt32s splits a large int32 slice to batches.
    94  func SplitInt32s(slice []int32, batch int) [][]int32 {
    95  	return Split(slice, batch)
    96  }
    97  
    98  // SplitInt64s splits a large int64 slice to batches.
    99  func SplitInt64s(slice []int64, batch int) [][]int64 {
   100  	return Split(slice, batch)
   101  }
   102  
   103  // SplitStrings splits a large string slice to batches.
   104  func SplitStrings(slice []string, batch int) [][]string {
   105  	return Split(slice, batch)
   106  }
   107  
   108  // UniqueInt32s returns a new slice containing the elements of the given
   109  // slice in same order, but filter out duplicate values.
   110  //
   111  // When inplace is true, it does not create a new slice, the unique values
   112  // will be written to the input slice from the beginning.
   113  //
   114  // Deprecated: the generic function Unique is favored over this.
   115  func UniqueInt32s(slice []int32, inplace bool) []int32 {
   116  	return Unique(slice, inplace)
   117  }
   118  
   119  // UniqueInt64s returns a new slice containing the elements of the given
   120  // slice in same order, but filter out duplicate values.
   121  //
   122  // When inplace is true, it does not create a new slice, the unique values
   123  // will be written to the input slice from the beginning.
   124  //
   125  // Deprecated: the generic function Unique is favored over this.
   126  func UniqueInt64s(slice []int64, inplace bool) []int64 {
   127  	return Unique(slice, inplace)
   128  }
   129  
   130  // UniqueStrings returns a new slice containing the elements of the given
   131  // slice in same order, but filter out duplicate values.
   132  //
   133  // When inplace is true, it does not create a new slice, the unique values
   134  // will be written to the input slice from the beginning.
   135  //
   136  // Deprecated: the generic function Unique is favored over this.
   137  func UniqueStrings(slice []string, inplace bool) []string {
   138  	return Unique(slice, inplace)
   139  }
   140  
   141  // DiffInt32s returns a new int32 slice containing the values which present
   142  // in slice a but not present in slice b.
   143  //
   144  // Deprecated: the generic function Diff is favored over this.
   145  func DiffInt32s(a []int32, b []int32) []int32 {
   146  	return Diff(a, b)
   147  }
   148  
   149  // DiffInt64s returns a new int64 slice containing the values which present
   150  // in slice a but not present in slice b.
   151  //
   152  // Deprecated: the generic function Diff is favored over this.
   153  func DiffInt64s(a []int64, b []int64) []int64 {
   154  	return Diff(a, b)
   155  }
   156  
   157  // DiffStrings returns a new string slice containing the values which
   158  // present in slice a but not present in slice b.
   159  //
   160  // Deprecated: the generic function Diff is favored over this.
   161  func DiffStrings(a []string, b []string) []string {
   162  	return Diff(a, b)
   163  }
   164  
   165  // -------- map utilities -------- //
   166  
   167  // MapKeys returns the keys of the map m.
   168  // The keys will be in an indeterminate order.
   169  //
   170  // Deprecated: the generic function Keys is favored over this.
   171  func MapKeys[M ~map[K]V, K comparable, V any](m M) any {
   172  	return Keys[M, K, V](m)
   173  }
   174  
   175  // MapValues returns the values of the map m.
   176  // The values will be in an indeterminate order.
   177  //
   178  // Deprecated: the generic function Values is favored over this.
   179  func MapValues[M ~map[K]V, K comparable, V any](m M) any {
   180  	return Values[M, K, V](m)
   181  }
   182  
   183  // IntKeys returns a int64 slice containing all the keys present
   184  // in the map, in an indeterminate order.
   185  //
   186  // Deprecated: the generic function Keys is favored over this.
   187  func IntKeys[M ~map[K]V, K constraints.Integer, V any](m M) (keys []int64) {
   188  	keys = make([]int64, 0, len(m))
   189  	for k := range m {
   190  		keys = append(keys, int64(k))
   191  	}
   192  	return
   193  }
   194  
   195  // IntValues returns a int64 slice containing all the values present
   196  // in the map, in an indeterminate order.
   197  //
   198  // Deprecated: the generic function Values is favored over this.
   199  func IntValues[M ~map[K]V, K comparable, V constraints.Integer](m M) (values []int64) {
   200  	values = make([]int64, 0, len(m))
   201  	for _, v := range m {
   202  		values = append(values, int64(v))
   203  	}
   204  	return
   205  }
   206  
   207  // StringKeys returns a string slice containing all the keys present
   208  // in the map, in an indeterminate order.
   209  //
   210  // Deprecated: the generic function Keys is favored over this.
   211  func StringKeys[M ~map[K]V, K ~string, V any](m M) (keys []string) {
   212  	keys = make([]string, 0, len(m))
   213  	for k := range m {
   214  		keys = append(keys, string(k))
   215  	}
   216  	return
   217  }
   218  
   219  // StringValues returns a string slice containing all the values present
   220  // in the map, in an indeterminate order.
   221  //
   222  // Deprecated: the generic function Values is favored over this.
   223  func StringValues[M ~map[K]V, K comparable, V ~string](m M) (values []string) {
   224  	values = make([]string, 0, len(m))
   225  	for _, v := range m {
   226  		values = append(values, string(v))
   227  	}
   228  	return
   229  }
   230  
   231  // -------- ezmap alias names -------- //
   232  
   233  // Map is a map of string key and any value.
   234  // It provides many useful methods to work with map[string]any.
   235  //
   236  // Deprecated: this type has been moved to sub-package [ezmap],
   237  // please use ezmap.Map directly, this alias name will be removed
   238  // in future releases.
   239  type Map = ezmap.Map
   240  
   241  // SafeMap wraps Map with a RWMutex, user can acquire the lock before
   242  // accessing to the map to get concurrent safety.
   243  //
   244  // Deprecated: this type has been moved to sub-package [ezmap],
   245  // please use ezmap.SafeMap directly, this alias name will be removed
   246  // in future releases.
   247  type SafeMap = ezmap.SafeMap
   248  
   249  // NewMap returns a new initialized Map.
   250  //
   251  // Deprecated: please use ezmap.NewMap directly, this alias name will
   252  // be removed in future releases.
   253  func NewMap() Map { return ezmap.NewMap() }
   254  
   255  // NewSafeMap returns a new initialized SafeMap.
   256  //
   257  // Deprecated: please use ezmap.NewSafeMap directly, this alias name will
   258  // be removed in future releases.
   259  func NewSafeMap() *SafeMap { return ezmap.NewSafeMap() }
   260  
   261  // -------- http utilities -------- //
   262  
   263  // Request represents a request and options to send with the Do function.
   264  //
   265  // Deprecated: this type has been moved to sub-package [ezhttp],
   266  // please use [ezhttp.Request] instead of this.
   267  type Request = ezhttp.Request
   268  
   269  // DoRequest is a convenient function to send request and control redirect
   270  // and debug options.
   271  //
   272  // Deprecated: this function has been moved to sub-package [ezhttp],
   273  // please use [ezhttp.Do] instead of this.
   274  func DoRequest(req *Request) (header http.Header, respContent []byte, status int, err error) {
   275  	return ezhttp.Do(req)
   276  }