From f73905ddb0bff4ac76076090051e5ef6586b15a9 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Thu, 27 Jan 2022 11:15:33 -0400 Subject: [PATCH] feat: maps map (#9) Co-authored-by: Ruidy --- examples/chaining.go | 2 +- examples/filterMapReduce.go | 2 +- maps/map.go | 16 +++++++++++++++ maps/map_test.go | 40 +++++++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 maps/map.go create mode 100644 maps/map_test.go diff --git a/examples/chaining.go b/examples/chaining.go index f4b5a9d..2e863f3 100644 --- a/examples/chaining.go +++ b/examples/chaining.go @@ -10,6 +10,6 @@ func chaining() int { Filter(func(n int) bool { return n%2 == 0 }). // square every number in the slice Map(func(n int) int { return n * n }). - // reduce to the sum + // reduce the slice to its sum Reduce(func(n, acc int) int { return n + acc }, 0) } diff --git a/examples/filterMapReduce.go b/examples/filterMapReduce.go index f295eb4..74752f8 100644 --- a/examples/filterMapReduce.go +++ b/examples/filterMapReduce.go @@ -10,7 +10,7 @@ func filterMapReduce() int { evens := u.Filter(numbers, func(n int) bool { return n%2 == 0 }) // square every number in the slice squares := u.Map(evens, func(n int) int { return n * n }) - // reduce to the sum + // reduce the slice to its sum res := u.Reduce(squares, func(n, acc int) int { return n + acc }, 0) return res diff --git a/maps/map.go b/maps/map.go new file mode 100644 index 0000000..ac3a404 --- /dev/null +++ b/maps/map.go @@ -0,0 +1,16 @@ +package maps + +type M[K comparable, V any] map[K]V + +// Map produces a new slice of values by mapping each value in the slice through +// a transform function. +func Map[K, Q comparable, V, W any](m M[K, V], f func(K, V) M[Q, W]) M[Q, W] { + res := make(M[Q, W], len(m)) + for k, v := range m { + mm := f(k, v) + for k2, v2 := range mm { + res[k2] = v2 + } + } + return res +} diff --git a/maps/map_test.go b/maps/map_test.go new file mode 100644 index 0000000..ee6e249 --- /dev/null +++ b/maps/map_test.go @@ -0,0 +1,40 @@ +package maps_test + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + u "github.com/rjNemo/underscore" + m "github.com/rjNemo/underscore/maps" +) + +func TestMap(t *testing.T) { + scores := m.M[string, int]{ + "alice": 0, + "bob": 10, + "clara": 7, + "david": 23, + } + + hasWon := func(key string, value int) m.M[string, bool] { return m.M[string, bool]{key: value > 21} } + want := m.M[string, bool]{ + "alice": false, + "bob": false, + "clara": false, + "david": true} + assert.Equal(t, want, m.Map(scores, hasWon)) +} + +func TestMapSlices(t *testing.T) { + scores := []m.M[string, int]{ + {"score": 0}, + {"score": 10}, + {"score": 7}, + {"score": 23}, + } + + hasWon := func(s m.M[string, int]) bool { return s["score"] > 21 } + want := []bool{false, false, false, true} + assert.Equal(t, want, u.Map(scores, hasWon)) +}