Skip to content

Commit

Permalink
Merge pull request #9 from devalexandre/feat-otmization
Browse files Browse the repository at this point in the history
Feat otmization
  • Loading branch information
devalexandre authored Sep 3, 2023
2 parents a3f19d0 + 781650e commit 86e47de
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@

# Dependency directories (remove the comment below to include it)
# vendor/
.*benchmark*.
4 changes: 2 additions & 2 deletions array/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func (a *Array[T]) Filter(f func(T) bool) *Array[T] {
return &Array[T]{value: Filter(a.value, f)}
}

func (a *Array[T]) Find(f func(T) bool) *T {
return Find(&a.value, f)
func (a *Array[T]) Find(f func(T) bool) (T, bool) {
return Find(a.value, f)
}

func (a *Array[T]) Map(f func(T) T) *Array[T] {
Expand Down
4 changes: 2 additions & 2 deletions array/array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func TestArray(t *testing.T) {

t.Run("test Find", func(t *testing.T) {
a := []int{1, 2, 3, 4, 5}
b := array.NewArray(a).Find(func(x int) bool { return x%2 == 0 })
if *b != 2 {
b, _ := array.NewArray(a).Find(func(x int) bool { return x%2 == 0 })
if b != 2 {
t.Error("Find failed. Got", b, "Expected", 2)
}
})
Expand Down
45 changes: 35 additions & 10 deletions array/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,39 @@ type Number interface {
* b := Filter(a, func(x int) bool { return x%2 == 0 })
* fmt.Println(b) // [2 4]
*/

func Filter[T any](a []T, f func(T) bool) []T {
var b []T
y := make([]T, len(a))
i := 0
for _, x := range a {
if f(x) {
b = append(b, x)
y[i] = x
i++
}
}
return b
return y[:i]
}

//with side effects
func Filter0Loc[T any](a []T, f func(T) bool) []T {
i := 0
for _, x := range a {
if f(x) {
a[i] = x
i++
}
}
return a[:i]
}

func Find[T any](a *[]T, f func(T) bool) *T {
for _, x := range *a {
func Find[T any](a []T, f func(T) bool) (T, bool) {
for _, x := range a {
if f(x) {
return &x
return x, true
}
}
return nil
var zero T
return zero, false
}

/* Map
Expand All @@ -45,13 +61,21 @@ func Find[T any](a *[]T, f func(T) bool) *T {
*/

func Map[T, U any](a []T, f func(T) U) []U {
var b []U
for _, x := range a {
b = append(b, f(x))
var b = make([]U, len(a))
for i := 0; i < len(a); i++ {
b[i] = f(a[i])
}

return b
}

//with side effects
func Map0Loc[T any](a []T, f func(T) T) {
for i, x := range a {
a[i] = f(x)
}
}

/* FlatMap
* Example:
* a := []int{1, 2, 3, 4, 5}
Expand Down Expand Up @@ -416,5 +440,6 @@ func GroupBy[T any, K comparable](w []T, key func(T) K) map[K][]T {
m[key(x)] = make([]T, 0)
}
}

return m
}
26 changes: 23 additions & 3 deletions array/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,34 @@ func TestFilter(t *testing.T) {
if !reflect.DeepEqual(b, []int{2, 4}) {
t.Error("Filter failed. Got", b, "Expected", []int{2, 4})
}

if len(a) == len(b) {
t.Error("Filter failed. Got", len(a), "Expected", len(b))
}

fmt.Println(a)
}

func TestFilter0Loc(t *testing.T) {
a := []int{1, 2, 3, 4, 5}
b := array.Filter0Loc(a, func(x int) bool { return x%2 == 0 })
if !reflect.DeepEqual(b, []int{2, 4}) {
t.Error("Filter failed. Got", b, "Expected", []int{2, 4})
}

if len(a) == len(b) {
t.Error("Filter failed. Got", len(a), "Expected", len(b))
}

fmt.Println(a)
}

//test array.Find
func TestFind(t *testing.T) {
a := []int{1, 2, 3, 4, 5}
b := array.Find(&a, func(x int) bool { return x%2 == 0 })
if *b != 2 {
t.Error("Find failed. Got", *b, "Expected", 2)
b, _ := array.Find(a, func(x int) bool { return x%2 == 0 })
if b != 2 {
t.Error("Find failed. Got", b, "Expected", 2)
}
}

Expand Down

0 comments on commit 86e47de

Please sign in to comment.