MCPcopy Create free account
hub / github.com/butuzov/harmony / ExampleQueueWithContext

Function ExampleQueueWithContext

examples_test.go:185–214  ·  view source on GitHub ↗

Generate fibonacci sequence

()

Source from the content-addressed store, hash-verified

183
184// Generate fibonacci sequence
185func ExampleQueueWithContext() {
186 // fin returns function that returns Fibonacci sequence up to n element,
187 // it returns 0 after limit reached.
188 fib := func(limit int) func() int {
189 a, b, nTh := 0, 1, 1
190 return func() int {
191 if nTh > limit {
192 return 0
193 }
194
195 nTh++
196 a, b = b, a+b
197 return a
198 }
199 }
200
201 first10FibNumbers := make([]int, 10)
202 incoming, err := harmony.QueueWithContext(context.Background(), fib(10))
203 if err != nil {
204 log.Printf("err: %v", err)
205 return
206 }
207
208 for i := 0; i < cap(first10FibNumbers); i++ {
209 first10FibNumbers[i] = <-incoming
210 }
211
212 fmt.Println(first10FibNumbers)
213 // Output: [1 1 2 3 5 8 13 21 34 55]
214}
215
216func ExampleTeeWithDone() {
217 done := make(chan struct{})

Callers

nothing calls this directly

Calls 1

QueueWithContextFunction · 0.92

Tested by

no test coverage detected