From 9271225c1b34a14c20568cf6bcb62e4e4365f7c3 Mon Sep 17 00:00:00 2001 From: Vladimir G Date: Mon, 5 Aug 2024 11:16:32 -0400 Subject: [PATCH] Update to Go 1.23 features --- Collections/CollectionFunctions.go | 17 ++------- Collections/Hashset.go | 25 +++++++------ Collections/Interfaces.go | 4 +- Collections/LinkedList.go | 34 +++++++++-------- Collections/List.go | 37 ++++++++++++------- Collections/Queue.go | 7 ++-- Collections/Stack.go | 7 ++-- FileSystem/DirectoryInfo.go | 2 +- Tests/Collections/CollectionFunctions_test.go | 6 --- Tests/Collections/Hashset_test.go | 7 ++-- Tests/Collections/List_test.go | 2 +- go.mod | 2 +- 12 files changed, 75 insertions(+), 75 deletions(-) diff --git a/Collections/CollectionFunctions.go b/Collections/CollectionFunctions.go index d042bbf..1ea11f6 100644 --- a/Collections/CollectionFunctions.go +++ b/Collections/CollectionFunctions.go @@ -5,28 +5,19 @@ func GroupBy[T any, R comparable, M ~map[R]List[T]](col Iterable[T], groupFunc f var res = make(map[R]List[T]) - col.Iterate(func(item *T) bool { - r := groupFunc(*item) + for item := range col.GetSeq() { + r := groupFunc(item) g, exists := res[r] if !exists { g = NewList[T]() res[r] = g } - g.Add(*item) + g.Add(item) + } - return true - }) return res } -func GetKeysFromMap[K comparable, V any](m map[K]V) List[K] { - r := NewList[K]() - for k := range m { - r.Add(k) - } - return r -} - func CopyMap(m *map[any]bool) *map[any]bool { m2 := map[any]bool{} for k, v := range *m { diff --git a/Collections/Hashset.go b/Collections/Hashset.go index 3154b63..8c1ecfd 100644 --- a/Collections/Hashset.go +++ b/Collections/Hashset.go @@ -1,5 +1,7 @@ package Collections +import "iter" + type Hashset[K any] struct { _imap *map[interface{}]bool } @@ -19,10 +21,9 @@ func NewHashsetFromSlice[K any](items *[]K) *Hashset[K] { func NewHashsetFromIterable[K any](items Iterable[K]) *Hashset[K] { hs := NewHashset[K]() - items.Iterate(func(item *K) bool { - hs.Add(*item) - return true - }) + for item := range items.GetSeq() { + hs.Add(item) + } return hs } @@ -51,14 +52,16 @@ func (this *Hashset[K]) Contains(key K) bool { return false } -func (this *Hashset[K]) Iterate(foreach func(item *K) bool) { - for k, _ := range *this._imap { - switch t := k.(type) { - default: - return - case K: - if !foreach(&t) { +func (this *Hashset[K]) GetSeq() iter.Seq[K] { + return func(yield func(K) bool) { + for k, _ := range *this._imap { + switch t := k.(type) { + default: return + case K: + if !yield(t) { + return + } } } } diff --git a/Collections/Interfaces.go b/Collections/Interfaces.go index 8c6eade..543a246 100644 --- a/Collections/Interfaces.go +++ b/Collections/Interfaces.go @@ -1,7 +1,9 @@ package Collections +import "iter" + type Iterable[T any] interface { - Iterate(foreach func(item *T) bool) + GetSeq() iter.Seq[T] } type Cloneable[T any] interface { diff --git a/Collections/LinkedList.go b/Collections/LinkedList.go index 2b887a2..10fc95c 100644 --- a/Collections/LinkedList.go +++ b/Collections/LinkedList.go @@ -1,5 +1,7 @@ package Collections +import "iter" + type LinkedList[T any] struct { _root *LinkedListNode[T] _end *LinkedListNode[T] @@ -70,10 +72,9 @@ func NewLinkedListFromSlice[T any](items *[]T) *LinkedList[T] { func NewLinkedListFromIterable[T any](items Iterable[T]) *LinkedList[T] { list := NewLinkedList[T]() - items.Iterate(func(item *T) bool { - list.AddLast(*item) - return true - }) + for item := range items.GetSeq() { + list.AddLast(item) + } return list } @@ -162,23 +163,24 @@ func (this *LinkedList[T]) RemoveNode(node *LinkedListNode[T]) { } } -func (this *LinkedList[K]) Iterate(foreach func(item *K) bool) { - c := this._root._next - for c._readonly == false { - val := c.GetValue() - do_next := foreach(&val) - if !do_next { - return +func (this *LinkedList[K]) GetSeq() iter.Seq[K] { + return func(yield func(K) bool) { + c := this._root._next + for c._readonly == false { + val := c.GetValue() + do_next := yield(val) + if !do_next { + return + } + c = c._next } - c = c._next } } func (this *LinkedList[T]) Clone() *LinkedList[T] { r := NewLinkedList[T]() - this.Iterate(func(item *T) bool { - r.AddLast(*item) - return true - }) + for item := range this.GetSeq() { + r.AddLast(item) + } return r } diff --git a/Collections/List.go b/Collections/List.go index 6dbd05a..90cd19f 100644 --- a/Collections/List.go +++ b/Collections/List.go @@ -1,6 +1,7 @@ package Collections import ( + "iter" "slices" ) @@ -35,10 +36,9 @@ func NewListFromSlice[T any](items *[]T) List[T] { func NewListFromIterable[T any](items Iterable[T]) List[T] { list := NewList[T]() - items.Iterate(func(item *T) bool { - list.Add(*item) - return true - }) + for item := range items.GetSeq() { + list.Add(item) + } return list } @@ -79,11 +79,10 @@ func (list List[T]) AddRange(items []T) { } // Adds all items from iterable -func (list List[T]) AddIterable(iterable Iterable[T]) { - iterable.Iterate(func(item *T) bool { - list.Add(*item) - return true - }) +func (list List[T]) AddSeq(seq iter.Seq[T]) { + for v := range seq { + list.Add(v) + } } // Clear removes all items from the list @@ -199,10 +198,22 @@ func (list List[T]) Insert(item T, index int) { list._ilist._arr[index] = item } -func (list *List[T]) Iterate(foreach func(item *T) bool) { - for i := 0; i < list.Size(); i++ { - if !foreach(&list._ilist._arr[i]) { - return +func (list *List[T]) GetSeq() iter.Seq[T] { + return func(yield func(T) bool) { + for _, item := range list._ilist._arr { + if !yield(item) { + return + } + } + } +} + +func (list *List[T]) GetSeq2() iter.Seq2[T, int] { + return func(yield func(T, int) bool) { + for i, item := range list._ilist._arr { + if !yield(item, i) { + return + } } } } diff --git a/Collections/Queue.go b/Collections/Queue.go index 13366e9..d952c4f 100644 --- a/Collections/Queue.go +++ b/Collections/Queue.go @@ -21,10 +21,9 @@ func NewQueueFromSlice[T any](items *[]T) *Queue[T] { func NewQueueFromIterable[T any](items Iterable[T]) *Queue[T] { res := NewQueue[T]() - items.Iterate(func(item *T) bool { - res.Enqueue(*item) - return true - }) + for item := range items.GetSeq() { + res.Enqueue(item) + } return res } diff --git a/Collections/Stack.go b/Collections/Stack.go index b653974..b19f1b4 100644 --- a/Collections/Stack.go +++ b/Collections/Stack.go @@ -21,10 +21,9 @@ func NewStackFromSlice[T any](items *[]T) *Stack[T] { func NewStackFromIterable[T any](items Iterable[T]) *Stack[T] { res := NewStack[T]() - items.Iterate(func(item *T) bool { - res.Push(*item) - return true - }) + for item := range items.GetSeq() { + res.Push(item) + } return res } diff --git a/FileSystem/DirectoryInfo.go b/FileSystem/DirectoryInfo.go index 96c5bdf..eea08a2 100644 --- a/FileSystem/DirectoryInfo.go +++ b/FileSystem/DirectoryInfo.go @@ -78,7 +78,7 @@ func (this *DirectoryInfo) GetAllFiles() (Collections.List[string], error) { err = err2 return } - allfiles.AddIterable(&res) + allfiles.AddSeq(res.GetSeq()) }) if err != nil { diff --git a/Tests/Collections/CollectionFunctions_test.go b/Tests/Collections/CollectionFunctions_test.go index 363c9d0..4269980 100644 --- a/Tests/Collections/CollectionFunctions_test.go +++ b/Tests/Collections/CollectionFunctions_test.go @@ -18,9 +18,3 @@ func Test_GroupBy(t *testing.T) { assert.Equal(t, 5, grouped[true].Size()) assert.Equal(t, 5, grouped[false].Size()) } - -func Test_GetKeysFromMap(t *testing.T) { - m := map[int]bool{1: true, 2: false, 3: true} - keys := Collections.GetKeysFromMap(m) - assert.Equal(t, 3, keys.Size()) -} diff --git a/Tests/Collections/Hashset_test.go b/Tests/Collections/Hashset_test.go index 7d3ebf2..c143e62 100644 --- a/Tests/Collections/Hashset_test.go +++ b/Tests/Collections/Hashset_test.go @@ -56,10 +56,9 @@ func Test_Iterate(t *testing.T) { hs.Add(20) hs.Add(3) s := 0 - hs.Iterate(func(item *int) bool { - s += *item - return true - }) + for item := range hs.GetSeq() { + s += item + } assert.Equal(t, 123, s) } diff --git a/Tests/Collections/List_test.go b/Tests/Collections/List_test.go index abbade9..f5f04d6 100644 --- a/Tests/Collections/List_test.go +++ b/Tests/Collections/List_test.go @@ -46,7 +46,7 @@ func Test_AddIterable(t *testing.T) { list1.AddRange([]int{1, 2, 3, 4, 5}) list2 := Collections.NewList[int]() list2.AddRange([]int{6, 7}) - list1.AddIterable(&list2) + list1.AddSeq(list2.GetSeq()) assert.Equal(t, 7, list1.Size()) assert.Equal(t, 1, list1.Get(0)) diff --git a/go.mod b/go.mod index ea0bbb8..2bcca4e 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/RENCI/GoUtils -go 1.22 +go 1.23 require github.com/stretchr/testify v1.9.0