pipeline

package module
v0.0.0-...-e8d21fe Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2015 License: GPL-3.0 Imports: 4 Imported by: 0

README

Pipeline

Build Status Pipeline Coverage

written by mparaiso mparaiso@online.fr , copyrights 2014 ,license GPL-3.0

version 0.1

Pipeline is a functionnal programming package for the Go language. With Pipeline developpers can use functionnal principles such as map, reduce or filter. Pipeline is written in go and inspired by underscore.js , lodash.js and Martin Fowler's pipelines :

http://martinfowler.com/articles/collection-pipeline/

Installating:

go get github.com/interactiv/pipeline

Examples:

Counting words
    // 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)     
Calculating the total cost of a customer order
	// 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
Sorting numbers
    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>
Symmetric difference
	var result []int
	err := pipeline.In([]int{1, 2}).Xor([]int{2, 3}).Out(&result)
	fmt.Print(result, " ", err)
	// Output: [1 3] <nil>

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

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

Examples

Constants

View Source
const VERSION = "0.1"

VERSION of pipeline

Variables

This section is empty.

Functions

func Chunk

func Chunk(array interface{}, length int) (interface{}, error)

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 Every

func Every(array interface{}, predicate func(v interface{}, index int) bool) (interface{}, error)

Every returns true if the callback predicate is true for every element of the array

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 Flatten

func Flatten(array interface{}) (interface{}, error)

Flattens a nested 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 Head(array interface{}, endIndex int) (interface{}, error)

Head returns the head until end

func IndexOf

func IndexOf(array interface{}, searchedElement interface{}, fromIndex int) (interface{}, error)

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 IsString

func IsString(value interface{}) bool

func Last

func Last(array interface{}) (interface{}, error)

Last returns the last element of an array

func LastIndexOf

func LastIndexOf(array interface{}, searchElement interface{}, fromIndex int) (interface{}, error)

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 Map

func Map(value interface{}, callback func(interface{}, int) interface{}) (interface{}, error)

Map is a map function

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 Slice

func Slice(array interface{}, start int, end int) (interface{}, error)

Slice returns a slice of an array

func Some

func Some(array interface{}, predicate func(v interface{}, index int) bool) (interface{}, error)

Some returns true if the callback predicate is satisfied

func Sort

func Sort(array interface{}, compareFunc func(a, b interface{}) bool) (interface{}, error)

Sort sorts an array given a compare function

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 Tail

func Tail(array interface{}, startIndex int) (interface{}, error)

Tail returns the tail starting from start

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

func Xor

func Xor(arrays ...interface{}) (interface{}, error)

Xor creates an array of unique values that is the symmetric difference of the provided arrays.

func Zip

func Zip(arrayS interface{}) (interface{}, error)

Zip Creates an array of grouped elements, the first of which contains the first elements of the given arrays, the second of which contains the second elements of the given arrays, and so on.

Types

type Array

type Array interface{}

Array is a place holder for interface{}

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

func (iterable *Iterable) At(index int) interface{}

At is the value at index

func (Iterable) Length

func (iterable Iterable) Length() int

Length is the length

func (Iterable) String

func (iterable Iterable) String() string

String returns a string representation

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

func (NotIterableError) Error

func (err NotIterableError) Error() string

Error returns a string

type Pipeline

type Pipeline struct {
	// contains filtered or unexported fields
}

Pipeline allow sequential operations on slices, arrays or strings

func In

func In(sliceOrStringOrMap Array) *Pipeline

In Returns a new Pipeline

func (*Pipeline) Chunk

func (pipeline *Pipeline) Chunk(length int) *Pipeline

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

func (pipeline *Pipeline) Compact() *Pipeline

Compact remove nil values from array

func (*Pipeline) Concat

func (pipeline *Pipeline) Concat(arrays ...interface{}) *Pipeline

Concat adds arrays to the end of the array and returns an new array

func (*Pipeline) Difference

func (pipeline *Pipeline) Difference(array interface{}) *Pipeline

Difference returns a collection of the differences between 2 collections

func (*Pipeline) Equals

func (pipeline *Pipeline) Equals(arrays ...interface{}) *Pipeline

Equals returns true if all arrays are of equal length and Equal content

func (*Pipeline) Every

func (pipeline *Pipeline) Every(predicate func(element interface{}, index int) bool) *Pipeline

Every returns true if the callback predicate is true for every element of the array

func (*Pipeline) Filter

func (pipeline *Pipeline) Filter(predicate func(element interface{}, index int) bool) *Pipeline

Filter Iterates over elements of collection, returning a collection of all elements the predicate returns truthy for

func (*Pipeline) First

func (pipeline *Pipeline) First() *Pipeline

First returns the first element

func (*Pipeline) Flatten

func (pipeline *Pipeline) Flatten() *Pipeline

Flattens a nested array.

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

func (pipeline *Pipeline) Head(end int) *Pipeline

Head returns the head until end

func (*Pipeline) IndexOf

func (pipeline *Pipeline) IndexOf(value interface{}, fromIndex int) *Pipeline

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

func (pipeline *Pipeline) Intersection(arrays ...interface{}) *Pipeline

Intersection creates a collection of unique values that are included in all of the provided collections.

func (*Pipeline) Last

func (pipeline *Pipeline) Last() *Pipeline

Last returns the last element

func (*Pipeline) LastIndexOf

func (pipeline *Pipeline) LastIndexOf(value interface{}, fromIndex int) *Pipeline

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

func (pipeline *Pipeline) Map(callback func(interface{}, int) interface{}) *Pipeline

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

func (pipeline *Pipeline) Op(callback func(in interface{}) (interface{}, error)) *Pipeline

Op insert a custom operation in the pipeline

func (*Pipeline) Out

func (pipeline *Pipeline) Out(output interface{}) error

Out sets the output for the pipeline or return an error if an operation has failed output must be a pointer.

func (*Pipeline) Push

func (pipeline *Pipeline) Push(values ...interface{}) *Pipeline

Push adds an element at the end of the array

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

func (pipeline *Pipeline) Reverse() *Pipeline

Reverse reverse the order of the elements of the array and returns a new one

func (*Pipeline) Slice

func (pipeline *Pipeline) Slice(start int, end int) *Pipeline

Slice returns a slice of an array

func (*Pipeline) Some

func (pipeline *Pipeline) Some(predicate func(element interface{}, index int) bool) *Pipeline

Some returns true if the callback predicate is satisfied

func (*Pipeline) Sort

func (pipeline *Pipeline) Sort(compareFunc func(a, b interface{}) bool) *Pipeline

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

func (pipeline *Pipeline) Splice(start int, deleteCount int, items ...interface{}) *Pipeline

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

func (pipeline *Pipeline) Tail(start int) *Pipeline

Tail returns the tail starting from start

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

func (pipeline *Pipeline) Union(arrays ...interface{}) *Pipeline

Union returns an array filled by all unique values of the arrays

func (*Pipeline) Unique

func (pipeline *Pipeline) Unique() *Pipeline

Unique returns all the unique elements in a collection

func (*Pipeline) Unshift

func (pipeline *Pipeline) Unshift(values ...interface{}) *Pipeline

Unshift add an element at the beginning of a collection

func (*Pipeline) Without

func (pipeline *Pipeline) Without(values ...interface{}) *Pipeline

Without returns a collection without the values

func (*Pipeline) Xor

func (pipeline *Pipeline) Xor(arrays ...interface{}) *Pipeline

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>

func (*Pipeline) Zip

func (pipeline *Pipeline) Zip() *Pipeline

Zip creates an array of grouped elements, the first of which contains the first elements of the given arrays, the second of which contains the second elements of the given arrays, and so on.

type StepError

type StepError struct {
	// contains filtered or unexported fields
}

StepError discriminates a step error

func (StepError) Error

func (stepError StepError) Error() string

Error returns a string

Jump to

Keyboard shortcuts

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