| 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 |
type CountConsumer []int |
| 14 |
|
| 15 |
func (c CountConsumer) Slice(i, j int) concurrent.Mapper { return c[i:j] } |
| 16 |
func (c CountConsumer) Len() int { return len(c) } |
| 17 |
|
| 18 |
func (c 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 ExampleMap() { |
| 28 |
c := CountConsumer{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} |
| 29 |
fmt.Println(c) |
| 30 |
|
| 31 |
for c.Len() > 1 { |
| 32 |
result, err := concurrent.Map(c, 1, 2) |
| 33 |
if err != nil { |
| 34 |
fmt.Println(err) |
| 35 |
} else { |
| 36 |
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 |
} |