gopart

package module
v0.0.0-...-37e9492 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 20, 2018 License: MIT Imports: 0 Imported by: 12

README

Go Type-Agnostic Collection Partitioning

GoDoc Travis

Type-agnostic partitioning for anything that can be indexed in Go - slices, arrays,strings. Inspired by Guava's Lists.partition. This tiny library alleviates the issue of partitioning collections with wide ranging types - Go lacks generics - by returning consecutive index ranges that can be used on any indexable object.

Usage

    ...
    // bigList can be any type
    for idxRange := range gopart.Partition(len(bigList), partitionSize) {
        bulkOperation(bigList[idxRange.Low:idxRange.High])
    }
    ...

Full Executable Example

Installation

# install the library:
go get github.com/meirf/gopart

// use in your .go code:
import (
    "github.com/meirf/gopart"
)

Implementation

The partitioning is done with a separate goroutine that passes the index ranges to a channel. This requires the use of a for...range loop, but adds concurrency and lowers memory usage (no slice of index ranges is stored anywhere).

Documentation

Overview

The gopart package alleviates the issue of partitioning collections with wide-ranging types by returning index ranges that can be used on any indexable object. Inspired by Guava's Lists.partition

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Partition

func Partition(collectionLen, partitionSize int) chan IdxRange

Partition enables type-agnostic partitioning of anything indexable by specifying the length and the desired partition size of the indexable object. Consecutive index ranges are sent to the channel, each of which is the same size. The final range may be smaller than the others.

For example, a collection with length 8 and partition size 3 yields ranges: {0, 3}, {3, 6}, {6, 8}

This method should be used in a for...range loop. No results will be returned if the partition size is nonpositive. If the partition size is greater than the collection length, the range returned includes the entire collection.

Types

type IdxRange

type IdxRange struct {
	Low, High int
}

IdxRange specifies a single range. Low and High are the indexes in the larger collection at which this range begins and ends, respectively. Note that High is exclusive, whereas Low is inclusive.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL