Debian Codegraph @

concurrent/lazy.go

raw symbols SCIP
1 // Copyright ©2011-2012 The bíogo Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package concurrent
6
7 // Evaluator is a function for lazy evaluation.
8 type Evaluator func(...interface{}) (interface{}, State)
9
10 type State []interface{}
11
12 // Lazily is function to generate a lazy evaluator.
13 //
14 // Lazy functions are terminated by closing the reaper channel. nil should be passed as
15 // a reaper for perpetual lazy functions.
16 func Lazily(f Evaluator, lookahead int, reaper <-chan struct{}, init ...interface{}) func() interface{} {
17 rc := make(chan interface{}, lookahead)
18 go func(rc chan interface{}) {
19 defer close(rc)
20 var state State = init
21 var result interface{}
22
23 for {
24 result, state = f(state...)
25 select {
26 case rc <- result:
27 case <-reaper:
28 return
29 }
30 }
31 }(rc)
32
33 return func() interface{} {
34 return <-rc
35 }
36 }