github.com/go-board/x-go@v0.1.2-0.20220610024734-db1323f6cb15/xslice/filter.go (about)

     1  package xslice
     2  
     3  func Filter(a []interface{}, fn func(v interface{}) bool) []interface{} {
     4  	b := make([]interface{}, 0)
     5  	for _, x := range a {
     6  		if fn(x) {
     7  			b = append(b, x)
     8  		}
     9  	}
    10  	return b
    11  }
    12  
    13  func FilterString(a []string, fn func(v string) bool) []string {
    14  	b := make([]string, 0)
    15  	for _, x := range a {
    16  		if fn(x) {
    17  			b = append(b, x)
    18  		}
    19  	}
    20  	return b
    21  }
    22  
    23  func FilterInt(a []int, fn func(v int) bool) []int {
    24  	b := make([]int, 0)
    25  	for _, x := range a {
    26  		if fn(x) {
    27  			b = append(b, x)
    28  		}
    29  	}
    30  	return b
    31  }
    32  
    33  func FilterInt64(a []int64, fn func(v int64) bool) []int64 {
    34  	b := make([]int64, 0)
    35  	for _, x := range a {
    36  		if fn(x) {
    37  			b = append(b, x)
    38  		}
    39  	}
    40  	return b
    41  }