From 9a969363542cbf45bdda41d74c045b3d27dc7429 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Wed, 29 Dec 2021 10:33:00 -0400 Subject: [PATCH] doc: add documentation --- Dockerfile | 3 +-- Makefile | 5 +---- each.go | 5 +++-- filter.go | 1 + map.go | 6 ++++-- reduce.go | 6 ++++-- 6 files changed, 14 insertions(+), 12 deletions(-) diff --git a/Dockerfile b/Dockerfile index 448f9c7..d7c77b6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,5 +7,4 @@ RUN go mod download COPY *.go ./ -#RUN go test -json ./... -count=1 -cover | gotestfmt -RUN go test ./... \ No newline at end of file +RUN go test ./... -cover \ No newline at end of file diff --git a/Makefile b/Makefile index be61efe..d5f5f3d 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,2 @@ -lint: - golangci-lint run --fix - test: - go test -json ./... -count=1 -cover | gotestfmt \ No newline at end of file + docker build . \ No newline at end of file diff --git a/each.go b/each.go index 29ea020..809098c 100644 --- a/each.go +++ b/each.go @@ -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) } } diff --git a/filter.go b/filter.go index f277cb4..cc1fa9d 100644 --- a/filter.go +++ b/filter.go @@ -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) { diff --git a/map.go b/map.go index 3797dd8..a82448c 100644 --- a/map.go +++ b/map.go @@ -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 } diff --git a/reduce.go b/reduce.go index bb8691e..b628541 100644 --- a/reduce.go +++ b/reduce.go @@ -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 }