File: lazy_example_test.go

package info (click to toggle)
golang-github-biogo-biogo 1.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 5,332 kB
  • sloc: sh: 282; makefile: 2
file content (54 lines) | stat: -rw-r--r-- 1,255 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright ©2011-2012 The bíogo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package concurrent_test

import (
	"github.com/biogo/biogo/concurrent"

	"fmt"
)

func ExampleLazily() {
	sentence := "A sentence to be slowly ROT'ed."

	ROT13 := func(b byte) byte {
		c := b & ('a' - 'A')
		i := b&^c - 'A'
		if i < 26 {
			return (i+13)%26 + 'A' | c
		}
		return b
	}

	mutator := concurrent.Lazily(
		func(state ...interface{}) (interface{}, concurrent.State) {
			b, c := []byte(state[0].(string)), state[1].(int)
			b[c] = ROT13(b[c])
			ms := string(b)
			return state[0], concurrent.State{ms, (c + 1) % len(ms)}
		},
		0,           // No lookahead
		nil,         // Perpetual evaluator
		sentence, 0, // Initial state
	)

	var r string
	for i := 0; i < len(sentence)*2; i++ {
		r = mutator().(string)
		if i%10 == 0 {
			fmt.Println(r)
		}
	}
	fmt.Println(r)
	// Output:
	// A sentence to be slowly ROT'ed.
	// N fragrapr to be slowly ROT'ed.
	// N fragrapr gb or fybwly ROT'ed.
	// N fragrapr gb or fybjyl EBG'rq.
	// A sentencr gb or fybjyl EBG'rq.
	// A sentence to be slbjyl EBG'rq.
	// A sentence to be slowly ROT'eq.
	// A sentence to be slowly ROT'ed.
}