Skip to content

Commit eb5504c

Browse files
committed
ref(nav): define local isNil method on sampling-while-iterator (#378)
1 parent 7ce6898 commit eb5504c

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

collections/iterators.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ type IteratorCtrl[T any] interface {
1515
Valid() bool
1616
}
1717

18-
// Iterator represents an iterator over a slice. The underlying slice can
19-
// not be empty. When created, the iterator does not point to a valid
20-
// slice entry. To begin iteration, the client invokes Start. At any one
18+
// Iterator represents an iterator over a slice. The underlying slice may
19+
// be empty. When created, the iterator does not point to a valid
20+
// slice entry. To begin iteration, the client invokes Start. At any stage
2121
// after the iteration has started, the iterator, points to the current item
2222
// in the sequence. The client can query the validity of the current item
2323
// using the Valid method. To obtain successive elements, the client invokes
@@ -42,15 +42,14 @@ type Iterator[T any] interface {
4242
Next() T
4343

4444
// Reset is designed to be used in high frequency applications. The client
45-
// can reuse this entry for a new collection rather that having throw this
45+
// can reuse this iterator for a new collection rather that having to throw this
4646
// instance away and create a new one. This helps to reduce the number of
4747
// allocations in a high frequency application.
4848
Reset(entries []T)
4949
}
5050

5151
// BeginIt creates a forward iterator over a non empty slice. If the provided
52-
// slice is empty, then a nil iterator is returned. (NB: please remember to check
53-
// for a nil interface correctly; see the helper function IsNil in utils).
52+
// slice is empty, then a nil iterator is returned.
5453
//
5554
// The zero value represents the value that is returned if the Next method on the
5655
// iterator is incorrectly invoked after Valid has returned false.

xfs/nav/sampling-while-iterator.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"github.com/samber/lo"
88
"github.com/snivilised/extendio/collections"
9-
"github.com/snivilised/extendio/xfs/utils"
109
)
1110

1211
// WhileDirectoryPredicate determines when to terminate the loop
@@ -104,6 +103,14 @@ func newDirectoryEntryWhileIt(
104103
return whit
105104
}
106105

106+
func (i *directoryEntryWhileIt) isNil() bool {
107+
// 📚 It is ok to use == operator with nil interface here because
108+
// the iterator interface has not been set to anything so the value of the
109+
// interface itself is nil as well as the value it points to.
110+
//
111+
return i.iterator == nil
112+
}
113+
107114
func (i *directoryEntryWhileIt) initInspector(navigator inspector) {
108115
i.navigator = navigator
109116
}
@@ -113,7 +120,7 @@ func (i *directoryEntryWhileIt) withParams(tp *traverseParams) {
113120
}
114121

115122
func (i *directoryEntryWhileIt) start(entries []fs.DirEntry) {
116-
if utils.IsNil(i.iterator) {
123+
if i.isNil() {
117124
i.iterator = lo.TernaryF(i.forward,
118125
func() collections.Iterator[fs.DirEntry] {
119126
return collections.BeginIt[fs.DirEntry](entries, i.zero)

0 commit comments

Comments
 (0)