From ef1a2d0690e535477b758994f00464dfcf615113 Mon Sep 17 00:00:00 2001 From: Bill Burdick Date: Sun, 7 Nov 2010 20:07:14 +0200 Subject: [PATCH] Removed Bleed and changed it to close the channel. Now functions in Seq() have to handle closed channels --- seq/README.md | 3 +-- seq/seq.go | 11 +++-------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/seq/README.md b/seq/README.md index f044a56..3ded2f6 100644 --- a/seq/README.md +++ b/seq/README.md @@ -11,6 +11,7 @@ Sequence supports the following operations: * Seq(f func(c chan Element)) Sequence returns a new Sequence, a function which returns a new channel and runs a goroutine that applies f to the channel and then closes the channel + Note: f must behave properly when the channel is closed from outside. IsEmpty and First close the channel. * From(el... interface{}) Sequence returns a new Sequence of el; to make a Sequence from a vector, vec, use this: From(vec...) * Upto(limit int) Sequence @@ -55,5 +56,3 @@ Sequence supports the following operations: output all of the elements of s to the channel * (s Sequence) Output(c SeqChan) output all of the elements of s to the channel -* Bleed(c SeqChan) - read the remaining elements of c diff --git a/seq/seq.go b/seq/seq.go index f37aa02..4999df7 100644 --- a/seq/seq.go +++ b/seq/seq.go @@ -18,6 +18,7 @@ func IsSeq(s interface{}) bool { return test } +// f must behave properly when the channel is closed, so that IsEmpty and First work properly func Seq(f func(c SeqChan)) Sequence { return func() SeqChan { c := make(SeqChan) @@ -39,15 +40,9 @@ func Output(c SeqChan, el... interface{}) { } } -func Bleed(c SeqChan) { - for !closed(c) { - <- c - } -} - func (s Sequence) First() Element { c := s() - defer Bleed(c) + defer close(c) return <- c } @@ -81,7 +76,7 @@ func (s Sequence) IsEmpty() bool { c := s() <- c result := closed(c) - if !result {defer Bleed(c)} + if !result {defer close(c)} return result } -- 2.11.4.GIT