mirror of
https://github.com/rjNemo/underscore
synced 2026-06-06 02:26:42 +00:00
test: examples
This commit is contained in:
parent
81c98e8bf5
commit
8cb444fa3f
6 changed files with 75 additions and 11 deletions
48
.dockerignore
Normal file
48
.dockerignore
Normal 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
|
||||
|
|
@ -5,6 +5,6 @@ WORKDIR /app
|
|||
COPY go.* ./
|
||||
RUN go mod download
|
||||
|
||||
COPY *.go ./
|
||||
COPY . ./
|
||||
|
||||
RUN go test ./... -cover
|
||||
|
|
@ -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
11
examples/chaining_test.go
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
package examples
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestChainingExample(t *testing.T) {
|
||||
assert.Equal(t, 120, chaining())
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
11
examples/filterMapReduce_test.go
Normal file
11
examples/filterMapReduce_test.go
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
package examples
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestFilterMapReduceExample(t *testing.T) {
|
||||
assert.Equal(t, 120, filterMapReduce())
|
||||
}
|
||||
Loading…
Reference in a new issue