git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/validate/arrays_example_test.go (about) 1 package validate 2 3 func ExampleFilter() { 4 data := []interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 5 var fn ConditionIterator = func(value interface{}, index int) bool { 6 return value.(int)%2 == 0 7 } 8 _ = Filter(data, fn) // result = []interface{}{2, 4, 6, 8, 10} 9 } 10 11 func ExampleCount() { 12 data := []interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 13 var fn ConditionIterator = func(value interface{}, index int) bool { 14 return value.(int)%2 == 0 15 } 16 _ = Count(data, fn) // result = 5 17 } 18 19 func ExampleMap() { 20 data := []interface{}{1, 2, 3, 4, 5} 21 var fn ResultIterator = func(value interface{}, index int) interface{} { 22 return value.(int) * 3 23 } 24 _ = Map(data, fn) // result = []interface{}{1, 6, 9, 12, 15} 25 } 26 27 func ExampleEach() { 28 data := []interface{}{1, 2, 3, 4, 5} 29 var fn Iterator = func(value interface{}, index int) { 30 println(value.(int)) 31 } 32 Each(data, fn) 33 } 34 35 func ExampleFind() { 36 data := []interface{}{1, 2, 3, 4, 5} 37 var fn ConditionIterator = func(value interface{}, index int) bool { 38 return value.(int) == 4 39 } 40 _ = Find(data, fn) // result = 4 41 }