doc: add documentation

This commit is contained in:
Ruidy 2021-12-29 10:33:00 -04:00
parent b4e8b7acc8
commit 9a96936354
6 changed files with 14 additions and 12 deletions

View file

@ -7,5 +7,4 @@ RUN go mod download
COPY *.go ./
#RUN go test -json ./... -count=1 -cover | gotestfmt
RUN go test ./...
RUN go test ./... -cover

View file

@ -1,5 +1,2 @@
lint:
golangci-lint run --fix
test:
go test -json ./... -count=1 -cover | gotestfmt
docker build .

View file

@ -1,7 +1,8 @@
package underscore
func Each[T any](values []T, predicate func(T)) {
// Each iterates over a slice of elements, yielding each in turn to an action function.
func Each[T any](values []T, action func(T)) {
for _, v := range values {
predicate(v)
action(v)
}
}

View file

@ -1,5 +1,6 @@
package underscore
// Filter looks through each value in the slice, returning a slice of all the values that pass a truth test (predicate).
func Filter[T any](values []T, predicate func(T) bool) (res []T) {
for _, v := range values {
if predicate(v) {

6
map.go
View file

@ -1,9 +1,11 @@
package underscore
func Map[T, P any](values []T, predicate func(T) P) []P {
// Map produces a new slice of values by mapping each value in the slice through
// a transform function.
func Map[T, P any](values []T, transform func(T) P) []P {
res := make([]P, 0, len(values))
for _, v := range values {
res = append(res, predicate(v))
res = append(res, transform(v))
}
return res
}

View file

@ -1,8 +1,10 @@
package underscore
func Reduce[T, P any](values []T, predicate func(T, P) P, acc P) P {
// Reduce combine a list of values into a single value.
// acc is the initial state, and each successive step of it should be returned by the reduction function.
func Reduce[T, P any](values []T, reduction func(T, P) P, acc P) P {
for _, v := range values {
acc = predicate(v, acc)
acc = reduction(v, acc)
}
return acc
}