-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10ec0a5
commit ecb5832
Showing
11 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package main | ||
|
||
func main() { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package mapper |