Debian Codegraph @

concurrent/lazy_example_test.go

raw
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_test
6
7 import (
8 "github.com/biogo/biogo/concurrent"
9
10 "fmt"
11 )
12
13 func ExampleLazily() {
14 sentence := "A sentence to be slowly ROT'ed."
15
16 ROT13 := func(b byte) byte {
17 c := b & ('a' - 'A')
18 i := b&^c - 'A'
19 if i < 26 {
20 return (i+13)%26 + 'A' | c
21 }
22 return b
23 }
24
25 mutator := concurrent.Lazily(
26 func(state ...interface{}) (interface{}, concurrent.State) {
27 b, c := []byte(state[0].(string)), state[1].(int)
28 b[c] = ROT13(b[c])
29 ms := string(b)
30 return state[0], concurrent.State{ms, (c + 1) % len(ms)}
31 },
32 0, // No lookahead
33 nil, // Perpetual evaluator
34 sentence, 0, // Initial state
35 )
36
37 var r string
38 for i := 0; i < len(sentence)*2; i++ {
39 r = mutator().(string)
40 if i%10 == 0 {
41 fmt.Println(r)
42 }
43 }
44 fmt.Println(r)
45 // Output:
46 // A sentence to be slowly ROT'ed.
47 // N fragrapr to be slowly ROT'ed.
48 // N fragrapr gb or fybwly ROT'ed.
49 // N fragrapr gb or fybjyl EBG'rq.
50 // A sentencr gb or fybjyl EBG'rq.
51 // A sentence to be slbjyl EBG'rq.
52 // A sentence to be slowly ROT'eq.
53 // A sentence to be slowly ROT'ed.
54 }