Skip to content

Instantly share code, notes, and snippets.

@Mistobaan
Created June 11, 2012 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mistobaan/2911848 to your computer and use it in GitHub Desktop.
Save Mistobaan/2911848 to your computer and use it in GitHub Desktop.
func buffer(in <-chan T) <-chan T {
out := make(chan T)
go func() {
var buf = list.New()
for {
outc := out
var v T
if buf.Len() == 0 {
// buffer empty: don't try to send on output
if in == nil {
close(out)
return
}
outc = nil
}else{
v = buf.Front().Value.(T)
}
select {
case e := <-in:
if closed(in) {
in = nil
} else {
buf.PushBack(e)
}
case outc <- v:
buf.Remove(buf.Front())
}
}
}()
return out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment