1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-12 16:52:29 +08:00

feat: add FilterMap

This commit is contained in:
dudaodong
2023-02-24 10:55:27 +08:00
parent f7aaa1ed2a
commit 5767aad303
3 changed files with 54 additions and 0 deletions

View File

@@ -3,6 +3,7 @@ package slice
import (
"fmt"
"math"
"strconv"
"testing"
"github.com/duke-git/lancet/v2/internal"
@@ -315,6 +316,23 @@ func TestMap(t *testing.T) {
assert.Equal(studentsOfAdd10Aage, Map(students, mapFunc))
}
func TestFilterMap(t *testing.T) {
assert := internal.NewAssert(t, "TestFilterMap")
nums := []int{1, 2, 3, 4, 5}
getEvenNumStr := func(i, num int) (string, bool) {
if num%2 == 0 {
return strconv.FormatInt(int64(num), 10), true
}
return "", false
}
result := FilterMap(nums, getEvenNumStr)
assert.Equal([]string{"2", "4"}, result)
}
func TestReduce(t *testing.T) {
cases := [][]int{
{},