Skip to content

Commit 6f7779a

Browse files
committed
Improve documentation for iter package
1 parent 982e9b2 commit 6f7779a

File tree

15 files changed

+205
-141
lines changed

15 files changed

+205
-141
lines changed

iter/chain.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type ChainIter[T any] struct {
88
iteratorIndex int
99
}
1010

11-
// Chain instantiates a [ChainIter] that will yield all items in the provided
11+
// Chain instantiates a [*ChainIter] that will yield all items in the provided
1212
// iterators to exhaustion first to last.
1313
func Chain[T any](iterators ...Iterator[T]) *ChainIter[T] {
1414
return &ChainIter[T]{iterators, 0}

iter/channel.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ type ChannelIter[T any] struct {
77
item chan T
88
}
99

10-
// FromChannel instantiates a [ChannelIter] that will yield each value from the
11-
// provided channel.
10+
// FromChannel instantiates a [*ChannelIter] that will yield each value from
11+
// the provided channel.
1212
func FromChannel[T any](ch chan T) *ChannelIter[T] {
1313
return &ChannelIter[T]{ch}
1414
}

iter/counter.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,34 @@ package iter
22

33
import "github.com/BooleanCat/go-functional/option"
44

5-
// CountIter implements `Count`. See `Count`'s documentation.
5+
// CountIter iterator, see [Count].
66
type CountIter struct {
77
index int
88
}
99

10-
// Count instantiates a `CountIter` that will iterate over 0 and the
10+
// Count instantiates a [*CountIter] that will iterate over 0 and the
1111
// natural numbers. Count is functionally "unlimited" although it does not
1212
// protect against the integer limit.
1313
func Count() *CountIter {
1414
return new(CountIter)
1515
}
1616

17-
// Next implements the Iterator interface for `CountIter`.
17+
// Next implements the [Iterator] interface.
1818
func (c *CountIter) Next() option.Option[int] {
1919
c.index++
2020
return option.Some(c.index - 1)
2121
}
2222

2323
var _ Iterator[int] = new(CountIter)
2424

25-
// Drop is an alternative way of invoking Drop(iter)
25+
// Drop is a convenience method for [Drop], providing this iterator as an
26+
// argument.
2627
func (c *CountIter) Drop(n uint) *DropIter[int] {
2728
return Drop[int](c, n)
2829
}
2930

30-
// Take is an alternative way of invoking Take(iter)
31+
// Take is a convenience method for [Take], providing this iterator as an
32+
// argument.
3133
func (iter *CountIter) Take(n uint) *TakeIter[int] {
3234
return Take[int](iter, n)
3335
}

iter/cycle.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ package iter
22

33
import "github.com/BooleanCat/go-functional/option"
44

5-
// CycleIter implements `Cycle`. See `Cycle`'s documentation.
5+
// CycleIter iterator, see [Cycle].
66
type CycleIter[T any] struct {
77
iter Iterator[T]
88
items []T
99
index int
1010
}
1111

12-
// Cycle instantiates a `CycleIter` yielding all items from the provided
12+
// Cycle instantiates a [*CycleIter] yielding all items from the provided
1313
// iterator until exhaustion, and then yields them all over again on repeat.
1414
//
15-
// Note that CycleIter stores the members from the underlying iterator so will
16-
// grow in size as items are yielded until the underlying iterator is
15+
// Note that [CycleIter] stores the members from the underlying iterator so
16+
// will grow in size as items are yielded until the underlying iterator is
1717
// exhausted.
1818
//
1919
// In most cases this iterator is infinite, except when the underlying iterator
@@ -23,7 +23,7 @@ func Cycle[T any](iter Iterator[T]) *CycleIter[T] {
2323
return &CycleIter[T]{iter, make([]T, 0), 0}
2424
}
2525

26-
// Next implements the Iterator interface for `CycleIter`.
26+
// Next implements the [Iterator] interface.
2727
func (iter *CycleIter[T]) Next() option.Option[T] {
2828
if iter.iter != nil {
2929
if value, ok := iter.iter.Next().Value(); ok {
@@ -49,12 +49,14 @@ func (iter *CycleIter[T]) Next() option.Option[T] {
4949

5050
var _ Iterator[struct{}] = new(CycleIter[struct{}])
5151

52-
// Drop is an alternative way of invoking Drop(iter)
52+
// Drop is a convenience method for [Drop], providing this iterator as an
53+
// argument.
5354
func (iter *CycleIter[T]) Drop(n uint) *DropIter[T] {
5455
return Drop[T](iter, n)
5556
}
5657

57-
// Take is an alternative way of invoking Take(iter)
58+
// Take is a convenience method for [Take], providing this iterator as an
59+
// argument.
5860
func (iter *CycleIter[T]) Take(n uint) *TakeIter[T] {
5961
return Take[T](iter, n)
6062
}

iter/drop.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ package iter
22

33
import "github.com/BooleanCat/go-functional/option"
44

5-
// DropIter implements `Drop`. See `Drop`'s documentation.
5+
// DropIter iterator, see [Drop].
66
type DropIter[T any] struct {
77
iter Iterator[T]
88
count uint
99
dropped bool
1010
exhausted bool
1111
}
1212

13-
// Drop instantiates a `DropIter` that will skip the number of items of its
13+
// Drop instantiates a [*DropIter] that will skip the number of items of its
1414
// wrapped iterator by the provided count.
1515
func Drop[T any](iter Iterator[T], count uint) *DropIter[T] {
1616
return &DropIter[T]{iter, count, false, false}
1717
}
1818

19-
// Next implements the Iterator interface for `DropIter`.
19+
// Next implements the [Iterator] interface.
2020
func (iter *DropIter[T]) Next() option.Option[T] {
2121
if iter.exhausted {
2222
return option.None[T]()
@@ -46,17 +46,20 @@ func (iter *DropIter[T]) delegateNext() option.Option[T] {
4646

4747
var _ Iterator[struct{}] = new(DropIter[struct{}])
4848

49-
// Collect is an alternative way of invoking Collect(iter)
49+
// Collect is a convenience method for [Collect], providing this iterator as
50+
// an argument.
5051
func (iter *DropIter[T]) Collect() []T {
5152
return Collect[T](iter)
5253
}
5354

54-
// Drop is an alternative way of invoking Drop(iter)
55+
// Drop is a convenience method for [Drop], providing this iterator as an
56+
// argument.
5557
func (iter *DropIter[T]) Drop(n uint) *DropIter[T] {
5658
return Drop[T](iter, n)
5759
}
5860

59-
// Take is an alternative way of invoking Take(iter)
61+
// Take is a convenience method for [Take], providing this iterator as an
62+
// argument.
6063
func (iter *DropIter[T]) Take(n uint) *TakeIter[T] {
6164
return Take[T](iter, n)
6265
}

iter/exhausted.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,36 @@ package iter
22

33
import "github.com/BooleanCat/go-functional/option"
44

5-
// ExhaustedIter implements `Exhausted`. See `Exhausted`'s documentation.
5+
// ExhaustedIter iterator, see [Exhausted].
66
type ExhaustedIter[T any] struct{}
77

8-
// Exhausted instantiates an `ExhaustedIter` that will immediately be exhausted
9-
// (`Next` will always return a `None` variant).
8+
// Exhausted instantiates an [*ExhaustedIter] that will immediately be
9+
// exhausted (Next will always return a None variant).
1010
func Exhausted[T any]() *ExhaustedIter[T] {
1111
return new(ExhaustedIter[T])
1212
}
1313

14-
// Next implements the Iterator interface for `ExhaustedIter`.
14+
// Next implements the [Iterator] interface.
1515
func (iter *ExhaustedIter[T]) Next() option.Option[T] {
1616
return option.None[T]()
1717
}
1818

1919
var _ Iterator[struct{}] = new(ExhaustedIter[struct{}])
2020

21-
// Collect is an alternative way of invoking Collect(iter)
21+
// Collect is a convenience method for [Collect], providing this iterator as
22+
// an argument.
2223
func (iter *ExhaustedIter[T]) Collect() []T {
2324
return Collect[T](iter)
2425
}
2526

26-
// Drop is an alternative way of invoking Drop(iter)
27+
// Drop is a convenience method for [Drop], providing this iterator as an
28+
// argument.
2729
func (iter *ExhaustedIter[T]) Drop(n uint) *DropIter[T] {
2830
return Drop[T](iter, n)
2931
}
3032

31-
// Take is an alternative way of invoking Take(iter)
33+
// Take is a convenience method for [Take], providing this iterator as an
34+
// argument.
3235
func (iter *ExhaustedIter[T]) Take(n uint) *TakeIter[T] {
3336
return Take[T](iter, n)
3437
}

iter/filter.go

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ package iter
22

33
import "github.com/BooleanCat/go-functional/option"
44

5-
// FilterIter implements `Filter`. See `Filter`'s documentation.
5+
// FilterIter iterator, see [Filter].
66
type FilterIter[T any] struct {
77
iter Iterator[T]
88
fun func(T) bool
99
exhausted bool
1010
}
1111

12-
// Filter instantiates a `FilterIter` that selectively yields only results that
13-
// cause the provided function to return `true`.
12+
// Filter instantiates a [*FilterIter] that selectively yields only results
13+
// that cause the provided function to return `true`.
1414
func Filter[T any](iter Iterator[T], fun func(T) bool) *FilterIter[T] {
1515
return &FilterIter[T]{iter, fun, false}
1616
}
1717

18-
// Next implements the Iterator interface for `Filter`.
18+
// Next implements the [Iterator] interface.
1919
func (iter *FilterIter[T]) Next() option.Option[T] {
2020
if iter.exhausted {
2121
return option.None[T]()
@@ -36,36 +36,39 @@ func (iter *FilterIter[T]) Next() option.Option[T] {
3636

3737
var _ Iterator[struct{}] = new(FilterIter[struct{}])
3838

39-
// Collect is an alternative way of invoking Collect(iter)
39+
// Collect is a convenience method for [Collect], providing this iterator as
40+
// an argument.
4041
func (iter *FilterIter[T]) Collect() []T {
4142
return Collect[T](iter)
4243
}
4344

44-
// Drop is an alternative way of invoking Drop(iter)
45+
// Drop is a convenience method for [Drop], providing this iterator as an
46+
// argument.
4547
func (iter *FilterIter[T]) Drop(n uint) *DropIter[T] {
4648
return Drop[T](iter, n)
4749
}
4850

49-
// Take is an alternative way of invoking Take(iter)
51+
// Take is a convenience method for [Take], providing this iterator as an
52+
// argument.
5053
func (iter *FilterIter[T]) Take(n uint) *TakeIter[T] {
5154
return Take[T](iter, n)
5255
}
5356

54-
// Exclude instantiates a `FilterIter` that selectively yields only results that
55-
// cause the provided function to return `false`.
57+
// Exclude instantiates a [*FilterIter] that selectively yields only results
58+
// that cause the provided function to return `false`.
5659
func Exclude[T any](iter Iterator[T], fun func(T) bool) *FilterIter[T] {
5760
inverse := func(t T) bool { return !fun(t) }
5861
return &FilterIter[T]{iter, inverse, false}
5962
}
6063

61-
// FilterMapIter implements `FilterMap`. See `FilterMap`'s documentation.
64+
// FilterMapIter iterator, see [FilterMap].
6265
type FilterMapIter[T any, U any] struct {
6366
iter Iterator[T]
6467
fn func(T) option.Option[U]
6568
exhausted bool
6669
}
6770

68-
// Next implements the Iterator interface for `FilterMapIter`.
71+
// Next implements the [Iterator] interface.
6972
func (iter *FilterMapIter[T, U]) Next() option.Option[U] {
7073
if iter.exhausted {
7174
return option.None[U]()
@@ -87,24 +90,27 @@ func (iter *FilterMapIter[T, U]) Next() option.Option[U] {
8790

8891
var _ Iterator[struct{}] = new(FilterMapIter[struct{}, struct{}])
8992

90-
// Accepts an underlying iterator as data source and a filtering + mapping function
91-
// it allows the user to filter elements by returning a None variant and to transform
92-
// elements by returning a Some variant.
93+
// FilterMap instantiates a [*FilterMapIter] that selectively yields only
94+
// results that cause the provided function to return `true` with a map
95+
// operation performed on them.
9396
func FilterMap[T any, U any](itr Iterator[T], fun func(T) option.Option[U]) *FilterMapIter[T, U] {
9497
return &FilterMapIter[T, U]{itr, fun, false}
9598
}
9699

97-
// Collect is an alternative way of invoking Collect(iter)
100+
// Collect is a convenience method for [Collect], providing this iterator as
101+
// an argument.
98102
func (iter *FilterMapIter[T, U]) Collect() []U {
99103
return Collect[U](iter)
100104
}
101105

102-
// Drop is an alternative way of invoking Drop(iter)
106+
// Drop is a convenience method for [Drop], providing this iterator as an
107+
// argument.
103108
func (iter *FilterMapIter[T, U]) Drop(n uint) *DropIter[U] {
104109
return Drop[U](iter, n)
105110
}
106111

107-
// Take is an alternative way of invoking Take(iter)
112+
// Take is a convenience method for [Take], providing this iterator as an
113+
// argument.
108114
func (iter *FilterMapIter[T, U]) Take(n uint) *TakeIter[U] {
109115
return Take[U](iter, n)
110116
}

iter/iter.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ import "github.com/BooleanCat/go-functional/option"
44

55
// Iterator declares that each Iterator must implement a Next method.
66
// Successive calls to the next method shall return the next item in the
7-
// Iterator, wrapped in an `Option.Some` variant.
7+
// Iterator, wrapped in an [option.Some] variant.
88
//
9-
// Exhausted Iterators shall return a `None` variant of `Option` on every
10-
// subsequent call.
9+
// Exhausted Iterators shall return a [option.None] variant on every subsequent
10+
// call.
1111
type Iterator[T any] interface {
1212
Next() option.Option[T]
1313
}
1414

15-
// Collect consumes an Iterator and returns all remaining items within a slice.
16-
// It does not protect against infinite Iterators.
15+
// Collect consumes an [Iterator] and returns all remaining items within a
16+
// slice. It does not protect against infinite Iterators.
1717
func Collect[T any](iter Iterator[T]) []T {
1818
items := make([]T, 0)
1919

@@ -26,10 +26,10 @@ func Collect[T any](iter Iterator[T]) []T {
2626
}
2727
}
2828

29-
// Fold consumes an Iterator and returns the final result of applying the accumulator function to each element.
30-
// The accumulator function accepts two arguments - an accumulator and an element and returns a new accumulator.
31-
// The initial value is the accumulator for the first call.
32-
// Fold does not protect against infinite Iterators.
29+
// Fold consumes an [Iterator] and returns the final result of applying the
30+
// accumulator function to each element. The accumulator function accepts two
31+
// arguments - an accumulator and an initial value and returns a new value for
32+
// the next accumulation. Fold does not protect against infinite Iterators.
3333
func Fold[T any, U any](iter Iterator[T], initial U, biop func(U, T) U) U {
3434
for {
3535
if value, ok := iter.Next().Value(); ok {
@@ -40,9 +40,9 @@ func Fold[T any, U any](iter Iterator[T], initial U, biop func(U, T) U) U {
4040
}
4141
}
4242

43-
// ToChannel consumes an iterator and returns a channel that will receive all
44-
// values from the provided iterator. The channel is closed once the iterator
45-
// is exhausted.
43+
// ToChannel consumes an [Iterator] and returns a channel that will receive all
44+
// values from the provided [Iterator]. The channel is closed once the
45+
// [Iterator] is exhausted.
4646
func ToChannel[T any](iter Iterator[T]) chan T {
4747
ch := make(chan T)
4848

@@ -61,7 +61,7 @@ func ToChannel[T any](iter Iterator[T]) chan T {
6161
return ch
6262
}
6363

64-
// ForEach consumes an iterator and executes callback function on each item.
64+
// ForEach consumes an [Iterator] and executes callback function on each item.
6565
func ForEach[T any](iter Iterator[T], callback func(T)) {
6666
for {
6767
if value, ok := iter.Next().Value(); ok {
@@ -73,7 +73,7 @@ func ForEach[T any](iter Iterator[T], callback func(T)) {
7373
}
7474

7575
// Find the first occurrence of a value that satisfies the predicate and return
76-
// that value. If no value satisfies the predicate, return `None`.
76+
// that value. If no value satisfies the predicate, return [option.None].
7777
func Find[T any](iter Iterator[T], predicate func(v T) bool) option.Option[T] {
7878
for {
7979
if value, ok := iter.Next().Value(); ok {

0 commit comments

Comments
 (0)