| 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 concurrentscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/concurrent`/ |
| 6 | |
| 7 | // Evaluator is a function for lazy evaluation. |
| 8 | type Evaluatorscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/concurrent`/Evaluator# func(...interface{}) (interface{}, Statescip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/concurrent`/State#) |
| 9 | |
| 10 | type Statescip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/concurrent`/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 Lazilyscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/concurrent`/Lazily().(f Evaluatorscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/concurrent`/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 Statescip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/concurrent`/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 | } |