Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
sjohnsonaz committed Jul 25, 2022
0 parents commit 2dd8bf8
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 0 deletions.
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Go Array Utils
61 changes: 61 additions & 0 deletions array.go
Original file line number Diff line number Diff line change
@@ -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
}
89 changes: 89 additions & 0 deletions array_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/CardboardRobots/go-array

go 1.18

0 comments on commit 2dd8bf8

Please sign in to comment.