Skip to content

Commit 174608e

Browse files
committed
feat: adding FilterMapToSlice
1 parent d91b19f commit 174608e

4 files changed

Lines changed: 60 additions & 0 deletions

File tree

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,21 @@ s := lo.MapToSlice(m, func(k int, v int64) string {
14551455

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

1458+
### FilterMapToSlice
1459+
1460+
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.
1461+
1462+
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.
1463+
1464+
```go
1465+
kv := map[int]int64{1: 1, 2: 2, 3: 3, 4: 4}
1466+
1467+
result := lo.FilterMapToSlice(kv, func(k int, v int64) (string, bool) {
1468+
return fmt.Sprintf("%d_%d", k, v), k%2 == 0
1469+
})
1470+
// []{"2_2", "4_4"}
1471+
```
1472+
14581473
### Range / RangeFrom / RangeWithSteps
14591474

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

map.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,19 @@ func MapToSlice[K comparable, V any, R any](in map[K]V, iteratee func(key K, val
325325

326326
return result
327327
}
328+
329+
// FilterMapToSlice transforms a map into a slice based on specific iteratee.
330+
// The iteratee returns a value and a boolean. If the boolean is true, the value is added to the result slice.
331+
// If the boolean is false, the value is not added to the result slice.
332+
// 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.
333+
func FilterMapToSlice[K comparable, V any, R any](in map[K]V, iteratee func(key K, value V) (R, bool)) []R {
334+
result := make([]R, 0, len(in))
335+
336+
for k := range in {
337+
if v, ok := iteratee(k, in[k]); ok {
338+
result = append(result, v)
339+
}
340+
}
341+
342+
return result
343+
}

map_example_test.go

Lines changed: 12 additions & 0 deletions
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

Lines changed: 17 additions & 0 deletions
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)