Documentation
¶
Overview ¶
Package partition contains methods to help partition slices. Benchmark results show that the ToFunc function is the fastest option available in this package. BenchmarkToChan100-4 300000 4166 ns/op BenchmarkToChan10-4 2000000 840 ns/op BenchmarkToChan0-4 3000000 435 ns/op BenchmarkToFunc100-4 10000000 197 ns/op BenchmarkToFunc10-4 50000000 24.9 ns/op BenchmarkToFunc0-4 500000000 3.05 ns/op BenchmarkToHandler100-4 3000000 590 ns/op BenchmarkToHandler10-4 10000000 127 ns/op BenchmarkToHandler0-4 50000000 38.8 ns/op BenchmarkToIndices10-4 20000000 90.0 ns/op BenchmarkToIndices100-4 3000000 504 ns/op BenchmarkToIndices0-4 300000000 4.98 ns/op
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ToChan ¶
ToChan returns a chan that receives indices for all parts.
Example ¶
package main
import (
"fmt"
"github.com/crosscode-nl/partition"
)
func main() {
a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
for part := range partition.ToChan(len(a), 5) {
fmt.Printf("Part: %v\n", a[part.Low:part.High])
}
}
Output: Part: [1 2 3 4 5] Part: [6 7 8 9]
func ToFunc ¶
func ToFunc(totalLength int, partitionLength int, hf HandleFunc)
ToFunc calls provided func per fragment
Example ¶
package main
import (
"fmt"
"github.com/crosscode-nl/partition"
)
func main() {
a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
partition.ToFunc(len(a), 5, func(l int, h int) {
fmt.Printf("Part: %v\n", a[l:h])
})
}
Output: Part: [1 2 3 4 5] Part: [6 7 8 9]
func ToHandler ¶
ToHandler calls the handler for each part.
Example ¶
package main
import (
"fmt"
"github.com/crosscode-nl/partition"
)
func main() {
a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
s := partition.SliceHandler{}
partition.ToHandler(len(a), 5, &s)
for _, part := range s.Parts {
fmt.Printf("Part: %v\n", a[part.Low:part.High])
}
}
Output: Part: [1 2 3 4 5] Part: [6 7 8 9]
Types ¶
type HandleFunc ¶
HandleFunc type defines the func that is used as a callback in ToFunc
type Handler ¶
Handler interface defines an interface for a partition handler that Partition accepts.
type Indices ¶
type Indices struct {
// Low contains the low index of part
Low int
// High contains the high index of part
High int
}
Indices contains a low and high index of a partition
func ToIndices ¶
ToIndices returns []Indices
Example ¶
package main
import (
"fmt"
"github.com/crosscode-nl/partition"
)
func main() {
a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
for _, part := range partition.ToIndices(len(a), 5) {
fmt.Printf("Part: %v\n", a[part.Low:part.High])
}
}
Output: Part: [1 2 3 4 5] Part: [6 7 8 9]
type SliceHandler ¶
type SliceHandler struct {
Parts []Indices
}
SliceHandler is an implementation of partition.Handler and is a handler that is used internally and for tests and examples.
func (*SliceHandler) Handle ¶
func (h *SliceHandler) Handle(lowIndex int, highIndex int)
Handle implements handling a partition part.