From 2dd8bf8b6b3157cc3533c64b75e608744901a76b Mon Sep 17 00:00:00 2001 From: Sean Johnson Date: Sun, 24 Jul 2022 21:38:30 -0700 Subject: [PATCH] Initial commit. --- LICENSE | 7 ++++ Readme.md | 1 + array.go | 61 +++++++++++++++++++++++++++++++++++ array_test.go | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 3 ++ 5 files changed, 161 insertions(+) create mode 100644 LICENSE create mode 100644 Readme.md create mode 100644 array.go create mode 100644 array_test.go create mode 100644 go.mod diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e4780ac --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2022 Jitter, LLC + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..2056fde --- /dev/null +++ b/Readme.md @@ -0,0 +1 @@ +# Go Array Utils diff --git a/array.go b/array.go new file mode 100644 index 0000000..e264f36 --- /dev/null +++ b/array.go @@ -0,0 +1,61 @@ +package array + +import "errors" + +func ForEach[T any](array []T, callback func(item T)) { + for _, item := range array { + callback(item) + } +} + +func Map[T any, U any](array []T, callback func(item T) U) []U { + var output []U = make([]U, len(array)) + for index, item := range array { + result := callback(item) + output[index] = result + } + return output +} + +func Reduce[T any, U any](array []T, callback func(target U, item T) U, target U) U { + var output U = target + for _, item := range array { + output = callback(output, item) + } + return output +} + +func Filter[T any](array []T, callback func(item T) bool) []T { + var output []T = make([]T, 0) + for _, item := range array { + valid := callback(item) + if valid { + output = append(output, item) + } + } + return output +} + +func Equal[T comparable](a, b []T) bool { + if len(a) != len(b) { + return false + } + for i, v := range a { + if v != b[i] { + return false + } + } + return true +} + +var ErrNotFound = errors.New("not found") + +func Find[T any](array []T, callback func(item T) bool) (*T, error) { + for _, item := range array { + match := callback(item) + if match { + return &item, nil + } + } + return nil, ErrNotFound +} diff --git a/array_test.go b/array_test.go new file mode 100644 index 0000000..038ce44 --- /dev/null +++ b/array_test.go @@ -0,0 +1,89 @@ +package array + +import ( + "fmt" + "testing" +) + +func TestForEach(t *testing.T) { + items := []int64{0, 1, 2} + result := 0 + + ForEach(items, func(item int64) { + result++ + }) + + want := 3 + + if result != want { + t.Errorf("Count is wrong. Expected: %#v, Received: %#v", want, result) + } +} + +// Map should create an array with a new value for each value of the original array +func TestMapEach(t *testing.T) { + items := []int64{0, 1, 2} + + result := Map(items, func(item int64) string { + return "" + fmt.Sprint(item) + }) + + want := []string{"0", "1", "2"} + + if !Equal(want, result) { + t.Errorf("Count is wrong. Expected: %#v, Received: %#v", want, result) + } +} + +func TestNestedMap(t *testing.T) { + items := []int64{0, 1, 2} + + result := Map(Map(Map( + items, + func(item int64) float64 { + return float64(item) / 2 + }), + func(item float64) string { + return "" + fmt.Sprint(item) + }), + func(item string) string { + return "a" + item + }) + + want := []string{"a0", "a0.5", "a1"} + + if !Equal(want, result) { + t.Errorf("Count is wrong. Expected: %#v, Received: %#v", want, result) + } +} + +func TestReduce(t *testing.T) { + items := []int{0, 1, 2} + target := 0 + + result := Reduce(items, func(target int, item int) int { + return target + item + }, target) + + want := 3 + + if result != want { + t.Errorf("Count is wrong. Expected: %#v, Received: %#v", want, result) + } +} + +func TestFilter(t *testing.T) { + items := []int{0, 3, 1, 4, 2, 5} + + result := Filter(items, func(item int) bool { + if item >= 3 { + return true + } else { + return false + } + }) + + if !Equal(result, []int{3, 4, 5}) { + t.Error("Count is wrong") + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ea46171 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/CardboardRobots/go-array + +go 1.18