| 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_testscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/concurrent_test`/ |
| 6 | |
| 7 | import ( |
| 8 | "github.com/biogo/biogo/concurrentscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/concurrent`/" |
| 9 | |
| 10 | "fmtscip-go gomod github.com/golang/go/src go1.14 fmt/" |
| 11 | ) |
| 12 | |
| 13 | type CountConsumerscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/concurrent_test`/CountConsumer# []int |
| 14 | |
| 15 | func (c CountConsumerscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/concurrent_test`/CountConsumer#) Slicescip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/concurrent_test`/CountConsumer#Slice().(i, j int) concurrentscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/concurrent`/.Mapperscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/concurrent`/Mapper# { return c[i:j] } |
| 16 | func (c CountConsumerscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/concurrent_test`/CountConsumer#) Lenscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/concurrent_test`/CountConsumer#Len().() int { return len(c) } |
| 17 | |
| 18 | func (c CountConsumerscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/concurrent_test`/CountConsumer#) Operationscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/concurrent_test`/CountConsumer#Operation().() (r interface{}, err error) { |
| 19 | var sum int |
| 20 | for i, v := range c { |
| 21 | sum += v |
| 22 | c[i] = 0 |
| 23 | } |
| 24 | return sum, nil |
| 25 | } |
| 26 | |
| 27 | func ExampleMapscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/concurrent_test`/ExampleMap().() { |
| 28 | c := CountConsumerscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/concurrent_test`/CountConsumer#{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} |
| 29 | fmtscip-go gomod github.com/golang/go/src go1.14 fmt/.Printlnscip-go gomod github.com/golang/go/src go1.14 fmt/Println().(c) |
| 30 | |
| 31 | for c.Lenscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/concurrent_test`/CountConsumer#Len().() > 1 { |
| 32 | result, err := concurrentscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/concurrent`/.Mapscip-go gomod github.com/biogo/biogo . `github.com/biogo/biogo/concurrent`/Map().(c, 1, 2) |
| 33 | if err != nil { |
| 34 | fmtscip-go gomod github.com/golang/go/src go1.14 fmt/.Printlnscip-go gomod github.com/golang/go/src go1.14 fmt/Println().(err) |
| 35 | } else { |
| 36 | fmtscip-go gomod github.com/golang/go/src go1.14 fmt/.Printlnscip-go gomod github.com/golang/go/src go1.14 fmt/Println().(result) |
| 37 | c = c[:0] |
| 38 | for _, r := range result { |
| 39 | c = append(c, r.(int)) |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // Output: |
| 45 | // [1 2 3 4 5 6 7 8 9 10] |
| 46 | // [3 7 11 15 19] |
| 47 | // [10 26 19] |
| 48 | // [36 19] |
| 49 | // [55] |
| 50 | } |