test: examples

This commit is contained in:
Ruidy 2022-01-03 11:36:54 -04:00
parent 81c98e8bf5
commit 8cb444fa3f
6 changed files with 75 additions and 11 deletions

48
.dockerignore Normal file
View file

@ -0,0 +1,48 @@
*~
.fuse_hidden*
.directory
.Trash-*
.nfs*
/public/
/resources/_gen/
hugo.exe
hugo.darwin
hugo.linux
*.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
/docs

View file

@ -5,6 +5,6 @@ WORKDIR /app
COPY go.* ./
RUN go mod download
COPY *.go ./
COPY . ./
RUN go test ./... -cover

View file

@ -1,19 +1,15 @@
package examples
import (
"fmt"
"github.com/rjNemo/underscore/chain"
)
func chaining() {
sum := chain.Of([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}).
func chaining() int {
return chain.Of([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}).
// filter even numbers from the slice
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(func(n, acc int) int { return n + acc }, 0)
fmt.Println(sum)
}

11
examples/chaining_test.go Normal file
View file

@ -0,0 +1,11 @@
package examples
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestChainingExample(t *testing.T) {
assert.Equal(t, 120, chaining())
}

View file

@ -1,12 +1,10 @@
package examples
import (
"fmt"
u "github.com/rjNemo/underscore"
)
func filterMapReduce() {
func filterMapReduce() int {
numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
// filter even numbers from the slice
evens := u.Filter(numbers, func(n int) bool { return n%2 == 0 })
@ -15,5 +13,5 @@ func filterMapReduce() {
// reduce to the sum
res := u.Reduce(squares, func(n, acc int) int { return n + acc }, 0)
fmt.Println(res)
return res
}

View file

@ -0,0 +1,11 @@
package examples
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestFilterMapReduceExample(t *testing.T) {
assert.Equal(t, 120, filterMapReduce())
}