File: fibonacci.go

package info (click to toggle)
golang-github-marstr-collection 0.3.3%2Bgit20171004.e631537-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 152 kB
  • sloc: makefile: 2
file content (26 lines) | stat: -rw-r--r-- 472 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package collection

type fibonacciGenerator struct{}

// Fibonacci is an Enumerable which will dynamically generate the fibonacci sequence.
var Fibonacci Enumerable = fibonacciGenerator{}

func (gen fibonacciGenerator) Enumerate(cancel <-chan struct{}) Enumerator {
	retval := make(chan interface{})

	go func() {
		defer close(retval)
		a, b := 0, 1

		for {
			select {
			case retval <- a:
				a, b = b, a+b
			case <-cancel:
				return
			}
		}
	}()

	return retval
}