Skip to content

Commit

Permalink
Merge pull request #10 from DmitriyMV/remove-pull
Browse files Browse the repository at this point in the history
remove `iter.Pull` and `iter.Pull2` usages
  • Loading branch information
vyskocilm authored Feb 28, 2024
2 parents 4ed681d + 6064a85 commit f40661c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 93 deletions.
70 changes: 17 additions & 53 deletions it.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,11 @@ func FromSlice[T any](slice []T) iter.Seq[T] {
// Filter yields only values for which filterFunc returns true
func Filter[T any](s iter.Seq[T], filterFunc FilterFunc[T]) iter.Seq[T] {
return func(yield func(T) bool) {
next, stop := iter.Pull(s)
defer stop()

for {
t, ok := next()
if !ok {
return
}
if shouldYield := filterFunc(t); !shouldYield {
for v := range s {
if shouldYield := filterFunc(v); !shouldYield {
continue
}
if !yield(t) {
if !yield(v) {
return
}
}
Expand All @@ -61,15 +54,8 @@ func Filter[T any](s iter.Seq[T], filterFunc FilterFunc[T]) iter.Seq[T] {
func IndexFrom[T any](seq iter.Seq[T], initial int) iter.Seq2[int, T] {
index := initial
return func(yield func(int, T) bool) {
next, stop := iter.Pull(seq)
defer stop()

for {
t, ok := next()
if !ok {
return
}
if !yield(index, t) {
for v := range seq {
if !yield(index, v) {
return
}
index++
Expand All @@ -86,15 +72,8 @@ func Index[T any](seq iter.Seq[T]) iter.Seq2[int, T] {
// Map calls a mapping function on each member of the sequence
func Map[T, V any](s iter.Seq[T], mapFunc MapFunc[T, V]) iter.Seq[V] {
return func(yield func(V) bool) {
next, stop := iter.Pull(s)
defer stop()

for {
t, ok := next()
if !ok {
return
}
v := mapFunc(t)
for v := range s {
v := mapFunc(v)
if !yield(v) {
return
}
Expand All @@ -106,15 +85,8 @@ func Map[T, V any](s iter.Seq[T], mapFunc MapFunc[T, V]) iter.Seq[V] {
// and a typical use is return a mapped type together with an error
func MapSeq2[T, K, V any](seq iter.Seq[T], mapFunc MapSeq2Func[T, K, V]) iter.Seq2[K, V] {
return func(yield func(K, V) bool) {
next, stop := iter.Pull(seq)
defer stop()

for {
t, ok := next()
if !ok {
return
}
k, v := mapFunc(t)
for v := range seq {
k, v := mapFunc(v)
if !yield(k, v) {
return
}
Expand All @@ -134,16 +106,12 @@ func MapError[T, V any](seq iter.Seq[T], mapFunc MapErrorFunc[T, V]) iter.Seq2[V

func Reduce[T any](s iter.Seq[T], reduceFunc ReduceFunc[T], initial T) T {
ret := initial
next, stop := iter.Pull(s)
defer stop()

for {
t, ok := next()
if !ok {
return ret
}
ret = reduceFunc(ret, t)
for v := range s {
ret = reduceFunc(ret, v)
}

return ret
}

func Reverse[T any](s iter.Seq[T]) iter.Seq[T] {
Expand All @@ -169,14 +137,10 @@ func Sort[T any](s iter.Seq[T], sortFunc SortFunc[T]) iter.Seq[T] {
// TODO: provide IntoScile(slice []T, seq iter.Seq[T])?
func AsSlice[T any](seq iter.Seq[T]) []T {
ret := make([]T, 0, 1024)
next, stop := iter.Pull(seq)
defer stop()

for {
t, ok := next()
if !ok {
return ret
}
ret = append(ret, t)
for v := range seq {
ret = append(ret, v)
}

return ret
}
47 changes: 7 additions & 40 deletions it2.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,7 @@ func From2Slice[K comparable, V any](m map[K]V) iter.Seq2[K, V] {

func Filter2[K, V any](seq iter.Seq2[K, V], filterFunc Filter2Func[K, V]) iter.Seq2[K, V] {
return func(yield func(K, V) bool) {
next, stop := iter.Pull2(seq)
defer stop()

for {
k, v, ok := next()
if !ok {
return
}
for k, v := range seq {
if shouldYield := filterFunc(k, v); !shouldYield {
continue
}
Expand All @@ -42,14 +35,7 @@ func Filter2[K, V any](seq iter.Seq2[K, V], filterFunc Filter2Func[K, V]) iter.S

func Map2[K, V, K2, V2 any](seq iter.Seq2[K, V], mapFunc Map2Func[K, V, K2, V2]) iter.Seq2[K2, V2] {
return func(yield func(K2, V2) bool) {
next, stop := iter.Pull2(seq)
defer stop()

for {
k, v, ok := next()
if !ok {
return
}
for k, v := range seq {
k2, v2 := mapFunc(k, v)
if !yield(k2, v2) {
return
Expand All @@ -60,14 +46,7 @@ func Map2[K, V, K2, V2 any](seq iter.Seq2[K, V], mapFunc Map2Func[K, V, K2, V2])

func Keys[K any, V any](seq iter.Seq2[K, V]) iter.Seq[K] {
return func(yield func(K) bool) {
next, stop := iter.Pull2(seq)
defer stop()

for {
k, _, ok := next()
if !ok {
return
}
for k, _ := range seq {
if !yield(k) {
return
}
Expand All @@ -77,14 +56,7 @@ func Keys[K any, V any](seq iter.Seq2[K, V]) iter.Seq[K] {

func Values[K any, V any](seq iter.Seq2[K, V]) iter.Seq[V] {
return func(yield func(V) bool) {
next, stop := iter.Pull2(seq)
defer stop()

for {
_, v, ok := next()
if !ok {
return
}
for _, v := range seq {
if !yield(v) {
return
}
Expand Down Expand Up @@ -116,14 +88,9 @@ func Sort2[K comparable, V any](seq iter.Seq2[K, V], sortFunc SortFunc[K]) iter.
// AsMap converts the iter.Seq2 into map
func AsMap[K comparable, V any](seq iter.Seq2[K, V]) map[K]V {
ret := make(map[K]V, 1024)
next, stop := iter.Pull2(seq)
defer stop()

for {
k, v, ok := next()
if !ok {
return ret
}
for k, v := range seq {
ret[k] = v
}

return ret
}

0 comments on commit f40661c

Please sign in to comment.