gitee.com/quant1x/gox@v1.21.2/util/lambda/README.md (about) 1 # lambda 2 3 ## overview 4 5 `lambda` is a lambda expression for go,lets you extract elements from array by lambda expression 6 7 ## Installation 8 9 go get github.com/favar/lambda 10 11 ## Getting Started 12 13 #### use LambdaArray returns Array interface 14 15 ```go 16 sa := []int{1,2,3,4,5,6,7,8,9} 17 arr := LambdaArray(sa) // return Array<int> 18 ``` 19 20 21 22 #### interface Array 23 24 ```go 25 type Array interface { 26 IsSlice() bool 27 Join(options JoinOptions) string 28 Filter(express interface{}) Array 29 Sort(express interface{}) Array 30 SortMT(express interface{}) Array 31 Map(express interface{}) Array 32 Append(elements ...interface{}) Array 33 Max(express interface{}) interface{} 34 Min(express interface{}) interface{} 35 Any(express interface{}) bool 36 All(express interface{}) bool 37 Count(express interface{}) int 38 First(express interface{}) (interface{}, error) 39 Last(express interface{}) (interface{}, error) 40 index(i int) (interface{}, error) 41 Take(skip, count int) Array 42 Sum(express interface{}) interface{} 43 Average(express interface{}) float64 44 Contains(express interface{}) bool 45 Pointer() interface{} 46 } 47 ``` 48 49 ## Usage 50 51 ***define test struct*** 52 53 ```go 54 type user struct { 55 name string 56 age int 57 } 58 ``` 59 60 61 62 #### Join 63 64 array join into string 65 66 ```go 67 type JoinOptions struct { 68 Symbol string // split string,default `,` 69 express interface{} // express match func(ele TElement) string 70 } 71 Join(options JoinOptions) string 72 ``` 73 74 ``` 75 arr := []int{1,2,3,4,5} 76 str1 := LambdaArray(arr).Join(JoinOptions{ 77 express: func(e int) string { return strconv.Itoa(e) }, 78 }) 79 fmt.Println(str1) // 1,2,3,4,5 default `,` 80 81 str2 := LambdaArray(arr).Join(JoinOptions{ 82 express: func(e int) string { return strconv.Itoa(e) }, 83 Symbol: "|", 84 }) 85 fmt.Println(str2) // 1|2|3|4|5 86 87 88 ``` 89 90 91 92 #### Filter 93 94 array filter 95 96 ```go 97 Filter(express interface{}) Array // express match func(ele TElement) bool 98 ``` 99 100 ```go 101 arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 102 larr := LambdaArray(arr) 103 ret1 := larr.Filter(func(ele int) bool { return ele > 5 }).Pointer().([]int) 104 fmt.Println(ret1) // [6 7 8 9 10] 105 106 ret2 := larr.Filter(func(ele int) bool { return ele%2 == 0 }).Pointer().([]int) 107 fmt.Println(ret2) // [2 4 6 8 10] 108 109 ret3 := LambdaArray(users).Filter(func(u user) bool { return u.age < 30 }).Pointer().([]user) 110 fmt.Println(ret3) // [{Abraham 20} {Edith 25} {Anthony 26}] 111 ``` 112 113 114 115 #### Sort 116 117 quick sort 118 119 ```go 120 Sort(express interface{}) Array // express match func(e1, e2 TElement) bool 121 ``` 122 123 ```go 124 arr := []int{1, 3, 8, 6, 12, 5, 9} 125 // order by asc 126 ret1 := LambdaArray(arr).Sort(func(a, b int) bool { return a < b }).Pointer().([]int) 127 // order by desc 128 ret2 := LambdaArray(arr).Sort(func(a, b int) bool { return a > b }).Pointer().([]int) 129 130 fmt.Println(ret1) // [1 3 5 6 8 9 12] 131 fmt.Println(ret2) // [12 9 8 6 5 3 1] 132 133 users := []user{ 134 {"Abraham", 20}, 135 {"Edith", 25}, 136 {"Charles", 40}, 137 {"Anthony", 26}, 138 {"Abel", 33}, 139 } 140 // order by user.age asc 141 ret3 := LambdaArray(users).Sort(func(a, b user) bool { return a.age < b.age }).Pointer().([]user) 142 fmt.Println(ret3) // [{Abraham 20} {Edith 25} {Anthony 26} {Abel 33} {Charles 40}] 143 ``` 144 145 146 147 #### SortMT 148 149 sort by quick multithreading 150 151 usage like Sort 152 153 #### Map 154 155 .map to new array 156 157 ```go 158 Map(express interface{}) Array // express match func(ele TElement) TOut 159 ``` 160 161 ```go 162 arr := LambdaArray([]int{1, 2, 3, 4, 5}) 163 users := arr.Map(func(i int) user { 164 return user{name: "un:" + strconv.Itoa(i), age: i} 165 }).Pointer().([]user) 166 fmt.Println(users) // [{un:1 1} {un:2 2} {un:3 3} {un:4 4} {un:5 5}] 167 168 ``` 169 170 171 172 #### Append 173 174 .append element 175 176 ```go 177 Append(elements ...interface{}) Array // each of elements type must be TElmenent 178 ``` 179 180 ```go 181 arr := LambdaArray([]int{1, 2, 3}) 182 arr.Append(4) 183 fmt.Println(arr.Pointer().([]int)) // [1 2 3 4] 184 arr.Append(5, 6) 185 fmt.Println(arr.Pointer().([]int)) // [1 2 3 4 5 6] 186 ``` 187 188 #### Max 189 190 .maximum element of array 191 192 ```go 193 Max(express interface{}) interface{} 194 ``` 195 196 ```go 197 users := []user{ 198 {"Abraham", 20}, 199 {"Edith", 25}, 200 {"Charles", 40}, 201 {"Anthony", 26}, 202 {"Abel", 33}, 203 } 204 eldest := LambdaArray(users).Max(func(u user) int { return u.age }).(user) 205 fmt.Println(eldest.name + " is the eldest") // Charles is the eldest 206 207 want := []int{1, 5, 6, 3, 8, 9, 3, 12, 56, 186, 4, 9, 14} 208 var iArr = LambdaArray(want) 209 ret := iArr.Max(nil).(int) 210 fmt.Println(ret) // 186 211 ``` 212 213 214 215 #### Min 216 217 .minimum element of array 218 219 ```go 220 Min(express interface{}) interface{} 221 ``` 222 223 ```go 224 users := []user{ 225 {"Abraham", 20}, 226 {"Edith", 25}, 227 {"Charles", 40}, 228 {"Anthony", 26}, 229 {"Abel", 33}, 230 } 231 eldest := LambdaArray(users).Min(func(u user) int { return u.age }).(user) 232 fmt.Println(eldest.name + " is the eldest") // Abraham is the Charles 233 234 want := []int{1, 5, 6, 3, 8, 9, 3, 12, 56, 186, 4, 9, 14} 235 var iArr = LambdaArray(want) 236 ret := iArr.Min(nil).(int) 237 fmt.Println(ret) // 1 238 ``` 239 240 241 242 #### Any 243 244 .Determines whether the Array contains any elements 245 246 ```go 247 Any(express interface{}) bool 248 ``` 249 250 ```go 251 us := []user{ 252 {"Abraham", 20}, 253 {"Edith", 25}, 254 {"Charles", 40}, 255 {"Anthony", 26}, 256 {"Abel", 33}, 257 } 258 ret1 := LambdaArray(us).Any(func(u user) bool { return u.age > 30 }) 259 fmt.Println(ret1) // true 260 ret2 := LambdaArray(us).Any(func(u user) bool { return u.age < 0 }) 261 fmt.Println(ret2) // false 262 ``` 263 264 #### All 265 266 Determines whether the condition is satisfied for all elements in the Array 267 268 ```go 269 All(express interface{}) bool 270 ``` 271 272 ```go 273 us := []user{ 274 {"Abraham", 20}, 275 {"Edith", 25}, 276 {"Charles", 40}, 277 {"Anthony", 26}, 278 {"Abel", 33}, 279 } 280 ret1 := LambdaArray(us).All(func(u user) bool { return u.age > 30 }) 281 fmt.Println(ret1) // false 282 ret2 := LambdaArray(us).All(func(u user) bool { return u.age > 10 }) 283 fmt.Println(ret2) // true 284 ``` 285 286 #### Count 287 288 Returns a number indicating how many elements in the specified Array satisfy the condition 289 290 ```go 291 Count(express interface{}) int 292 ``` 293 294 ```go 295 us := []user{ 296 {"Abraham", 20}, 297 {"Edith", 25}, 298 {"Charles", 40}, 299 {"Anthony", 26}, 300 {"Abel", 33}, 301 } 302 ret1 := LambdaArray(us).Count(func(u user) bool { return u.age > 30 }) 303 fmt.Println(ret1) // 2 304 ret2 := LambdaArray(us).Count(func(u user) bool { return u.age > 20 }) 305 fmt.Println(ret2) // 4 306 ``` 307 308 309 310 #### First 311 312 Returns the first element of an Array that satisfies the condition 313 314 ```go 315 First(express interface{}) (interface{}, error) 316 ``` 317 318 ```go 319 us := []user{ 320 {"Abraham", 20}, 321 {"Edith", 25}, 322 {"Charles", 40}, 323 {"Anthony", 26}, 324 {"Abel", 33}, 325 } 326 arr := LambdaArray(us) 327 if u, err := arr.First(func(u user) bool { return u.name == "Charles" }); err == nil { 328 fmt.Println(u, " found") 329 } else { 330 fmt.Println("not found") 331 } 332 // {Charles 40} found 333 if u, err := arr.First(func(u user) bool { return u.name == "jack" }); err == nil { 334 fmt.Println(u, " found") 335 } else { 336 fmt.Println("not found") 337 } 338 // not found 339 340 ``` 341 342 #### Last 343 344 Returns the last element of an Array that satisfies the condition 345 346 ```go 347 Last(express interface{}) (interface{}, error) 348 ``` 349 350 ```go 351 us := []user{ 352 {"Abraham", 20}, 353 {"Edith", 25}, 354 {"Charles", 40}, 355 {"Anthony", 26}, 356 {"Abel", 33}, 357 } 358 arr := LambdaArray(us) 359 if u, err := arr.Last(func(u user) bool { return u.name == "Anthony" }); err == nil { 360 fmt.Println(u, " found") 361 } else { 362 fmt.Println("not found") 363 } 364 // {Anthony 26} found 365 if u, err := arr.Last(func(u user) bool { return u.age > 35 }); err == nil { 366 fmt.Println(u, " found") 367 } else { 368 fmt.Println("not found") 369 } 370 // {Charles 40} found 371 ``` 372 373 374 375 #### Index 376 377 Returns the zero based index of the first occurrence in an Array 378 379 ```go 380 Index(i int) (interface{}, error) 381 ``` 382 383 ```go 384 if element, err := LambdaArray([]int{1, 2, 3, 4, 5}).Index(3); err == nil { 385 fmt.Println(element) 386 } else { 387 fmt.Println(err) 388 } 389 // 4 390 if element, err := LambdaArray([]int{1, 2, 3, 4, 5}).Index(10); err == nil { 391 fmt.Println(element) 392 } else { 393 fmt.Println(err) 394 } 395 // 10 out of range 396 ``` 397 398 399 400 #### Take 401 402 take `count` elements start by `skip` 403 404 ```go 405 Take(skip, count int) Array 406 ``` 407 408 ```go 409 ret1 := LambdaArray([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}).Take(4, 10).Pointer().([]int) 410 fmt.Println(ret1) // [5 6 7 8 9 10] 411 ret2 := LambdaArray([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}).Take(10, 10).Pointer().([]int) 412 fmt.Println(ret2) // [] 413 ``` 414 415 416 417 #### Sum 418 419 sum of the values returned by the expression 420 421 ```go 422 Sum(express interface{}) interface{} 423 ``` 424 425 ```go 426 us := []user{ 427 {"Abraham", 20}, 428 {"Edith", 25}, 429 {"Charles", 40}, 430 {"Anthony", 26}, 431 {"Abel", 33}, 432 } 433 arr := LambdaArray(us) 434 fmt.Println("total user age is ", arr.Sum(func(u user) int { return u.age })) 435 // total user age is 144 436 ``` 437 438 439 440 #### Average 441 442 average of the values returned by the expression 443 444 ```go 445 Average(express interface{}) float64 446 ``` 447 448 ```go 449 us := []user{ 450 {"Abraham", 20}, 451 {"Edith", 25}, 452 {"Charles", 40}, 453 {"Anthony", 26}, 454 {"Abel", 33}, 455 } 456 arr := LambdaArray(us) 457 fmt.Println("all user average age is", arr.Average(func(u user) int { return u.age })) 458 // all user average age is 28.8 459 ``` 460 461 462 463 #### Contains 464 465 Determines whether the array contains the specified element 466 467 ```go 468 Contains(express interface{}) bool 469 ``` 470 471 ```go 472 us := []user{ 473 {"Abraham", 20}, 474 {"Edith", 25}, 475 {"Charles", 40}, 476 {"Anthony", 26}, 477 {"Abel", 33}, 478 } 479 arr2 := LambdaArray(us) 480 fmt.Println(arr2.Contains(func(u user) bool { return u.age > 25 })) //true 481 482 fmt.Println(LambdaArray([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}).Contains(9)) // true 483 fmt.Println(LambdaArray([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}).Contains(0)) // false 484 ``` 485 486 #### Pointer 487 488 array or slice pointer 489 490 ```go 491 Pointer() interface{} 492 ``` 493 494 495 496 ## Tutorial 497 498 Usage 499 500 ## Questions 501 502 Please let me know if you have any questions. 503 504 505