Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
BreezeHubs committed Sep 24, 2022
1 parent 10ec0a5 commit ecb5832
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/bekit.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions array/array.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package array

import "errors"

type arrayT interface {
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | string
}

func For[T arrayT](arr []T, f func(T, int) any) ([]T, error) {
var newArr []T
for i, a := range arr {
v := f(a, i)
switch v.(type) {
case T:
newArr = append(newArr, v.(T))
case bool:
default:
return nil, errors.New("func has a return type that is not allowed")
}
}
return newArr, nil
}
91 changes: 91 additions & 0 deletions array/array_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package array

import (
"testing"
)

func TestArrayForInt(t *testing.T) {
arr := []int{1, 4, 5, 67, 89, 0, 346, 3}

new, err := For(arr, func(val int, index int) any {
if val > 10 {
return val
}
return false
})
if err != nil {
t.Fatal(err)
}
t.Log(new)
}

func TestArrayForString(t *testing.T) {
arr := []string{"1", "4", "5", "67", "89", "0", "346", "3"}

new, err := For(arr, func(val string, index int) any {
if len(val) > 2 {
return val
}
return false
})
if err != nil {
t.Fatal(err)
}
t.Log(new)
}

//func TestArrayFor(t *testing.T) {
// testCases := []struct {
// name string
// input any
// args any
// wantOut any
// wantErr error
// }{
// {
// name: "array for int",
// input: []int{1, 4, 5, 67, 89, 0, 346, 3},
// args: func(val int, index int) any {
// if val > 10 {
// return val
// }
// return false
// },
// wantOut: []int{67, 89, 346},
// },
// {
// name: "array for string",
// input: []string{"1", "4", "5", "67", "89", "0", "346", "3"},
// args: func(val string, index int) any {
// if len(val) > 2 {
// return val
// }
// return false
// },
// wantOut: []string{"346"},
// },
// }
//
// for _, tc := range testCases {
// t.Run(tc.name, func(t *testing.T) {
// i := tc.input.([]any)
// res, err := aa(i, tc.args)
//
// assert.Equal(t, tc.wantErr, err)
// if err != nil {
// return
// }
//
// assert.Equal(t, tc.wantOut, res)
// })
// }
//}
//
//func aa[T arrayT](input []any, args any) ([]T, error) {
// var i []T
// for _, a := range input {
// i = append(i, a)
// }
// a := args.(func(T, int) any)
// return For(i, a)
//}
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module bekit

go 1.19

require github.com/stretchr/testify v1.8.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
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.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package main

func main() {
}
5 changes: 5 additions & 0 deletions mapper/mapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package mapper

type arrayT interface {
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | string
}
1 change: 1 addition & 0 deletions mapper/mapper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package mapper

0 comments on commit ecb5832

Please sign in to comment.