Skip to content

Commit 124d300

Browse files
authored
feat: adding FilterSliceToMap (#581)
1 parent 19d8355 commit 124d300

File tree

4 files changed

+92
-5
lines changed

4 files changed

+92
-5
lines changed

README.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ Supported helpers for slices:
102102
- [Repeat](#repeat)
103103
- [RepeatBy](#repeatby)
104104
- [KeyBy](#keyby)
105-
- [Associate / SliceToMap](#associate-alias-slicetomap)
105+
- [SliceToMap / Associate](#slicetomap-alias-associate)
106+
- [FilterSliceToMap](#filterslicetomap)
106107
- [Keyify](#keyify)
107108
- [Drop](#drop)
108109
- [DropRight](#dropright)
@@ -299,6 +300,9 @@ Concurrency helpers:
299300
- [Debounce](#debounce)
300301
- [DebounceBy](#debounceby)
301302
- [Throttle](#throttle)
303+
- [ThrottleWithCount](#throttle)
304+
- [ThrottleBy](#throttle)
305+
- [ThrottleByWithCount](#throttle)
302306
- [Synchronize](#synchronize)
303307
- [Async](#async)
304308
- [Transaction](#transaction)
@@ -756,7 +760,7 @@ result := lo.KeyBy(characters, func(char Character) string {
756760

757761
[[play](https://go.dev/play/p/mdaClUAT-zZ)]
758762

759-
### Associate (alias: SliceToMap)
763+
### SliceToMap (alias: Associate)
760764

761765
Returns a map containing key-value pairs provided by transform function applied to elements of the given slice.
762766
If any of two pairs would have the same key the last one gets added to the map.
@@ -766,14 +770,34 @@ The order of keys in returned map is not specified and is not guaranteed to be t
766770
```go
767771
in := []*foo{{baz: "apple", bar: 1}, {baz: "banana", bar: 2}}
768772

769-
aMap := lo.Associate(in, func (f *foo) (string, int) {
773+
aMap := lo.SliceToMap(in, func (f *foo) (string, int) {
770774
return f.baz, f.bar
771775
})
772776
// map[string][int]{ "apple":1, "banana":2 }
773777
```
774778

775779
[[play](https://go.dev/play/p/WHa2CfMO3Lr)]
776780

781+
### FilterSliceToMap
782+
783+
Returns a map containing key-value pairs provided by transform function applied to elements of the given slice.
784+
785+
If any of two pairs would have the same key the last one gets added to the map.
786+
787+
The order of keys in returned map is not specified and is not guaranteed to be the same from the original array.
788+
789+
The third return value of the transform function is a boolean that indicates whether the key-value pair should be included in the map.
790+
791+
792+
```go
793+
list := []string{"a", "aa", "aaa"}
794+
795+
result := lo.FilterSliceToMap(list, func(str string) (string, int, bool) {
796+
return str, len(str), len(str) > 1
797+
})
798+
// map[string][int]{"aa":2 "aaa":3}
799+
```
800+
777801
### Keyify
778802

779803
Returns a map with each unique element of the slice as a key.

slice.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,23 @@ func SliceToMap[T any, K comparable, V any](collection []T, transform func(item
387387
return Associate(collection, transform)
388388
}
389389

390+
// FilterSliceToMap returns a map containing key-value pairs provided by transform function applied to elements of the given slice.
391+
// If any of two pairs would have the same key the last one gets added to the map.
392+
// The order of keys in returned map is not specified and is not guaranteed to be the same from the original array.
393+
// The third return value of the transform function is a boolean that indicates whether the key-value pair should be included in the map.
394+
func FilterSliceToMap[T any, K comparable, V any](collection []T, transform func(item T) (K, V, bool)) map[K]V {
395+
result := make(map[K]V, len(collection))
396+
397+
for i := range collection {
398+
k, v, ok := transform(collection[i])
399+
if ok {
400+
result[k] = v
401+
}
402+
}
403+
404+
return result
405+
}
406+
390407
// Keyify returns a map with each unique element of the slice as a key.
391408
func Keyify[T comparable, Slice ~[]T](collection Slice) map[T]struct{} {
392409
result := make(map[T]struct{}, len(collection))

slice_example_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,17 +276,28 @@ func ExampleKeyBy() {
276276
// Output: map[1:a 2:aa 3:aaa]
277277
}
278278

279-
func ExampleAssociate() {
279+
func ExampleSliceToMap() {
280280
list := []string{"a", "aa", "aaa"}
281281

282-
result := Associate(list, func(str string) (string, int) {
282+
result := SliceToMap(list, func(str string) (string, int) {
283283
return str, len(str)
284284
})
285285

286286
fmt.Printf("%v", result)
287287
// Output: map[a:1 aa:2 aaa:3]
288288
}
289289

290+
func ExampleFilterSliceToMap() {
291+
list := []string{"a", "aa", "aaa"}
292+
293+
result := FilterSliceToMap(list, func(str string) (string, int, bool) {
294+
return str, len(str), len(str) > 1
295+
})
296+
297+
fmt.Printf("%v", result)
298+
// Output: map[aa:2 aaa:3]
299+
}
300+
290301
func ExampleKeyify() {
291302
list := []string{"a", "a", "b", "b", "d"}
292303

slice_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,41 @@ func TestSliceToMap(t *testing.T) {
531531
}
532532
}
533533

534+
func TestFilterSliceToMap(t *testing.T) {
535+
t.Parallel()
536+
537+
type foo struct {
538+
baz string
539+
bar int
540+
}
541+
transform := func(f *foo) (string, int, bool) {
542+
return f.baz, f.bar, f.bar > 1
543+
}
544+
testCases := []struct {
545+
in []*foo
546+
expect map[string]int
547+
}{
548+
{
549+
in: []*foo{{baz: "apple", bar: 1}},
550+
expect: map[string]int{},
551+
},
552+
{
553+
in: []*foo{{baz: "apple", bar: 1}, {baz: "banana", bar: 2}},
554+
expect: map[string]int{"banana": 2},
555+
},
556+
{
557+
in: []*foo{{baz: "apple", bar: 1}, {baz: "apple", bar: 2}},
558+
expect: map[string]int{"apple": 2},
559+
},
560+
}
561+
for i, testCase := range testCases {
562+
t.Run(fmt.Sprintf("test_%d", i), func(t *testing.T) {
563+
is := assert.New(t)
564+
is.Equal(FilterSliceToMap(testCase.in, transform), testCase.expect)
565+
})
566+
}
567+
}
568+
534569
func TestKeyify(t *testing.T) {
535570
t.Parallel()
536571
is := assert.New(t)

0 commit comments

Comments
 (0)