File: example_map_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 (50 lines) | stat: -rw-r--r-- 974 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
// 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"
)

type CountConsumer []int

func (c CountConsumer) Slice(i, j int) concurrent.Mapper { return c[i:j] }
func (c CountConsumer) Len() int                         { return len(c) }

func (c CountConsumer) Operation() (r interface{}, err error) {
	var sum int
	for i, v := range c {
		sum += v
		c[i] = 0
	}
	return sum, nil
}

func ExampleMap() {
	c := CountConsumer{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	fmt.Println(c)

	for c.Len() > 1 {
		result, err := concurrent.Map(c, 1, 2)
		if err != nil {
			fmt.Println(err)
		} else {
			fmt.Println(result)
			c = c[:0]
			for _, r := range result {
				c = append(c, r.(int))
			}
		}
	}

	// Output:
	// [1 2 3 4 5 6 7 8 9 10]
	// [3 7 11 15 19]
	// [10 26 19]
	// [36 19]
	// [55]
}