Skip to content
/ gopart Public

Type-agnostic partitioning for Go's indexable collections and strings.

License

Notifications You must be signed in to change notification settings

meirf/gopart

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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).

About

Type-agnostic partitioning for Go's indexable collections and strings.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages