Exemplo n.º 1
0
// 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)
		}
	}
}
Exemplo n.º 2
0
func ExampleSample() {
	stream.Run(
		stream.Numbers(100, 200),
		stream.Sample(4),
		stream.WriteLines(os.Stdout),
	)
	// Output not checked since it is non-deterministic.
}
Exemplo n.º 3
0
func ExampleCommand() {
	stream.Run(
		stream.Numbers(1, 100),
		stream.Command("wc", "-l"),
		stream.WriteLines(os.Stdout),
	)
	// Output:
	// 100
}
Exemplo n.º 4
0
func ExampleXargs() {
	stream.Run(
		stream.Numbers(1, 5),
		stream.Xargs("echo"),
		stream.WriteLines(os.Stdout),
	)
	// Output:
	// 1 2 3 4 5
}
Exemplo n.º 5
0
func ExampleForEach() {
	err := stream.ForEach(stream.Numbers(1, 5), func(s string) {
		fmt.Print(s)
	})
	if err != nil {
		panic(err)
	}
	// Output:
	// 12345
}
Exemplo n.º 6
0
func ExampleLast() {
	stream.Run(
		stream.Numbers(1, 10),
		stream.Last(2),
		stream.WriteLines(os.Stdout),
	)
	// Output:
	// 9
	// 10
}
Exemplo n.º 7
0
func ExampleDropFirst() {
	stream.Run(
		stream.Numbers(1, 10),
		stream.DropFirst(8),
		stream.WriteLines(os.Stdout),
	)
	// Output:
	// 9
	// 10
}
Exemplo n.º 8
0
func ExampleWriteLines() {
	stream.Run(
		stream.Numbers(1, 3),
		stream.WriteLines(os.Stdout),
	)
	// Output:
	// 1
	// 2
	// 3
}
Exemplo n.º 9
0
func ExampleSequence() {
	stream.ForEach(stream.Sequence(
		stream.Numbers(1, 25),
		stream.Grep("3"),
	), func(s string) { fmt.Println(s) })
	// Output:
	// 3
	// 13
	// 23
}
Exemplo n.º 10
0
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
}
Exemplo n.º 11
0
func ExampleNumbers() {
	stream.Run(
		stream.Numbers(2, 5),
		stream.WriteLines(os.Stdout),
	)
	// Output:
	// 2
	// 3
	// 4
	// 5
}
Exemplo n.º 12
0
func ExampleXargsFilter_LimitArgs() {
	stream.Run(
		stream.Numbers(1, 5),
		stream.Xargs("echo").LimitArgs(2),
		stream.WriteLines(os.Stdout),
	)
	// Output:
	// 1 2
	// 3 4
	// 5
}
Exemplo n.º 13
0
func ExampleGrepNot() {
	stream.Run(
		stream.Numbers(1, 12),
		stream.GrepNot("^.$"),
		stream.WriteLines(os.Stdout),
	)
	// Output:
	// 10
	// 11
	// 12
}
Exemplo n.º 14
0
func ExampleFirst() {
	stream.Run(
		stream.Numbers(1, 10),
		stream.First(3),
		stream.WriteLines(os.Stdout),
	)
	// Output:
	// 1
	// 2
	// 3
}
Exemplo n.º 15
0
func ExampleSampleWithSeed() {
	stream.Run(
		stream.Numbers(1, 100),
		stream.SampleWithSeed(2, 100),
		stream.Sort().Num(1),
		stream.WriteLines(os.Stdout),
	)
	// Output:
	// 11
	// 46
}
Exemplo n.º 16
0
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
}
Exemplo n.º 17
0
func ExampleSubstitute() {
	stream.Run(
		stream.Numbers(1, 5),
		stream.Substitute("(3)", "$1$1"),
		stream.WriteLines(os.Stdout),
	)
	// Output:
	// 1
	// 2
	// 33
	// 4
	// 5
}
Exemplo n.º 18
0
func ExampleDropLast() {
	stream.Run(
		stream.Numbers(1, 10),
		stream.DropLast(3),
		stream.WriteLines(os.Stdout),
	)
	// Output:
	// 1
	// 2
	// 3
	// 4
	// 5
	// 6
	// 7
}
Exemplo n.º 19
0
func ExampleContents() {
	out, err := stream.Contents(stream.Numbers(1, 3))
	fmt.Println(out, err)
	// Output:
	// [1 2 3] <nil>
}