From bf371cf8a90bd8e7939a5746993b79df11591b94 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Thu, 23 Oct 2014 19:25:36 -0400 Subject: [PATCH] implemented Empty in seq --- seq/seq.go | 7 +++++++ seq/seq_test.go | 7 ++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/seq/seq.go b/seq/seq.go index 562bd1d..114c0a9 100644 --- a/seq/seq.go +++ b/seq/seq.go @@ -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 // 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. diff --git a/seq/seq_test.go b/seq/seq_test.go index cd9915f..406a59f 100644 --- a/seq/seq_test.go +++ b/seq/seq_test.go @@ -6,7 +6,7 @@ import ( "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 { intsl := uint64(len(ints)) for i := range ints { @@ -18,8 +18,13 @@ func testSeqGen(t *T, s Seq, ints []types.Elem) Seq { assertValue(ok, true, t) assertValue(first, ints[i], t) + empty := Empty(s) + assertValue(empty, false, t) + s = rest } + empty := Empty(s) + assertValue(empty, true, t) return s }