Skip to content

Commit

Permalink
[feature] Support lower versions of Golang, now the required version …
Browse files Browse the repository at this point in the history
…is 1.18 or higher
  • Loading branch information
BioCrossCoder committed May 26, 2024
1 parent 49713e5 commit 7fa15ea
Show file tree
Hide file tree
Showing 15 changed files with 79 additions and 57 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ A Golang library to make development easier and more efficient

1. Robustness: Improve the coverage of unit tests.
2. Functionality: Provide more practical functions in `functools` for manipulating `sequences` (`slice`/`array`/`string`).
3. Compatibility: Support lower versions of `Golang` to ensure compatibility with older projects; split `typed` package.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/biocrosscoder/flex

go 1.21.0
go 1.18.0

require (
github.com/smartystreets/goconvey v1.8.1
Expand Down
5 changes: 4 additions & 1 deletion typed/collections/orderedcontainers/orderedchaindict.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ func (d OrderedChainDict[K, V]) Items() []*dict.DictItem[K, V] {
items := make([]*dict.DictItem[K, V], d.Size())
i := 0
_ = d.sequence.ForEach(func(key K) K {
items[i] = &dict.DictItem[K, V]{key, d.Get(key)}
items[i] = &dict.DictItem[K, V]{
Key: key,
Value: d.Get(key),
}
i++
return key
})
Expand Down
5 changes: 4 additions & 1 deletion typed/collections/orderedcontainers/ordereddict.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ func (d OrderedDict[K, V]) Values() []V {
func (d OrderedDict[K, V]) Items() []*dict.DictItem[K, V] {
items := make([]*dict.DictItem[K, V], d.Size())
for i, key := range d.sequence {
items[i] = &dict.DictItem[K, V]{key, d.Get(key)}
items[i] = &dict.DictItem[K, V]{
Key: key,
Value: d.Get(key),
}
}
return items
}
Expand Down
2 changes: 1 addition & 1 deletion typed/collections/queue/priorityqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewPriorityQueue[T any](capacity int) (q PriorityQueue[T], err error) {
err = common.ErrInvalidCapacity
return
}
elements := sortedcontainers.NewSortedDict(sortedlist.DescendOrder, dict.Dict[int, Queue[T]]{})
elements := sortedcontainers.NewSortedDict(sortedlist.DescendOrder[int], dict.Dict[int, Queue[T]]{})
q = &priorityQueue[T]{*elements, capacity, 0}
return
}
Expand Down
4 changes: 2 additions & 2 deletions typed/collections/set/modify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ func TestPop(t *testing.T) {
assert.False(t, s.Has(element))
})
convey.Convey("pop an element from an empty set", t, func() {
s := Set[any]{}
s := Set[int]{}
element, err := s.Pop()
assert.Equal(t, common.ErrEmptySet, err)
assert.Nil(t, element)
assert.Zero(t, element)
})
}

