Documentation ¶
Overview ¶
Pipeline ¶
Pipeline is a functionnal programming package for the Go language. With Pipeline developpers can use functionnal principles such as map, reduce or filter on their collection types. Pipeline is written in go and inspired by underscore.js , lodash.js and Martin Fowler's pipelines :
http://martinfowler.com/articles/collection-pipeline/
author mparaiso <mparaiso@online.fr>
copyrights 2014
license GPL-3.0
version 0.1
## Installating:
- Install the Go language
- Use 'go get' with a command line interface
go get github.com/interactiv/pipeline
## Examples:
### Counting words
```go
// Counting words const words = `Lorem ipsum nascetur, nascetur adipiscing. Aenean commodo nascetur. Aenean nascetur commodo ridiculus nascetur, commodo ,nascetur consequat.` var result map[string]int err := pipeline.In(strings.Split(words, " ")).Map( func(el interface{}, i int) interface{} { return strings.Trim(strings.Trim(el.(string), " \r\n\t"), ".,!") }).GroupBy(func(el interface{}, i int) interface{} { return el.(string) }).ToMap(func(v interface{}, k interface{}) (interface{}, interface{}) { return []interface{}{len(v.([]interface{})), k}, k }).Out(&result) // => map[ridiculus:1 ipsum:1 :9 Aenean:2 commodo:3 Lorem:1 nascetur:6 adipiscing:1 consequat:1] fmt.Print(err)
```
### Calculating the total cost of an customer order
```go
// Using Map reduce to compile the total cost of an invoice type Order struct { ProductName string Quantity int UnitPrice int } var totalCost int command := []Order{{"Iphone", 2, 500}, {"Graphic card", 1, 250}, {"Flat screen", 3, 600}, {"Ipad air", 5, 200}} err := pipeline.In(command).Map(func(el interface{}, index int) interface{} { return el.(Order).Quantity * el.(Order).UnitPrice }).Reduce(func(result, el interface{}, index int) interface{} { return result.(int) + el.(int) }, 0).Out(&totalCost) fmt.Print(err, " ", totalCost) // Output: <nil> 4050
```
## Implemented pipelines
- Chunk - Compact - Concat - Difference - Equals - Every - Filter - First - Flatten - GroupBy - Head - IndexOf - Intersection - Last - LastIndexOf - Map - Push - Reduce - ReduceRight - Reverse - Slice - Some - Sort - Splice - Tail - ToMap - Union - Unique - Unshift - Without - Xor - Zip
Index ¶
- Constants
- func Chunk(array interface{}, length int) (interface{}, error)
- func Compact(array interface{}) (interface{}, error)
- func Concat(array interface{}, arrays ...interface{}) (interface{}, error)
- func Difference(array interface{}, values interface{}) (interface{}, error)
- func Equals(arrays ...interface{}) (interface{}, error)
- func Every(array interface{}, predicate func(v interface{}, index int) bool) (interface{}, error)
- func Filter(array interface{}, predicate func(element interface{}, index int) bool) (interface{}, error)
- func First(array interface{}) (interface{}, error)
- func Flatten(array interface{}) (interface{}, error)
- func GroupBy(collection interface{}, iteratee func(interface{}, int) interface{}) (interface{}, error)
- func Head(array interface{}, endIndex int) (interface{}, error)
- func IndexOf(array interface{}, searchedElement interface{}, fromIndex int) (interface{}, error)
- func Intersection(arrays ...interface{}) (interface{}, error)
- func IsIterable(value interface{}) bool
- func IsString(value interface{}) bool
- func Last(array interface{}) (interface{}, error)
- func LastIndexOf(array interface{}, searchElement interface{}, fromIndex int) (interface{}, error)
- func Map(value interface{}, callback func(interface{}, int) interface{}) (interface{}, error)
- func Must(value interface{}, err error) interface{}
- func Push(array interface{}, values ...interface{}) (interface{}, error)
- func Reduce(value interface{}, ...) (interface{}, error)
- func ReduceRight(array interface{}, ...) (interface{}, error)
- func Reverse(array interface{}) (interface{}, error)
- func Slice(array interface{}, start int, end int) (interface{}, error)
- func Some(array interface{}, predicate func(v interface{}, index int) bool) (interface{}, error)
- func Sort(array interface{}, compareFunc func(a, b interface{}) bool) (interface{}, error)
- func Splice(array interface{}, start int, deleteCount int, items ...interface{}) (interface{}, error)
- func Tail(array interface{}, startIndex int) (interface{}, error)
- func ToMap(mapOrSlice interface{}, ...) (interface{}, error)
- func Union(arrays ...interface{}) (interface{}, error)
- func Unique(array interface{}) (interface{}, error)
- func Unshift(array interface{}, values ...interface{}) (interface{}, error)
- func Without(array interface{}, values ...interface{}) (interface{}, error)
- func Xor(arrays ...interface{}) (interface{}, error)
- func Zip(arrayS interface{}) (interface{}, error)
- type Array
- type CannotAppendError
- type CannotAssignError
- type IndexOutOfBoundsError
- type Iterable
- type IterableInterface
- type NotAPointerError
- type NotIterableError
- type Pipeline
- func (pipeline *Pipeline) Chunk(length int) *Pipeline
- func (pipeline *Pipeline) Compact() *Pipeline
- func (pipeline *Pipeline) Concat(arrays ...interface{}) *Pipeline
- func (pipeline *Pipeline) Difference(array interface{}) *Pipeline
- func (pipeline *Pipeline) Equals(arrays ...interface{}) *Pipeline
- func (pipeline *Pipeline) Every(predicate func(element interface{}, index int) bool) *Pipeline
- func (pipeline *Pipeline) Filter(predicate func(element interface{}, index int) bool) *Pipeline
- func (pipeline *Pipeline) First() *Pipeline
- func (pipeline *Pipeline) Flatten() *Pipeline
- func (pipeline *Pipeline) GroupBy(iteratee func(element interface{}, index int) interface{}) *Pipeline
- func (pipeline *Pipeline) Head(end int) *Pipeline
- func (pipeline *Pipeline) IndexOf(value interface{}, fromIndex int) *Pipeline
- func (pipeline *Pipeline) Intersection(arrays ...interface{}) *Pipeline
- func (pipeline *Pipeline) Last() *Pipeline
- func (pipeline *Pipeline) LastIndexOf(value interface{}, fromIndex int) *Pipeline
- func (pipeline *Pipeline) Map(callback func(interface{}, int) interface{}) *Pipeline
- func (pipeline *Pipeline) MustOut() interface{}
- func (pipeline *Pipeline) Op(callback func(in interface{}) (interface{}, error)) *Pipeline
- func (pipeline *Pipeline) Out(output interface{}) error
- func (pipeline *Pipeline) Push(values ...interface{}) *Pipeline
- func (pipeline *Pipeline) Reduce(callback func(result interface{}, element interface{}, index int) interface{}, ...) *Pipeline
- func (pipeline *Pipeline) ReduceRight(callback func(result interface{}, element interface{}, index int) interface{}, ...) *Pipeline
- func (pipeline *Pipeline) Reverse() *Pipeline
- func (pipeline *Pipeline) Slice(start int, end int) *Pipeline
- func (pipeline *Pipeline) Some(predicate func(element interface{}, index int) bool) *Pipeline
- func (pipeline *Pipeline) Sort(compareFunc func(a, b interface{}) bool) *Pipeline
- func (pipeline *Pipeline) Splice(start int, deleteCount int, items ...interface{}) *Pipeline
- func (pipeline *Pipeline) Tail(start int) *Pipeline
- func (pipeline *Pipeline) ToMap(...) *Pipeline
- func (pipeline *Pipeline) Union(arrays ...interface{}) *Pipeline
- func (pipeline *Pipeline) Unique() *Pipeline
- func (pipeline *Pipeline) Unshift(values ...interface{}) *Pipeline
- func (pipeline *Pipeline) Without(values ...interface{}) *Pipeline
- func (pipeline *Pipeline) Xor(arrays ...interface{}) *Pipeline
- func (pipeline *Pipeline) Zip() *Pipeline
- type StepError
Examples ¶
Constants ¶
const VERSION = "0.1"
VERSION of pipeline
Variables ¶
This section is empty.
Functions ¶
func Chunk ¶
Chunk Creates an array of elements split into groups the length of size. If collection can’t be split evenly, the final chunk will be the remaining elements.
func Compact ¶
func Compact(array interface{}) (interface{}, error)
Compact remove nil values from array
func Concat ¶
func Concat(array interface{}, arrays ...interface{}) (interface{}, error)
Concat adds arrays to the end of the array and returns an new array
func Difference ¶
func Difference(array interface{}, values interface{}) (interface{}, error)
Difference creates an array excluding all provided values
func Equals ¶
func Equals(arrays ...interface{}) (interface{}, error)
Equals returns true if all arrays are of equal length and equal content
func Filter ¶
func Filter(array interface{}, predicate func(element interface{}, index int) bool) (interface{}, error)
Filter Iterates over elements of collection, returning a collection of all elements the predicate returns truthy for
func First ¶
func First(array interface{}) (interface{}, error)
First returns the first element of an array
func GroupBy ¶
func GroupBy(collection interface{}, iteratee func(interface{}, int) interface{}) (interface{}, error)
GroupBy creates an object composed of keys generated from the results of running each element of collection through iteratee
func IndexOf ¶
IndexOf returns the index at which the first occurrence of element is found in array or -1 if the element is not found
func Intersection ¶
func Intersection(arrays ...interface{}) (interface{}, error)
Intersection creates a collection of unique values that are included in all of the provided collections.
func IsIterable ¶
func IsIterable(value interface{}) bool
IsIterable returns true if value is iterable
func Last ¶
func Last(array interface{}) (interface{}, error)
Last returns the last element of an array
func LastIndexOf ¶
LastIndexOf method returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex.
func Must ¶
func Must(value interface{}, err error) interface{}
Must returns value or panics if err is not nil
func Push ¶
func Push(array interface{}, values ...interface{}) (interface{}, error)
Push adds an element at the end of the array
func Reduce ¶
func Reduce(value interface{}, callback func(result interface{}, element interface{}, index int) interface{}, initialOrNil interface{}) (interface{}, error)
Reduce folds the array into a single value
func ReduceRight ¶
func ReduceRight(array interface{}, callback func(result interface{}, element interface{}, index int) interface{}, initialOrnil interface{}) (interface{}, error)
ReduceRight folds the array into a single value,from the last value to the first value
func Reverse ¶
func Reverse(array interface{}) (interface{}, error)
Reverse reverse the order of the elements of the array and returns a new one
func Splice ¶
func Splice(array interface{}, start int, deleteCount int, items ...interface{}) (interface{}, error)
Splice deletes 'deleteCount' elements of an array from 'start' index and optionally inserts 'items'
func ToMap ¶
func ToMap(mapOrSlice interface{}, mapper func(value interface{}, key interface{}) (valueResult interface{}, keyResult interface{})) (interface{}, error)
ToMap takes a collection or a map and a callback, and returns a map[interface{}]interface{}
func Union ¶
func Union(arrays ...interface{}) (interface{}, error)
Union returns an array filled by all unique values of the arrays
func Unique ¶
func Unique(array interface{}) (interface{}, error)
Unique filters remove duplicate values from an array
func Unshift ¶
func Unshift(array interface{}, values ...interface{}) (interface{}, error)
Unshift add an element at the beginning of a collection
func Without ¶
func Without(array interface{}, values ...interface{}) (interface{}, error)
Without creates an array excluding all provided values
Types ¶
type CannotAppendError ¶
type CannotAppendError struct {
// contains filtered or unexported fields
}
CannotAppendError discriminates a not appendable error
func (CannotAppendError) Error ¶
func (cannotAppendError CannotAppendError) Error() string
Error returns a string
type CannotAssignError ¶
type CannotAssignError struct {
// contains filtered or unexported fields
}
CannotAssignError discriminates a non assignable value error
func (CannotAssignError) Error ¶
func (cannotAssignError CannotAssignError) Error() string
Error returns a string
type IndexOutOfBoundsError ¶
type IndexOutOfBoundsError struct {
// contains filtered or unexported fields
}
IndexOutOfBoundsError discriminates an index out of bounds error
func (IndexOutOfBoundsError) Error ¶
func (indexOutOfBoundsError IndexOutOfBoundsError) Error() string
Error returns a string
type Iterable ¶
type Iterable struct {
// contains filtered or unexported fields
}
Iterable implements IterableInterface
func (Iterable) ToArrayOfInterface ¶
func (iterable Iterable) ToArrayOfInterface() []interface{}
ToArrayOfInterface returns []interface{}
type IterableInterface ¶
type IterableInterface interface { Length() int At(index int) interface{} ToArrayOfInterface() []interface{} }
IterableInterface represents an value that can be iterated on
func NewIterable ¶
func NewIterable(array interface{}) IterableInterface
NewIterable returns a new iterable
type NotAPointerError ¶
type NotAPointerError struct {
// contains filtered or unexported fields
}
NotAPointerError discriminate pointer errors
func (NotAPointerError) Error ¶
func (notAPointerError NotAPointerError) Error() string
Error returns a string
type NotIterableError ¶
type NotIterableError struct {
// contains filtered or unexported fields
}
NotIterableError discriminates a value that cannot be iterated on
type Pipeline ¶
type Pipeline struct {
// contains filtered or unexported fields
}
Pipeline allow sequential operations on slices, arrays or strings
func (*Pipeline) Chunk ¶
Chunk Creates an array of elements split into groups the length of size. If collection can’t be split evenly, the final chunk will be the remaining elements.
func (*Pipeline) Difference ¶
Difference returns a collection of the differences between 2 collections
func (*Pipeline) Every ¶
Every returns true if the callback predicate is true for every element of the array
func (*Pipeline) Filter ¶
Filter Iterates over elements of collection, returning a collection of all elements the predicate returns truthy for
func (*Pipeline) GroupBy ¶
func (pipeline *Pipeline) GroupBy(iteratee func(element interface{}, index int) interface{}) *Pipeline
GroupBy Creates a map composed of keys generated from the results of running each element of collection through iteratee
Example ¶
package main import ( "fmt" "strings" "github.com/interactiv/pipeline" ) func main() { // Counting words const words = `Lorem ipsum nascetur, nascetur adipiscing. Aenean commodo nascetur. Aenean nascetur commodo ridiculus nascetur, commodo ,nascetur consequat.` var result map[string]int err := pipeline.In(strings.Split(words, " ")).Map(func(el interface{}, i int) interface{} { return strings.Trim(strings.Trim(el.(string), " \r\n\t"), ".,!") }).GroupBy(func(el interface{}, i int) interface{} { return el.(string) }).ToMap(func(v interface{}, k interface{}) (interface{}, interface{}) { return len(v.([]interface{})), k }).Out(&result) // => map[ridiculus:1 ipsum:1 :9 Aenean:2 commodo:3 Lorem:1 nascetur:6 adipiscing:1 consequat:1] fmt.Print(err) }
Output: <nil>
func (*Pipeline) IndexOf ¶
IndexOf returns the index at which the first occurrence of element is found in array or -1 if the element is not found
func (*Pipeline) Intersection ¶
Intersection creates a collection of unique values that are included in all of the provided collections.
func (*Pipeline) LastIndexOf ¶
LastIndexOf method returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex.
func (*Pipeline) Map ¶
Map send each element of a iterable through a function and return an array of results
func (*Pipeline) MustOut ¶
func (pipeline *Pipeline) MustOut() interface{}
MustOut panics on error or returns the result of the pipeline
func (*Pipeline) Out ¶
Out sets the output for the pipeline or return an error if an operation has failed output must be a pointer.
func (*Pipeline) Reduce ¶
func (pipeline *Pipeline) Reduce(callback func(result interface{}, element interface{}, index int) interface{}, initialOrNil interface{}) *Pipeline
Reduce folds the array into a single value
Example ¶
package main import ( "fmt" "github.com/interactiv/pipeline" ) func main() { // Using Map reduce to compile the total cost of an invoice type Order struct { ProductName string Quantity int UnitPrice int } var totalCost int command := []Order{{"Iphone", 2, 500}, {"Graphic card", 1, 250}, {"Flat screen", 3, 600}, {"Ipad air", 5, 200}} err := pipeline.In(command).Map(func(el interface{}, index int) interface{} { return el.(Order).Quantity * el.(Order).UnitPrice }).Reduce(func(result, el interface{}, index int) interface{} { return result.(int) + el.(int) }, 0).Out(&totalCost) fmt.Print(err, " ", totalCost) }
Output: <nil> 4050
func (*Pipeline) ReduceRight ¶
func (pipeline *Pipeline) ReduceRight(callback func(result interface{}, element interface{}, index int) interface{}, initialOrNil interface{}) *Pipeline
ReduceRight folds the array from end into a single value
func (*Pipeline) Reverse ¶
Reverse reverse the order of the elements of the array and returns a new one
func (*Pipeline) Sort ¶
Sort sorts an array given a compare function
Example ¶
package main import ( "fmt" "github.com/interactiv/pipeline" ) func main() { var result []int err := pipeline.In([]int{2, 1, 6, 3, 5, 4}).Sort(func(a interface{}, b interface{}) bool { return a.(int) <= b.(int) }).Out(&result) fmt.Print(result, " ", err) }
Output: [1 2 3 4 5 6] <nil>
func (*Pipeline) Splice ¶
Splice deletes 'deleteCount' elements of an array from 'start' index and optionally inserts 'items'
Example ¶
package main import ( "fmt" "github.com/interactiv/pipeline" ) func main() { var result []int err := pipeline.In([]int{1, 2, 3, 4, 5}). Splice(1, 2, []interface{}{6, 7, 8}...). Out(&result) fmt.Print(result, " ", err) }
Output: [1 6 7 8 4 5] <nil>
func (*Pipeline) ToMap ¶
func (pipeline *Pipeline) ToMap(callback func(value interface{}, key interface{}) (resultValue interface{}, resultKey interface{})) *Pipeline
ToMap takes a collection or a map and a callback, and returns a map[interface{}]interface{}
func (*Pipeline) Xor ¶
Xor creates an array of unique values that is the symmetric difference of the provided arrays.
Example ¶
package main import ( "fmt" "github.com/interactiv/pipeline" ) func main() { var result []int err := pipeline.In([]int{1, 2}).Xor([]int{2, 3}).Out(&result) fmt.Print(result, " ", err) }
Output: [1 3] <nil>