Skip to content
Merged

each #10

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion each.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package underscore

// Each iterates over a slice of elements, yielding each in turn to an action function.
func Each[T any](values []T, action func(T)) {
// Returns the slice for chaining.
func Each[T any](values []T, action func(T)) []T {
for _, v := range values {
action(v)
}
return values
}
11 changes: 11 additions & 0 deletions each_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,14 @@ func TestEach(t *testing.T) {

assert.Equal(t, want, res)
}

func TestEachReturnsInitialSlice(t *testing.T) {
names := []string{"Alice", "Bob", "Charles"}
want := []string{"Alice", "Bob", "Charles"}

res := make([]string, 0)

assert.Equal(t, want, underscore.Each(names, func(n string) {
res = append(res, fmt.Sprintf("Hi %s", n))
}))
}
2 changes: 1 addition & 1 deletion examples/chaining.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion examples/filterMapReduce.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions maps/map.go
Original file line number Diff line number Diff line change
@@ -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
}
40 changes: 40 additions & 0 deletions maps/map_test.go
Original file line number Diff line number Diff line change
@@ -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))
}