Skip to content

Commit e5dba6f

Browse files
authored
feat: adding FilterMapToSlice (#623)
1 parent e343af0 commit e5dba6f

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -1456,6 +1456,21 @@ s := lo.MapToSlice(m, func(k int, v int64) string {
14561456

14571457
[[play](https://go.dev/play/p/ZuiCZpDt6LD)]
14581458

1459+
### FilterMapToSlice
1460+
1461+
Transforms a map into a slice based on specific iteratee. The iteratee returns a value and a boolean. If the boolean is true, the value is added to the result slice.
1462+
1463+
If the boolean is false, the value is not added to the result slice. The order of the keys in the input map is not specified and the order of the keys in the output slice is not guaranteed.
1464+
1465+
```go
1466+
kv := map[int]int64{1: 1, 2: 2, 3: 3, 4: 4}
1467+
1468+
result := lo.FilterMapToSlice(kv, func(k int, v int64) (string, bool) {
1469+
return fmt.Sprintf("%d_%d", k, v), k%2 == 0
1470+
})
1471+
// []{"2_2", "4_4"}
1472+
```
1473+
14591474
### Range / RangeFrom / RangeWithSteps
14601475

14611476
Creates an array of numbers (positive and/or negative) progressing from start up to, but not including end.

map.go

+16
Original file line numberDiff line numberDiff line change
@@ -326,3 +326,19 @@ func MapToSlice[K comparable, V any, R any](in map[K]V, iteratee func(key K, val
326326

327327
return result
328328
}
329+
330+
// FilterMapToSlice transforms a map into a slice based on specific iteratee.
331+
// The iteratee returns a value and a boolean. If the boolean is true, the value is added to the result slice.
332+
// If the boolean is false, the value is not added to the result slice.
333+
// The order of the keys in the input map is not specified and the order of the keys in the output slice is not guaranteed.
334+
func FilterMapToSlice[K comparable, V any, R any](in map[K]V, iteratee func(key K, value V) (R, bool)) []R {
335+
result := make([]R, 0, len(in))
336+
337+
for k := range in {
338+
if v, ok := iteratee(k, in[k]); ok {
339+
result = append(result, v)
340+
}
341+
}
342+
343+
return result
344+
}

map_example_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,15 @@ func ExampleMapToSlice() {
232232
fmt.Printf("%v", result)
233233
// Output: [1_1 2_2 3_3 4_4]
234234
}
235+
236+
func ExampleFilterMapToSlice() {
237+
kv := map[int]int64{1: 1, 2: 2, 3: 3, 4: 4}
238+
239+
result := FilterMapToSlice(kv, func(k int, v int64) (string, bool) {
240+
return fmt.Sprintf("%d_%d", k, v), k%2 == 0
241+
})
242+
243+
sort.StringSlice(result).Sort()
244+
fmt.Printf("%v", result)
245+
// Output: [2_2 4_4]
246+
}

map_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,23 @@ func TestMapToSlice(t *testing.T) {
513513
is.ElementsMatch(result2, []string{"1", "2", "3", "4"})
514514
}
515515

516+
func TestFilterMapToSlice(t *testing.T) {
517+
t.Parallel()
518+
is := assert.New(t)
519+
520+
result1 := FilterMapToSlice(map[int]int{1: 5, 2: 6, 3: 7, 4: 8}, func(k int, v int) (string, bool) {
521+
return fmt.Sprintf("%d_%d", k, v), k%2 == 0
522+
})
523+
result2 := FilterMapToSlice(map[int]int{1: 5, 2: 6, 3: 7, 4: 8}, func(k int, _ int) (string, bool) {
524+
return strconv.FormatInt(int64(k), 10), k%2 == 0
525+
})
526+
527+
is.Equal(len(result1), 2)
528+
is.Equal(len(result2), 2)
529+
is.ElementsMatch(result1, []string{"2_6", "4_8"})
530+
is.ElementsMatch(result2, []string{"2", "4"})
531+
}
532+
516533
func BenchmarkAssign(b *testing.B) {
517534
counts := []int{32768, 1024, 128, 32, 2}
518535

0 commit comments

Comments
 (0)