Skip to content

Commit

Permalink
add Iterable creator
Browse files Browse the repository at this point in the history
  • Loading branch information
hsldymq committed Apr 4, 2024
1 parent bdf92c0 commit e9eea8c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion funcs_aggregation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestReduce(t *testing.T) {
}

func TestScan(t *testing.T) {
iterable := NewSliceIterable([]int{1, 2, 3, 4, 5})
iterable := NewIterableFromSlice([]int{1, 2, 3, 4, 5})
e := Scan(iterable, 0, func(acc, each int) int {
return acc + each
})
Expand Down
14 changes: 13 additions & 1 deletion iterable.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import (
"iter"
)

func NewSliceIterable[T any](slice []T) Iterable[T] {
func NewIterableFromSlice[T any](slice []T) Iterable[T] {
return &sliceIterable[T]{s: slice}
}

func NewIterableFromSeq[TIter goiter.SeqX[T], T any](iter TIter) Iterable[T] {
return &seqIterable[TIter, T]{iter: iter}
}

type Iterable[T any] interface {
Iter() iter.Seq[T]
}
Expand All @@ -20,3 +24,11 @@ type sliceIterable[T any] struct {
func (si *sliceIterable[T]) Iter() iter.Seq[T] {
return goiter.SliceElem(si.s).Seq()
}

type seqIterable[TIter goiter.SeqX[T], T any] struct {
iter TIter
}

func (si *seqIterable[TIter, T]) Iter() iter.Seq[T] {
return iter.Seq[T](si.iter)
}

0 comments on commit e9eea8c

Please sign in to comment.