mirror of
https://github.com/rjNemo/underscore
synced 2026-06-06 02:26:42 +00:00
doc: add documentation
This commit is contained in:
parent
b4e8b7acc8
commit
9a96936354
6 changed files with 14 additions and 12 deletions
|
|
@ -7,5 +7,4 @@ RUN go mod download
|
||||||
|
|
||||||
COPY *.go ./
|
COPY *.go ./
|
||||||
|
|
||||||
#RUN go test -json ./... -count=1 -cover | gotestfmt
|
RUN go test ./... -cover
|
||||||
RUN go test ./...
|
|
||||||
5
Makefile
5
Makefile
|
|
@ -1,5 +1,2 @@
|
||||||
lint:
|
|
||||||
golangci-lint run --fix
|
|
||||||
|
|
||||||
test:
|
test:
|
||||||
go test -json ./... -count=1 -cover | gotestfmt
|
docker build .
|
||||||
5
each.go
5
each.go
|
|
@ -1,7 +1,8 @@
|
||||||
package underscore
|
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 {
|
for _, v := range values {
|
||||||
predicate(v)
|
action(v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
package underscore
|
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) {
|
func Filter[T any](values []T, predicate func(T) bool) (res []T) {
|
||||||
for _, v := range values {
|
for _, v := range values {
|
||||||
if predicate(v) {
|
if predicate(v) {
|
||||||
|
|
|
||||||
6
map.go
6
map.go
|
|
@ -1,9 +1,11 @@
|
||||||
package underscore
|
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))
|
res := make([]P, 0, len(values))
|
||||||
for _, v := range values {
|
for _, v := range values {
|
||||||
res = append(res, predicate(v))
|
res = append(res, transform(v))
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,10 @@
|
||||||
package underscore
|
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 {
|
for _, v := range values {
|
||||||
acc = predicate(v, acc)
|
acc = reduction(v, acc)
|
||||||
}
|
}
|
||||||
return acc
|
return acc
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue