// doTest checks that Sample picks items evenly. "n" samples are // drawn from a list of numbers of length "space". This is repeated // "iters" times. The number of times a particular number is drawn // should be within "tolerance" of the expected number. func doTest(t *testing.T, n, space, iters int, tolerance float64) { count := make([]int, space) for i := 0; i < iters; i++ { s := stream.Sequence( stream.Numbers(0, space-1), stream.SampleWithSeed(n, int64(i)), ) stream.ForEach(s, func(s string) { num := -1 // Will cause panic below if Scan fails fmt.Sscan(s, &num) count[num]++ }) } // Check that all counts are approximately equal. expected := (float64(iters) * float64(n)) / float64(space) minExpected := expected * (1.0 - tolerance) maxExpected := expected * (1.0 + tolerance) for i, n := range count { if float64(n) < minExpected || float64(n) > maxExpected { t.Errorf("%d has %d samples; expected range [%f,%f]\n", i, n, minExpected, maxExpected) } } }
func ExampleSample() { stream.Run( stream.Numbers(100, 200), stream.Sample(4), stream.WriteLines(os.Stdout), ) // Output not checked since it is non-deterministic. }
func ExampleCommand() { stream.Run( stream.Numbers(1, 100), stream.Command("wc", "-l"), stream.WriteLines(os.Stdout), ) // Output: // 100 }
func ExampleXargs() { stream.Run( stream.Numbers(1, 5), stream.Xargs("echo"), stream.WriteLines(os.Stdout), ) // Output: // 1 2 3 4 5 }
func ExampleForEach() { err := stream.ForEach(stream.Numbers(1, 5), func(s string) { fmt.Print(s) }) if err != nil { panic(err) } // Output: // 12345 }
func ExampleLast() { stream.Run( stream.Numbers(1, 10), stream.Last(2), stream.WriteLines(os.Stdout), ) // Output: // 9 // 10 }
func ExampleDropFirst() { stream.Run( stream.Numbers(1, 10), stream.DropFirst(8), stream.WriteLines(os.Stdout), ) // Output: // 9 // 10 }
func ExampleWriteLines() { stream.Run( stream.Numbers(1, 3), stream.WriteLines(os.Stdout), ) // Output: // 1 // 2 // 3 }
func ExampleSequence() { stream.ForEach(stream.Sequence( stream.Numbers(1, 25), stream.Grep("3"), ), func(s string) { fmt.Println(s) }) // Output: // 3 // 13 // 23 }
func ExampleIf() { stream.Run( stream.Numbers(1, 12), stream.If(func(s string) bool { return len(s) > 1 }), stream.WriteLines(os.Stdout), ) // Output: // 10 // 11 // 12 }
func ExampleNumbers() { stream.Run( stream.Numbers(2, 5), stream.WriteLines(os.Stdout), ) // Output: // 2 // 3 // 4 // 5 }
func ExampleXargsFilter_LimitArgs() { stream.Run( stream.Numbers(1, 5), stream.Xargs("echo").LimitArgs(2), stream.WriteLines(os.Stdout), ) // Output: // 1 2 // 3 4 // 5 }
func ExampleGrepNot() { stream.Run( stream.Numbers(1, 12), stream.GrepNot("^.$"), stream.WriteLines(os.Stdout), ) // Output: // 10 // 11 // 12 }
func ExampleFirst() { stream.Run( stream.Numbers(1, 10), stream.First(3), stream.WriteLines(os.Stdout), ) // Output: // 1 // 2 // 3 }
func ExampleSampleWithSeed() { stream.Run( stream.Numbers(1, 100), stream.SampleWithSeed(2, 100), stream.Sort().Num(1), stream.WriteLines(os.Stdout), ) // Output: // 11 // 46 }
func ExampleXargs_splitArguments() { // Xargs should split the long list of arguments into // three executions to keep command length below 4096. stream.Run( stream.Numbers(1, 2000), stream.Xargs("echo"), stream.Command("wc", "-l"), stream.WriteLines(os.Stdout), ) // Output: // 3 }
func ExampleSubstitute() { stream.Run( stream.Numbers(1, 5), stream.Substitute("(3)", "$1$1"), stream.WriteLines(os.Stdout), ) // Output: // 1 // 2 // 33 // 4 // 5 }
func ExampleDropLast() { stream.Run( stream.Numbers(1, 10), stream.DropLast(3), stream.WriteLines(os.Stdout), ) // Output: // 1 // 2 // 3 // 4 // 5 // 6 // 7 }
func ExampleContents() { out, err := stream.Contents(stream.Numbers(1, 3)) fmt.Println(out, err) // Output: // [1 2 3] <nil> }