Skip to content

Commit

Permalink
Merge pull request #18 from go-board/feat/iter
Browse files Browse the repository at this point in the history
[iterator] expose iterator/internal as iterator/ops for ordinary usage
  • Loading branch information
leaxoy authored Jul 19, 2022
2 parents afb03a4 + f5e5061 commit 5dd23ce
Show file tree
Hide file tree
Showing 17 changed files with 103 additions and 79 deletions.
4 changes: 4 additions & 0 deletions iterator/adapters/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func (self *sliceIter[T]) Next() optional.Optional[T] {
return optional.None[T]()
}

func (self *sliceIter[T]) SizeHint() (uint, optional.Optional[uint]) {
return 0, optional.Some(uint(len(self.elements)))
}

func OfSlice[T any](eles ...T) iterator.Iterator[T] {
return &sliceIter[T]{elements: eles}
}
32 changes: 10 additions & 22 deletions iterator/internal/arith.go → iterator/ops/arith.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
package internal
package ops

import (
"github.com/go-board/std/cmp"
"github.com/go-board/std/cond"
"github.com/go-board/std/core"
"github.com/go-board/std/iterator"
"github.com/go-board/std/optional"
)

func MaxBy[T any](iter iterator.Iterator[T], ord cmp.OrdFunc[T]) optional.Optional[T] {
return Reduce(iter, func(a, b T) T {
if ord(a, b).IsLe() {
return a
}
return b
})
return Reduce(iter, func(a, b T) T { return cond.Ternary(ord(a, b).IsGe(), a, b) })
}

func MinBy[T any](iter iterator.Iterator[T], ord cmp.OrdFunc[T]) optional.Optional[T] {
return Reduce(iter, func(a, b T) T {
if ord(a, b).IsLe() {
return a
}
return b
})
return Reduce(iter, func(a, b T) T { return cond.Ternary(ord(a, b).IsLe(), a, b) })
}

func SumBy[T any](iter iterator.Iterator[T], sum func(T, T) T) optional.Optional[T] {
Expand All @@ -32,11 +24,7 @@ func ProductBy[T any](iter iterator.Iterator[T], product func(T, T) T) optional.
return Reduce(iter, product)
}

type Numbric interface {
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | uintptr | float32 | float64
}

