feat: map

This commit is contained in:
Ruidy 2021-12-28 19:26:42 -04:00
parent 3ad658c76c
commit d27ed24589
9 changed files with 174 additions and 0 deletions

59
.gitignore vendored Normal file
View file

@ -0,0 +1,59 @@
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace
.history/
.idea/
cmake-build-*/
*.iws
out/
.idea_modules/
atlassian-ide-plugin.xml
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
*~
.fuse_hidden*
.directory
.Trash-*
.nfs*
*.exe
*.exe~
*.dll
*.so
*.dylib
*.test
*.out
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db
*.stackdump
[Dd]esktop.ini
$RECYCLE.BIN/
*.cab
*.msi
*.msix
*.msm
*.msp
*.lnk
.DS_Store
.AppleDouble
.LSOverride
Icon
._*
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

42
.golangci.yml Normal file
View file

@ -0,0 +1,42 @@
skip-dirs-use-default: true
run:
timeout: 5m
linters:
enable:
- bodyclose
- deadcode
- depguard
- dogsled
- errcheck
- errorlint
- exportloopref
- gocritic
- gocyclo
- gofmt
- goimports
- goprintffuncname
- gosimple
- gosec
- govet
- ineffassign
- misspell
- noctx
- nolintlint
- prealloc
- rowserrcheck
- staticcheck
- structcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
- whitespace
fast: true
linters-settings:
goimports:
local-prefixes: github.com/rjNemo/underscore

11
Dockerfile Normal file
View file

@ -0,0 +1,11 @@
FROM golang:1.18beta1-bullseye
WORKDIR /app
COPY go.* ./
RUN go mod download
COPY *.go ./
#RUN go test -json ./... -count=1 -cover | gotestfmt
RUN go test ./...

5
Makefile Normal file
View file

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

4
README.md Normal file
View file

@ -0,0 +1,4 @@
# Underscore
A port from the `underscore.js` library based on generics brought by `go1.18`.

11
go.mod Normal file
View file

@ -0,0 +1,11 @@
module github.com/rjNemo/underscore
go 1.18
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.3.0 // indirect
github.com/stretchr/testify v1.7.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)

15
go.sum Normal file
View file

@ -0,0 +1,15 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.3.0 h1:NGXK3lHquSN08v5vWalVI/L8XU9hdzE/G6xsrze47As=
github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

9
map.go Normal file
View file

@ -0,0 +1,9 @@
package underscore
func Map[T, P any](values []T, predicate func(T) P) []P {
res := make([]P, 0, len(values))
for _, v := range values {
res = append(res, predicate(v))
}
return res
}

18
map_test.go Normal file
View file

@ -0,0 +1,18 @@
package underscore_test
import (
"testing"
"github.com/stretchr/testify/assert"
u "github.com/rjNemo/underscore"
)
func TestMap(t *testing.T) {
nums := []int{1, 2, 3}
f := func(n int) int {
return n * n
}
want := []int{1, 4, 9}
assert.Equal(t, want, u.Map(nums, f))
}