github.com/puellanivis/breton@v0.2.16/lib/mapreduce/example_test.go (about)

     1  package mapreduce
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestExample(t *testing.T) {
    10  	mapper := MapFunc(func(ctx context.Context, in interface{}) (interface{}, error) {
    11  		letters := in.([]string)
    12  
    13  		var ucLetters []string
    14  
    15  		for _, letter := range letters {
    16  			ucLetters = append(ucLetters, strings.ToUpper(letter))
    17  		}
    18  
    19  		return ucLetters, nil
    20  	})
    21  
    22  	var collection [][]string
    23  
    24  	reducer := ReduceFunc(func(ctx context.Context, in interface{}) error {
    25  		ucLetters := in.([]string)
    26  
    27  		collection = append(collection, ucLetters)
    28  
    29  		return nil
    30  	})
    31  
    32  	mr := New(mapper, reducer)
    33  
    34  	ctx, cancel := context.WithCancel(context.Background())
    35  	defer cancel()
    36  
    37  	exampleInput := strings.Split("abcdefghijklmnopqrstuvwxyz", "")
    38  
    39  	for err := range mr.Run(ctx, exampleInput) {
    40  		t.Error(err)
    41  	}
    42  
    43  	t.Log(collection)
    44  }