func ord[T Numbric](lhs, rhs T) cmp.Ordering {
func ord[T core.Ordered](lhs, rhs T) cmp.Ordering {
if lhs > rhs {
return cmp.Greater
} else if lhs < rhs {
Expand All @@ -45,19 +33,19 @@ func ord[T Numbric](lhs, rhs T) cmp.Ordering {
return cmp.Equal
}

func Max[T Numbric](iter iterator.Iterator[T]) optional.Optional[T] {
func Max[T core.Ordered](iter iterator.Iterator[T]) optional.Optional[T] {
return MaxBy(iter, ord[T])
}

func Min[T Numbric](iter iterator.Iterator[T]) optional.Optional[T] {
func Min[T core.Ordered](iter iterator.Iterator[T]) optional.Optional[T] {
return MinBy(iter, ord[T])
}

func Sum[T Numbric](iter iterator.Iterator[T]) optional.Optional[T] {
func Sum[T core.Number](iter iterator.Iterator[T]) optional.Optional[T] {
return SumBy(iter, func(t1 T, t2 T) T { return t1 + t2 })
}

func Product[T Numbric](iter iterator.Iterator[T]) optional.Optional[T] {
func Product[T core.Number](iter iterator.Iterator[T]) optional.Optional[T] {
return ProductBy(iter, func(t1 T, t2 T) T { return t1 * t2 })
}

Expand Down
16 changes: 3 additions & 13 deletions iterator/internal/bool.go → iterator/ops/bool.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package ops

import (
"github.com/go-board/std/cmp"
Expand Down Expand Up @@ -38,21 +38,11 @@ func matchCount[T any](iter iterator.Iterator[T], predicate func(T) bool, n uint
}

func None[T any](iter iterator.Iterator[T], predicate func(T) bool) bool {
for s := iter.Next(); s.IsSome(); s = iter.Next() {
if predicate(s.Value()) {
return false
}
}
return true
return All(iter, func(t T) bool { return !predicate(t) })
}

func ContainsBy[T any](iter iterator.Iterator[T], target T, eq cmp.EqFunc[T]) bool {
for s := iter.Next(); s.IsSome(); s = iter.Next() {
if eq(s.Value(), target) {
return true
}
}
return false
return Any(iter, func(t T) bool { return eq(t, target) })
}

func Contains[T comparable](iter iterator.Iterator[T], target T) bool {
Expand Down
2 changes: 1 addition & 1 deletion iterator/internal/chain.go → iterator/ops/chain.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package ops

import (
"github.com/go-board/std/iterator"
Expand Down
2 changes: 1 addition & 1 deletion iterator/internal/chunk.go → iterator/ops/chunk.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package ops

import (
"github.com/go-board/std/iterator"
Expand Down
2 changes: 1 addition & 1 deletion iterator/internal/clone.go → iterator/ops/clone.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package ops

import (
"github.com/go-board/std/clone"
Expand Down
2 changes: 1 addition & 1 deletion iterator/internal/cmp.go → iterator/ops/cmp.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package ops

import (
"github.com/go-board/std/cmp"
Expand Down
2 changes: 1 addition & 1 deletion iterator/internal/collect.go → iterator/ops/collect.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package ops

import "github.com/go-board/std/iterator"

Expand Down
2 changes: 1 addition & 1 deletion iterator/internal/filter.go → iterator/ops/filter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package ops

import (
"github.com/go-board/std/iterator"
Expand Down
2 changes: 1 addition & 1 deletion iterator/internal/flatten.go → iterator/ops/flatten.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package ops

import (
"github.com/go-board/std/iterator"
Expand Down
2 changes: 1 addition & 1 deletion iterator/internal/fold.go → iterator/ops/fold.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package ops

import (
"github.com/go-board/std/iterator"
Expand Down
2 changes: 1 addition & 1 deletion iterator/internal/for_each.go → iterator/ops/for_each.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package ops

import (
"github.com/go-board/std/iterator"
Expand Down
2 changes: 1 addition & 1 deletion iterator/internal/inspect.go → iterator/ops/inspect.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package ops

import (
"github.com/go-board/std/iterator"
Expand Down
2 changes: 1 addition & 1 deletion iterator/internal/map.go → iterator/ops/map.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package ops

import (
"github.com/go-board/std/iterator"
Expand Down
2 changes: 1 addition & 1 deletion iterator/internal/reduce.go → iterator/ops/reduce.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package ops

import (
"github.com/go-board/std/iterator"
Expand Down
71 changes: 41 additions & 30 deletions iterator/stream/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,119 +3,130 @@ package stream
import (
"github.com/go-board/std/cmp"
"github.com/go-board/std/iterator"
"github.com/go-board/std/iterator/internal"
"github.com/go-board/std/iterator/ops"
"github.com/go-board/std/optional"
)

type streamImpl[T any] struct {
iter iterator.Iterator[T]
}
type streamImpl[T any] struct{ iter iterator.Iterator[T] }

var _ Stream[any] = (*streamImpl[any])(nil)

// FromIterator returns a Stream[T] from an iterator.Iterator[T].
func FromIterator[T any](iter iterator.Iterator[T]) Stream[T] {
return &streamImpl[T]{iter: iter}
}

// FromIterable returns a Stream[T] from an iterator.Iterable[T].
func FromIterable[T any](iterable iterator.Iterable[T]) Stream[T] {
return FromIterator(iterable.Iter())
}

func (self *streamImpl[T]) Iter() iterator.Iterator[T] { return self.iter }

func (self *streamImpl[T]) All(predicate func(T) bool) bool {
return internal.All(self.iter, predicate)
return ops.All(self.iter, predicate)
}

func (self *streamImpl[T]) Any(predicate func(T) bool) bool {
return internal.Any(self.iter, predicate)
return ops.Any(self.iter, predicate)
}

func (self *streamImpl[T]) Once(predicate func(T) bool) bool {
return internal.Once(self.iter, predicate)
return ops.Once(self.iter, predicate)
}

func (self *streamImpl[T]) None(predicate func(T) bool) bool {
return internal.None(self.iter, predicate)
return ops.None(self.iter, predicate)
}

func (self *streamImpl[T]) Chain(o iterator.Iterable[T]) Stream[T] {
return &streamImpl[T]{iter: internal.Chain[T](self.iter, o)}
return &streamImpl[T]{iter: ops.Chain[T](self.iter, o)}
}

func (self *streamImpl[T]) Map(transformer func(T) T) Stream[T] {

return &streamImpl[T]{iter: internal.Map(self.iter, transformer)}
return &streamImpl[T]{iter: ops.Map(self.iter, transformer)}
}

func (self *streamImpl[T]) Filter(predicate func(T) bool) Stream[T] {
return &streamImpl[T]{iter: internal.Filter(self.iter, predicate)}
return &streamImpl[T]{iter: ops.Filter(self.iter, predicate)}
}

func (self *streamImpl[T]) Fold(initial T, accumulator func(T, T) T) T {
return ops.Fold(self.iter, initial, accumulator)
}

func (self *streamImpl[T]) Reduce(reduce func(T, T) T) optional.Optional[T] {
return internal.Reduce(self.iter, reduce)
return ops.Reduce(self.iter, reduce)
}

func (self *streamImpl[T]) Flatten(flatten func(T) iterator.Iterator[T]) Stream[T] {
return &streamImpl[T]{iter: internal.Flatten(self.iter, flatten)}
return &streamImpl[T]{iter: ops.Flatten(self.iter, flatten)}
}

func (self *streamImpl[T]) Chunk(size int) Stream[Stream[T]] {
panic("implement me")
}

func (self *streamImpl[T]) Inspect(inspect func(T)) Stream[T] {
return &streamImpl[T]{iter: internal.Inspect(self.iter, inspect)}
return &streamImpl[T]{iter: ops.Inspect(self.iter, inspect)}
}

func (self *streamImpl[T]) MaxBy(ord cmp.OrdFunc[T]) optional.Optional[T] {
return internal.MaxBy(self.iter, ord)
return ops.MaxBy(self.iter, ord)
}

func (self *streamImpl[T]) MinBy(ord cmp.OrdFunc[T]) optional.Optional[T] {
return internal.MinBy(self.iter, ord)
return ops.MinBy(self.iter, ord)
}

func (self *streamImpl[T]) Equal(o iterator.Iterable[T], ord cmp.EqFunc[T]) bool {
return internal.EqualBy(self.iter, o, ord)
func (self *streamImpl[T]) EqualBy(o iterator.Iterable[T], ord cmp.EqFunc[T]) bool {
return ops.EqualBy(self.iter, o, ord)
}

func (self *streamImpl[T]) Collect() []T {
return internal.Collect(self.iter)
return ops.Collect(self.iter)
}

func (self *streamImpl[T]) Nth(n uint) optional.Optional[T] {
return internal.Nth(self.iter, n)
return ops.Nth(self.iter, n)
}

func (self *streamImpl[T]) Take(n uint) Stream[T] {
return &streamImpl[T]{iter: internal.Take(self.iter, n)}
return &streamImpl[T]{iter: ops.Take(self.iter, n)}
}

func (self *streamImpl[T]) Skip(n uint) Stream[T] {
return &streamImpl[T]{iter: internal.Skip(self.iter, n)}
return &streamImpl[T]{iter: ops.Skip(self.iter, n)}
}

func (self *streamImpl[T]) TakeWhile(predicate func(T) bool) Stream[T] {
return &streamImpl[T]{iter: internal.TakeWhile(self.iter, predicate)}
return &streamImpl[T]{iter: ops.TakeWhile(self.iter, predicate)}
}

func (self *streamImpl[T]) SkipWhile(predicate func(T) bool) Stream[T] {
return &streamImpl[T]{iter: internal.SkipWhile(self.iter, predicate)}
return &streamImpl[T]{iter: ops.SkipWhile(self.iter, predicate)}
}

func (self *streamImpl[T]) ForEach(consumer func(T)) {
internal.ForEach(self.iter, consumer)
ops.ForEach(self.iter, consumer)
}

func (self *streamImpl[T]) StepBy(step uint) Stream[T] {
return &streamImpl[T]{iter: internal.StepBy(self.iter, step)}
return &streamImpl[T]{iter: ops.StepBy(self.iter, step)}
}

func (self *streamImpl[T]) First() optional.Optional[T] {
return self.iter.Next()
}

func (self *streamImpl[T]) Last() optional.Optional[T] {
return internal.Last(self.iter)
return ops.Last(self.iter)
}

func (self *streamImpl[T]) Advancing(step uint) Stream[T] {
return &streamImpl[T]{iter: internal.Advancing(self.iter, step)}
return &streamImpl[T]{iter: ops.Advancing(self.iter, step)}
}

func (self *streamImpl[T]) IsSorted(ord cmp.OrdFunc[T]) bool {
return internal.IsSorted(self.iter, ord)
return ops.IsSorted(self.iter, ord)
}
Loading

0 comments on commit 5dd23ce

Please sign in to comment.