Expand Down
7 changes: 5 additions & 2 deletions typed/collections/sortedcontainers/sorteddict.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewSortedDict[K cmp.Ordered, V any](f func(a, b K) int, src dict.Dict[K, V]
src = make(dict.Dict[K, V])
}
if f == nil {
f = sortedlist.AscendOrder
f = sortedlist.AscendOrder[K]
}
return &SortedDict[K, V]{
src,
Expand Down Expand Up @@ -104,7 +104,10 @@ func (d SortedDict[K, V]) Items() []*dict.DictItem[K, V] {
for i := 0; i < length; i++ {
key, _ := d.KeyAt(i)
value := d.Get(key)
items[i] = &dict.DictItem[K, V]{key, value}
items[i] = &dict.DictItem[K, V]{
Key: key,
Value: value,
}
}
return items
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func TestReduce(t *testing.T) {
convey.Convey("reduce list", t, func() {
l := NewSortedList(AscendOrder, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
l := NewSortedList(AscendOrder[int], 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
f := func(x, y int) int {
return x - y
}
Expand Down Expand Up @@ -55,17 +55,17 @@ func TestReduce(t *testing.T) {

func TestFilter(t *testing.T) {
convey.Convey("filter list", t, func() {
l := NewSortedList(DescendOrder, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
l := NewSortedList(DescendOrder[int], 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
f := func(x int) bool {
return x%2 == 0
}
assert.True(t, NewSortedList(DescendOrder, 2, 4, 6, 8, 10).Equal(l.Filter(f)))
assert.True(t, NewSortedList(DescendOrder[int], 2, 4, 6, 8, 10).Equal(l.Filter(f)))
})
}

func TestSomeAndEvery(t *testing.T) {
convey.Convey("check condition on list", t, func() {
l := NewSortedList(AscendOrder, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
l := NewSortedList(AscendOrder[int], 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
f := func(x int) bool {
return x > 9
}
Expand Down
12 changes: 6 additions & 6 deletions typed/collections/sortedcontainers/sortedlist/modify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestRemove(t *testing.T) {
l := NewSortedList(AscendOrder, 1, 2, 3, 2, 4, 2, 3, 2, 1)
l := NewSortedList(AscendOrder[int], 1, 2, 3, 2, 4, 2, 3, 2, 1)
entry := 2
count := l.Count(entry)
length := l.Len()
Expand Down Expand Up @@ -68,7 +68,7 @@ func TestRemove(t *testing.T) {
}

func TestAddOrCutElement(t *testing.T) {
l := NewSortedList(AscendOrder, 1, 2, 3, 4, 5)
l := NewSortedList(AscendOrder[int], 1, 2, 3, 4, 5)
length := l.Len()
convey.Convey("remove one element from list tail", t, func() {
l2 := l.Copy()
Expand Down Expand Up @@ -110,14 +110,14 @@ func TestAddOrCutElement(t *testing.T) {

func TestReverse(t *testing.T) {
convey.Convey("reverse the list", t, func() {
l := NewSortedList(AscendOrder, 1, 2, 3, 4, 5)
l := NewSortedList(AscendOrder[int], 1, 2, 3, 4, 5)
_ = l.Reverse()
assert.True(t, l.Equal(*NewSortedList(DescendOrder, 1, 2, 3, 4, 5)))
assert.True(t, l.Equal(*NewSortedList(DescendOrder[int], 1, 2, 3, 4, 5)))
})
}

func ExampleSortedList_RemoveRange() {
l := NewSortedList(AscendOrder, 2, 3, 1, 4, 5)
l := NewSortedList(AscendOrder[int], 2, 3, 1, 4, 5)
fmt.Println(l.ToArray())
removed := l.RemoveRange(1, 3)
fmt.Println(removed.ToArray())
Expand All @@ -129,7 +129,7 @@ func ExampleSortedList_RemoveRange() {
}

func ExampleSortedList_Insert() {
l := NewSortedList(AscendOrder, 1, 5, 8)
l := NewSortedList(AscendOrder[int], 1, 5, 8)
fmt.Println(l.ToArray())
l.Insert(3)
fmt.Println(l.ToArray())
Expand Down
6 changes: 3 additions & 3 deletions typed/collections/sortedcontainers/sortedlist/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestSearchElement(t *testing.T) {
convey.Convey("search element in list", t, func() {
l := NewSortedList(AscendOrder, 1, 2, 3, 2, 4)
l := NewSortedList(AscendOrder[int], 1, 2, 3, 2, 4)
convey.Convey("search by index", func() {
assert.Equal(t, 1, l.IndexOf(2))
assert.Equal(t, 2, l.LastIndexOf(2))
Expand Down Expand Up @@ -73,7 +73,7 @@ func TestSearchElement(t *testing.T) {
}

func ExampleSortedList_Max() {
l := NewSortedList(AscendOrder, 1, 2, 3, 2, 4)
l := NewSortedList(AscendOrder[int], 1, 2, 3, 2, 4)
m, _ := l.Max()
fmt.Println(m)
l.Reverse()
Expand All @@ -85,7 +85,7 @@ func ExampleSortedList_Max() {
}

func ExampleSortedList_Min() {
l := NewSortedList(AscendOrder, 1, 2, 3, 2, 4)
l := NewSortedList(AscendOrder[int], 1, 2, 3, 2, 4)
m, _ := l.Min()
fmt.Println(m)
l.Reverse()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func TestSortedList(t *testing.T) {
list := arraylist.Of(1, 3, 2, 5, 6, 9)
convey.Convey("create a sorted list from a slice", t, func() {
l := NewSortedList(AscendOrder, list...)
l := NewSortedList(AscendOrder[int], list...)
assert.False(t, l.Empty())
assert.Equal(t, 6, l.Len())
assert.Equal(t, []int{1, 2, 3, 5, 6, 9}, l.ToArray())
Expand All @@ -21,14 +21,14 @@ func TestSortedList(t *testing.T) {
}
})
convey.Convey("copy a sorted list from a sorted list", t, func() {
l := NewSortedList(DescendOrder, list...)
l := NewSortedList(DescendOrder[int], list...)
assert.Equal(t, []int{9, 6, 5, 3, 2, 1}, l.Copy().ToArray())
assert.True(t, l.Slice(-1, -10, -1).Equal(l.ToReversed()))
})
}

func ExampleSortedList() {
l := NewSortedList(AscendOrder, 1, 3, 2, 5, 6, 9)
l := NewSortedList(AscendOrder[int], 1, 3, 2, 5, 6, 9)
fmt.Println(l.ToArray())
// Output: [1 2 3 5 6 9]
}
2 changes: 1 addition & 1 deletion typed/collections/sortedcontainers/sortedset.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type SortedSet[T cmp.Ordered] struct {
func NewSortedSet[T cmp.Ordered](f func(a, b T) int, entries ...T) *SortedSet[T] {
elements := set.Of(entries...)
if f == nil {
f = sortedlist.AscendOrder
f = sortedlist.AscendOrder[T]
}
sequence := sortedlist.NewSortedList(f)
for element := range elements {
Expand Down
42 changes: 21 additions & 21 deletions typed/functools/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
func TestCompare(t *testing.T) {
convey.Convey("get max/min element", t, func() {
arr := []int{1, 2, 3, 4, 5}
assert.Equal(t, 1, Max(sortedlist.DescendOrder, arr...))
assert.Equal(t, 1, Min(sortedlist.AscendOrder, arr...))
assert.Equal(t, 5, Max(sortedlist.AscendOrder, arr...))
assert.Equal(t, 5, Min(sortedlist.DescendOrder, arr...))
assert.Equal(t, 1, Max(sortedlist.DescendOrder[int], arr...))
assert.Equal(t, 1, Min(sortedlist.AscendOrder[int], arr...))
assert.Equal(t, 5, Max(sortedlist.AscendOrder[int], arr...))
assert.Equal(t, 5, Min(sortedlist.DescendOrder[int], arr...))
})
convey.Convey("compare elements", t, func() {
cmp := func(a, b []int) int {
Expand All @@ -29,28 +29,28 @@ func TestCompare(t *testing.T) {
})
convey.Convey("check monotonicity of array", t, func() {
arr := []int{1, 2, 3, 4, 5}
assert.True(t, IsMonotonic(arr, sortedlist.AscendOrder, true))
assert.True(t, IsMonotonic(arr, sortedlist.AscendOrder, false))
assert.True(t, IsMonotonic(arr, sortedlist.AscendOrder[int], true))
assert.True(t, IsMonotonic(arr, sortedlist.AscendOrder[int], false))
arr2 := []int{1, 2, 3, 4, 4}
assert.False(t, IsMonotonic(arr2, sortedlist.AscendOrder, true))
assert.True(t, IsMonotonic(arr2, sortedlist.AscendOrder, false))
assert.False(t, IsMonotonic(arr2, sortedlist.AscendOrder[int], true))
assert.True(t, IsMonotonic(arr2, sortedlist.AscendOrder[int], false))
arr3 := []int{5, 4, 3, 2, 1}
assert.True(t, IsMonotonic(arr3, sortedlist.AscendOrder, true))
assert.True(t, IsMonotonic(arr3, sortedlist.AscendOrder, false))
assert.True(t, IsMonotonic(arr3, sortedlist.AscendOrder[int], true))
assert.True(t, IsMonotonic(arr3, sortedlist.AscendOrder[int], false))
arr4 := []int{5, 3, 3, 2, 1}
assert.False(t, IsMonotonic(arr4, sortedlist.AscendOrder, true))
assert.True(t, IsMonotonic(arr4, sortedlist.AscendOrder, false))
assert.False(t, IsMonotonic(arr4, sortedlist.AscendOrder[int], true))
assert.True(t, IsMonotonic(arr4, sortedlist.AscendOrder[int], false))
})
}

func ExampleMax() {
max := Max(sortedlist.DescendOrder, 5, 3, 9, 2, 7)
max := Max(sortedlist.DescendOrder[int], 5, 3, 9, 2, 7)
fmt.Println(max)
// Output: 2
}

func ExampleMin() {
min := Min(sortedlist.DescendOrder, 5, 3, 9, 2, 7)
min := Min(sortedlist.DescendOrder[int], 5, 3, 9, 2, 7)
fmt.Println(min)
// Output: 9
}
Expand Down Expand Up @@ -87,27 +87,27 @@ func ExampleGreater() {

func ExampleIsIncreasing() {
arr := []int{1, 2, 3, 3, 4}
fmt.Println(IsIncreasing(arr, sortedlist.AscendOrder, false))
fmt.Println(IsIncreasing(arr, sortedlist.AscendOrder, true))
fmt.Println(IsIncreasing(arr, sortedlist.AscendOrder[int], false))
fmt.Println(IsIncreasing(arr, sortedlist.AscendOrder[int], true))
// Output:
// true
// false
}

func ExampleIsDecreasing() {
arr := []int{5, 4, 3, 3, 2}
fmt.Println(IsDecreasing(arr, sortedlist.AscendOrder, false))
fmt.Println(IsDecreasing(arr, sortedlist.AscendOrder, true))
fmt.Println(IsDecreasing(arr, sortedlist.AscendOrder[int], false))
fmt.Println(IsDecreasing(arr, sortedlist.AscendOrder[int], true))
// Output:
// true
// false
}

func ExampleIsMonotonic() {
arr := []int{1, 2, 2, 4, 5}
fmt.Println(IsMonotonic(arr, sortedlist.AscendOrder, false))
fmt.Println(IsMonotonic(arr, sortedlist.AscendOrder, true))
fmt.Println(IsMonotonic(arr, sortedlist.DescendOrder, false))
fmt.Println(IsMonotonic(arr, sortedlist.AscendOrder[int], false))
fmt.Println(IsMonotonic(arr, sortedlist.AscendOrder[int], true))
fmt.Println(IsMonotonic(arr, sortedlist.DescendOrder[int], false))
// Output:
// true
// false
Expand Down
12 changes: 6 additions & 6 deletions typed/functools/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
func TestSort(t *testing.T) {
convey.Convey("simple sort", t, func() {
a := []int{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}
b := Sorted(a, sortedlist.AscendOrder)
Sort(a, sortedlist.AscendOrder)
b := Sorted(a, sortedlist.AscendOrder[int])
Sort(a, sortedlist.AscendOrder[int])
assert.Equal(t, []int{1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9}, a)
assert.Equal(t, a, b)
})
Expand Down Expand Up @@ -57,7 +57,7 @@ func ExampleSort() {

func ExampleSorted() {
arr := []int{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}
arr2 := Sorted(arr, sortedlist.AscendOrder)
arr2 := Sorted(arr, sortedlist.AscendOrder[int])
fmt.Println(arr)
fmt.Println(arr2)
// Output:
Expand All @@ -67,9 +67,9 @@ func ExampleSorted() {

func ExampleIsSorted() {
arr := []int{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}
fmt.Println(IsSorted(arr, sortedlist.AscendOrder))
Sort(arr, sortedlist.AscendOrder)
fmt.Println(IsSorted(arr, sortedlist.AscendOrder))
fmt.Println(IsSorted(arr, sortedlist.AscendOrder[int]))
Sort(arr, sortedlist.AscendOrder[int])
fmt.Println(IsSorted(arr, sortedlist.AscendOrder[int]))
// Output:
// false
// true
Expand Down
22 changes: 18 additions & 4 deletions typed/itertools/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,28 @@ func (z *zipPairIterator[T, U]) Pour() []*zipPair[T, U] {
return result
}

func smaller(a, b int) int {
if a <= b {
return a
}
return b
}

func larger(a, b int) int {
if a >= b {
return a
}
return b
}

// ZipPair creates an iterator for paired elements from two collections of equal length.
func ZipPair[T, U any](entry1 []T, entry2 []U) ZipIterator[T, U] {
return newZipPairIterator(entry1, entry2, min(len(entry1), len(entry2)))
return newZipPairIterator(entry1, entry2, smaller(len(entry1), len(entry2)))
}

// ZipPairLongest creates an iterator for paired elements from two collections of unequal length.
func ZipPairLongest[T, U any](entry1 []T, entry2 []U) ZipIterator[T, U] {
return newZipPairIterator(entry1, entry2, max(len(entry1), len(entry2)))
return newZipPairIterator(entry1, entry2, larger(len(entry1), len(entry2)))
}

// zipListIterator is an iterator for paired elements from multiple collections.
Expand Down Expand Up @@ -161,7 +175,7 @@ func Zip[T any](entries ...[]T) (iterator ListIterator[[]T], err error) {
}
length := len(entries[0])
for i := 1; i < entryCount; i++ {
length = min(length, len(entries[i]))
length = smaller(length, len(entries[i]))
}
iterator = newZipListIterator(entries, length)
return
Expand All @@ -176,7 +190,7 @@ func ZipLongest[T any](entries ...[]T) (iterator ListIterator[[]T], err error) {
}
length := len(entries[0])
for i := 1; i < entryCount; i++ {
length = max(length, len(entries[i]))
length = larger(length, len(entries[i]))
}
iterator = newZipListIterator(entries, length)
return
Expand Down

0 comments on commit 7fa15ea

Please sign in to comment.