mirror of
https://github.com/duke-git/lancet.git
synced 2025-09-26 19:41:20 +08:00

Feature Reset allows for the iteration process over a sequence to be restarted from the beginning. It enables reusing the iterator for multiple traversals without needing to recreate it. Refactoring It is a idiomatic practice to design functions and methods to return concrete struct types. This approach promotes flexibility and decoupling, allowing the calling code to work with any implementation that satisfies the interface
84 lines
2.3 KiB
Go
84 lines
2.3 KiB
Go
// Copyright 2022 dudaodong@gmail.com. All rights resulterved.
|
|
// Use of this source code is governed by MIT license
|
|
|
|
// Package iterator provides a way to iterate over values stored in containers.
|
|
// note:
|
|
// 1. Full feature iterator is complicated, this package is just a experiment to explore how iterators could work in Go.
|
|
// 2. The functionality of this package is very simple and limited, may not meet the actual dev needs.
|
|
// 3. It is currently under development, unstable, and will not be completed for some time in the future.
|
|
// So, based on above factors, you may not use it in production. but, anyone is welcome to improve it.
|
|
// Hope that Go can support iterator in future. see https://github.com/golang/go/discussions/54245 and https://github.com/golang/go/discussions/56413
|
|
package iterator
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/duke-git/lancet/v2/internal"
|
|
)
|
|
|
|
func TestMapIterator(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
assert := internal.NewAssert(t, "TestMapIterator")
|
|
|
|
var iter Iterator[int] = FromSlice([]int{1, 2, 3, 4})
|
|
|
|
iter = Map(iter, func(n int) int { return n / 2 })
|
|
|
|
result := ToSlice(iter)
|
|
assert.Equal([]int{0, 1, 1, 2}, result)
|
|
}
|
|
|
|
func TestFilterIterator(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
assert := internal.NewAssert(t, "TestFilterIterator")
|
|
|
|
var iter Iterator[int] = FromSlice([]int{1, 2, 3, 4})
|
|
|
|
iter = Filter(iter, func(n int) bool { return n < 3 })
|
|
|
|
result := ToSlice(iter)
|
|
assert.Equal([]int{1, 2}, result)
|
|
}
|
|
|
|
func TestJoinIterator(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
assert := internal.NewAssert(t, "TestJoinIterator")
|
|
|
|
var iter1 Iterator[int] = FromSlice([]int{1, 2})
|
|
var iter2 Iterator[int] = FromSlice([]int{3, 4})
|
|
|
|
var iter Iterator[int] = Join(iter1, iter2)
|
|
|
|
item, ok := iter.Next()
|
|
assert.Equal(1, item)
|
|
assert.Equal(true, ok)
|
|
|
|
assert.Equal([]int{2, 3, 4}, ToSlice(iter))
|
|
}
|
|
|
|
func TestReduce(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
assert := internal.NewAssert(t, "TestReduce")
|
|
|
|
var iter Iterator[int] = FromSlice([]int{1, 2, 3, 4})
|
|
sum := Reduce(iter, 0, func(a, b int) int { return a + b })
|
|
assert.Equal(10, sum)
|
|
}
|
|
|
|
func TestTakeIterator(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
assert := internal.NewAssert(t, "TestTakeIterator")
|
|
|
|
var iter Iterator[int] = FromSlice([]int{1, 2, 3, 4, 5})
|
|
|
|
iter = Take(iter, 3)
|
|
|
|
result := ToSlice(iter)
|
|
assert.Equal([]int{1, 2, 3}, result)
|
|
}
|