implemented Empty in seq

This commit is contained in:
Brian Picciano 2014-10-23 19:25:36 -04:00
parent 0520386674
commit bf371cf8a9
2 changed files with 13 additions and 1 deletions

View File

@ -44,6 +44,13 @@ func Size(s Seq) uint64 {
} }
} }
// Returns whether or not the given Seq is empty. This is accomplished using
// FirstRest and NOT just by naively returning Size(s) == 0.
func Empty(s Seq) bool {
_, _, ok := s.FirstRest()
return !ok
}
// Returns the elements in the Seq as a slice. If the underlying Seq has any // Returns the elements in the Seq as a slice. If the underlying Seq has any
// implicit order to it that order will be kept. An empty Seq will return an // implicit order to it that order will be kept. An empty Seq will return an
// empty slice; nil is never returned. In general this completes in O(N) time. // empty slice; nil is never returned. In general this completes in O(N) time.

View File

@ -6,7 +6,7 @@ import (
"github.com/mediocregopher/ginger/types" "github.com/mediocregopher/ginger/types"
) )
// Tests the FirstRest, Size, and ToSlice methods of a Seq // Tests the FirstRest, Size, Empty, and ToSlice methods of a Seq
func testSeqGen(t *T, s Seq, ints []types.Elem) Seq { func testSeqGen(t *T, s Seq, ints []types.Elem) Seq {
intsl := uint64(len(ints)) intsl := uint64(len(ints))
for i := range ints { for i := range ints {
@ -18,8 +18,13 @@ func testSeqGen(t *T, s Seq, ints []types.Elem) Seq {
assertValue(ok, true, t) assertValue(ok, true, t)
assertValue(first, ints[i], t) assertValue(first, ints[i], t)
empty := Empty(s)
assertValue(empty, false, t)
s = rest s = rest
} }
empty := Empty(s)
assertValue(empty, true, t)
return s return s
} }