From 9cd6b0fca87679e570fbdd4942e3068feb3439fa Mon Sep 17 00:00:00 2001 From: Alexander Kiryukhin Date: Thu, 7 Apr 2022 21:36:40 +0300 Subject: [PATCH] initial --- Readme.md | 19 +++++++++++ each.go | 23 +++++++++++++ each_test.go | 30 +++++++++++++++++ example_each_test.go | 17 ++++++++++ example_map_test.go | 26 +++++++++++++++ filter.go | 34 +++++++++++++++++++ filter_test.go | 66 +++++++++++++++++++++++++++++++++++++ go.mod | 3 ++ map.go | 27 +++++++++++++++ map_test.go | 78 ++++++++++++++++++++++++++++++++++++++++++++ reduce.go | 8 +++++ reduce_test.go | 37 +++++++++++++++++++++ stack.go | 12 +++++++ stack_test.go | 36 ++++++++++++++++++++ 14 files changed, 416 insertions(+) create mode 100644 Readme.md create mode 100644 each.go create mode 100644 each_test.go create mode 100644 example_each_test.go create mode 100644 example_map_test.go create mode 100644 filter.go create mode 100644 filter_test.go create mode 100644 go.mod create mode 100644 map.go create mode 100644 map_test.go create mode 100644 reduce.go create mode 100644 reduce_test.go create mode 100644 stack.go create mode 100644 stack_test.go diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..d48a17d --- /dev/null +++ b/Readme.md @@ -0,0 +1,19 @@ +# Collection with generics + +Go >= 1.18 required. + +# Installation + +`go get https://go.neonxp.dev/collection@latest` + +# Methods + +|Method|Description|Example| +|:-----|:----------|------:| +|`Map`|Async map over slice|[example_map_test.go](./example_map_test.go)| +|`MapSync`|Sync map over slice|[example_map_test.go](./example_map_test.go)| +|`Each`|Async call cb over each element|[example_each_test.go](./example_each_test.go)| +|`MapEach`|Sync call cb over each element|[example_each_test.go](./example_each_test.go)| +|`Filter`|Returns filtered elements async|TODO| +|`FilterSync`|Returns filtered elements|TODO| +|`Reduce`|Produce one single result from a sequence of elements|TODO| diff --git a/each.go b/each.go new file mode 100644 index 0000000..d115054 --- /dev/null +++ b/each.go @@ -0,0 +1,23 @@ +package collection + +import "sync" + +func EachSync[T any](collection []T, cb func(item T, idx int)) { + for i, v := range collection { + cb(v, i) + } +} + +func Each[T any](collection []T, cb func(item T, idx int)) { + wg := sync.WaitGroup{} + wg.Add(len(collection)) + for i, v := range collection { + func(i int, v T) { + go func() { + defer wg.Done() + cb(v, i) + }() + }(i, v) + } + wg.Wait() +} diff --git a/each_test.go b/each_test.go new file mode 100644 index 0000000..f64e55b --- /dev/null +++ b/each_test.go @@ -0,0 +1,30 @@ +package collection + +import ( + "sync/atomic" + "testing" +) + +func TestEachSync(t *testing.T) { + collection := []int{1, 2, 3, 4, 5, 6} + want := 21 + sum := 0 + EachSync(collection, func(v int, _ int) { + sum += v + }) + if sum != want { + t.Errorf("Expected %d, got %d", want, sum) + } +} + +func TestEach(t *testing.T) { + collection := []int{1, 2, 3, 4, 5, 6} + want := int64(21) + sum := int64(0) + Each(collection, func(v int, _ int) { + atomic.AddInt64(&sum, int64(v)) + }) + if sum != want { + t.Errorf("Expected %d, got %d", want, sum) + } +} diff --git a/example_each_test.go b/example_each_test.go new file mode 100644 index 0000000..211494a --- /dev/null +++ b/example_each_test.go @@ -0,0 +1,17 @@ +package collection + +import "fmt" + +func ExampleEach() { + collection := []int{1, 2, 3, 4, 5, 6} + Each(collection, func(v int, idx int) { + fmt.Printf("Element %d: %d\n", idx, v) + }) +} + +func ExampleEachSync() { + collection := []int{1, 2, 3, 4, 5, 6} + EachSync(collection, func(v int, idx int) { + fmt.Printf("Element %d: %d\n", idx, v) + }) +} diff --git a/example_map_test.go b/example_map_test.go new file mode 100644 index 0000000..5ec879f --- /dev/null +++ b/example_map_test.go @@ -0,0 +1,26 @@ +package collection + +import ( + "fmt" + "strings" +) + +func ExampleMap() { + collection := []int{1, 2, 3, 4, 5} + cb := func(v int, idx int) string { + return fmt.Sprintf("[%d]", v) + } + result := Map(collection, cb) + fmt.Println(strings.Join(result, "_")) + // Output: [1]_[2]_[3]_[4]_[5] +} + +func ExampleMapSync() { + collection := []int{1, 2, 3, 4, 5} + cb := func(v int, idx int) string { + return fmt.Sprintf("[%d]", v) + } + result := MapSync(collection, cb) + fmt.Println(strings.Join(result, "_")) + // Output: [1]_[2]_[3]_[4]_[5] +} diff --git a/filter.go b/filter.go new file mode 100644 index 0000000..20c23a9 --- /dev/null +++ b/filter.go @@ -0,0 +1,34 @@ +package collection + +import "sync" + +func FilterSync[T any](collection []T, filter func(item T, idx int) bool) []T { + var result []T + for i, v := range collection { + if filter(v, i) { + result = append(result, v) + } + } + return result +} + +func Filter[T any](collection []T, filter func(item T, idx int) bool) []T { + var result []T + mu := sync.Mutex{} + wg := sync.WaitGroup{} + wg.Add(len(collection)) + for i, v := range collection { + func(v T, i int) { + go func() { + defer wg.Done() + if filter(v, i) { + mu.Lock() + result = append(result, v) + mu.Unlock() + } + }() + }(v, i) + } + wg.Wait() + return result +} diff --git a/filter_test.go b/filter_test.go new file mode 100644 index 0000000..a19d361 --- /dev/null +++ b/filter_test.go @@ -0,0 +1,66 @@ +package collection + +import ( + "reflect" + "testing" +) + +func TestFilterSync(t *testing.T) { + type args struct { + collection []int + filter func(item int, idx int) bool + } + tests := []struct { + name string + args args + want []int + }{ + { + name: "odds", + args: args{ + collection: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + filter: func(item int, idx int) bool { + return item%2 == 0 + }, + }, + want: []int{2, 4, 6, 8, 10}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := FilterSync(tt.args.collection, tt.args.filter); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Filter() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestFilter(t *testing.T) { + type args struct { + collection []int + filter func(item int, idx int) bool + } + tests := []struct { + name string + args args + want int + }{ + { + name: "odds count", + args: args{ + collection: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + filter: func(item int, idx int) bool { + return item%2 == 0 + }, + }, + want: 5, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := Filter(tt.args.collection, tt.args.filter); len(got) != tt.want { + t.Errorf("FilterParallel() returned %v elements, want %v", len(got), tt.want) + } + }) + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..74baae2 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module go.neonxp.dev/collection + +go 1.18 diff --git a/map.go b/map.go new file mode 100644 index 0000000..065a6a6 --- /dev/null +++ b/map.go @@ -0,0 +1,27 @@ +package collection + +import "sync" + +func MapSync[T any, R any](collection []T, cb func(item T, idx int) R) []R { + result := make([]R, len(collection)) + for i, v := range collection { + result[i] = cb(v, i) + } + return result +} + +func Map[T any, R any](collection []T, cb func(item T, idx int) R) []R { + result := make([]R, len(collection)) + wg := sync.WaitGroup{} + wg.Add(len(collection)) + for i, v := range collection { + func(v T, i int) { + go func() { + defer wg.Done() + result[i] = cb(v, i) + }() + }(v, i) + } + wg.Wait() + return result +} diff --git a/map_test.go b/map_test.go new file mode 100644 index 0000000..112677e --- /dev/null +++ b/map_test.go @@ -0,0 +1,78 @@ +package collection + +import ( + "fmt" + "reflect" + "testing" +) + +func TestMapSync(t *testing.T) { + type args struct { + collection []int + cb func(int, int) int + } + tests := []struct { + name string + args args + want []int + }{ + { + name: "multiple", + args: args{ + collection: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + cb: func(item int, idx int) int { + return item * idx + }, + }, + want: []int{0, 2, 6, 12, 20, 30, 42, 56, 72, 90}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := MapSync(tt.args.collection, tt.args.cb); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Map() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestMap(t *testing.T) { + type args struct { + collection []int + cb func(int, int) string + } + tests := []struct { + name string + args args + want []string + }{ + { + name: "counter", + args: args{ + collection: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + cb: func(item int, idx int) string { + return fmt.Sprintf("%dth element is %d", idx, item) + }, + }, + want: []string{ + "0th element is 1", + "1th element is 2", + "2th element is 3", + "3th element is 4", + "4th element is 5", + "5th element is 6", + "6th element is 7", + "7th element is 8", + "8th element is 9", + "9th element is 10", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := Map(tt.args.collection, tt.args.cb); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Map() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/reduce.go b/reduce.go new file mode 100644 index 0000000..46c186a --- /dev/null +++ b/reduce.go @@ -0,0 +1,8 @@ +package collection + +func Reduce[T any, R any](collection []T, cb func(previous R, current T, idx int) R, accumulator R) R { + for i, v := range collection { + accumulator = cb(accumulator, v, i) + } + return accumulator +} diff --git a/reduce_test.go b/reduce_test.go new file mode 100644 index 0000000..8df58e1 --- /dev/null +++ b/reduce_test.go @@ -0,0 +1,37 @@ +package collection + +import ( + "reflect" + "testing" +) + +func TestReduce(t *testing.T) { + type args struct { + collection []int + cb func(previous int, current int, idx int) int + accumulator int + } + tests := []struct { + name string + args args + want int + }{ + { + name: "Sum", + args: args{ + collection: []int{1, 2, 3, 4, 5, 6}, + cb: func(previous, current, idx int) int { + return previous + current + }, + }, + want: 21, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := Reduce(tt.args.collection, tt.args.cb, tt.args.accumulator); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Reduce() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/stack.go b/stack.go new file mode 100644 index 0000000..7908c98 --- /dev/null +++ b/stack.go @@ -0,0 +1,12 @@ +package collection + +func Push[T any](collection []T, element T) []T { + return append(collection, element) +} + +func Pop[T any](collection []T) ([]T, T) { + if len(collection) == 0 { + return collection, *new(T) + } + return collection[:len(collection)-1], collection[len(collection)-1] +} diff --git a/stack_test.go b/stack_test.go new file mode 100644 index 0000000..b2c1760 --- /dev/null +++ b/stack_test.go @@ -0,0 +1,36 @@ +package collection + +import ( + "reflect" + "testing" +) + +func TestPushPop(t *testing.T) { + collection := []int{} + collection = Push(collection, 1) + collection = Push(collection, 2) + collection = Push(collection, 3) + want := []int{1, 2, 3} + if !reflect.DeepEqual(collection, want) { + t.Errorf("Want %+v, but got %+v", want, collection) + } + collection, e := Pop(collection) + if e != 3 { + t.Errorf("Want 3, but got %d", e) + } + collection, e = Pop(collection) + if e != 2 { + t.Errorf("Want 2, but got %d", e) + } + collection, e = Pop(collection) + if e != 1 { + t.Errorf("Want 1, but got %d", e) + } + collection, e = Pop(collection) + if e != 0 { + t.Errorf("Want 0, but got %d", e) + } + if len(collection) != 0 { + t.Errorf("Collection must be empty, but got %+v (len = %d)", collection, len(collection)) + } +}