github.com/Schaudge/grailbase@v0.0.0-20240223061707-44c758a471c0/traverse/example_test.go (about)

     1  // Copyright 2018 GRAIL, Inc. All rights reserved.
     2  // Use of this source code is governed by the Apache 2.0
     3  // license that can be found in the LICENSE file.
     4  package traverse_test
     5  
     6  import (
     7  	"math/rand"
     8  
     9  	"github.com/Schaudge/grailbase/traverse"
    10  )
    11  
    12  func Example() {
    13  	// Compute N random numbers in parallel.
    14  	const N = 1e5
    15  	out := make([]float64, N)
    16  	_ = traverse.Parallel.Range(len(out), func(start, end int) error {
    17  		r := rand.New(rand.NewSource(rand.Int63()))
    18  		for i := start; i < end; i++ {
    19  			out[i] = r.Float64()
    20  		}
    21  		return nil
    22  	})
    23  }