arrayutil

package module
v0.0.0-...-96f2f79 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2015 License: Apache-2.0 Imports: 0 Imported by: 0

README

arrayutil

-- import "github.com/atedja/go-arrayutil"

Usage

func Compact
func Compact(arr []interface{}) []interface{}

Removes nil values from an array.

Example:

var arr = []interface{}{1, 2, 3, 4, nil, 5}
result := Compact(arr)  // [1, 2, 3, 4, 5]
func Flatten
func Flatten(arr []interface{}) []interface{}

Returns a new array that is one-dimensional flat.

Example:

var arr1 = []interface{}{1, 2, 3, 4}       // [1, 2, 3, 4]
var arr2 = []interface{}{5, 6, 7, arr1}    // [5, 6, 7, [1, 2, 3, 4]]
result := arrayutil.Flatten(arr2)          // [5, 6, 7, 1, 2, 3, 4]
func Inject
func Inject(arr []interface{}, initial interface{}, injectFunc InjectFunc) interface{}

Combines all elements of array by applying an operation, specified by InjectFunc. For each element, the InjectFunc is passed a memo value and the element. The result becomes the memo value for the next iteration.

If nil is passed to the initial memo value, it will use the first element.

Example:

func Sum(memo interface{}, e interface{}) interface{} {
	return memo.(int) + e.(int)
}

var myArray = []interface{}{1, 2, 3, 4}
result := Inject(myArray, nil, Sum)     // result is 10
result := Inject(myArray, 10, Sum)      // result is 20
func Map
func Map(arr []interface{}, mapFunc MapFunc) []interface{}

Invokes MapFunc for each element in the array. Creates a new array containing the values returned by MapFunc

Example:

func MyMapFunc(v interface{}) interface{} {
	return v.(int) * 3
}
var myArray = []interface{}{1, 2, 3, 4}
result := arrayutil.Map(myArray, MyMapFunc)  // [3, 6, 9, 12]
func Reject
func Reject(arr []interface{}, rejectFunc RejectFunc) []interface{}

Invokes RejectFunc for each element in the array, deleting elements for which the function returns true. Opposite of Select().

Example:

func Even(v interface{}) bool {
	return v.(int)%2 == 0
}
var myArray = []interface{}{1, 2, 3, 4}
result := Reject(myArray, Even)  // [1, 3]
func Select
func Select(arr []interface{}, selectFunc SelectFunc) []interface{}

Invokes SelectFunc for each element in the array, keeping elements for which the function returns true. Opposite of Reject().

Example:

func Even(v interface{}) bool {
	return v.(int)%2 == 0
}
var myArray = []interface{}{1, 2, 3, 4}
result := Select(myArray, Even)  // [2, 4]
func Unique
func Unique(arr []interface{}) []interface{}

Returns a new array with duplicates removed.

Example:

var myArray = []interface{}{1, 2, 3, 3, 4}
result := Unique(myArray)  // [1, 2, 3, 4]
type InjectFunc
type InjectFunc func(interface{}, interface{}) interface{}
type MapFunc
type MapFunc func(interface{}) interface{}
type RejectFunc
type RejectFunc func(interface{}) bool
type SelectFunc
type SelectFunc func(interface{}) bool

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compact

func Compact(arr []interface{}) []interface{}

Removes nil values from an array.

Example:

var arr = []interface{}{1, 2, 3, 4, nil, 5}
result := Compact(arr)  // [1, 2, 3, 4, 5]

func Flatten

func Flatten(arr []interface{}) []interface{}

Returns a new array that is one-dimensional flat.

Example:

var arr1 = []interface{}{1, 2, 3, 4}       // [1, 2, 3, 4]
var arr2 = []interface{}{5, 6, 7, arr1}    // [5, 6, 7, [1, 2, 3, 4]]
result := arrayutil.Flatten(arr2)          // [5, 6, 7, 1, 2, 3, 4]

func Inject

func Inject(arr []interface{}, initial interface{}, injectFunc InjectFunc) interface{}

Combines all elements of array by applying an operation, specified by `InjectFunc`. For each element, the `InjectFunc` is passed a memo value and the element. The result becomes the memo value for the next iteration.

If `nil` is passed to the `initial` memo value, it will use the first element.

Example:

func Sum(memo interface{}, e interface{}) interface{} {
	return memo.(int) + e.(int)
}

var myArray = []interface{}{1, 2, 3, 4}
result := Inject(myArray, nil, Sum)     // result is 10
result := Inject(myArray, 10, Sum)      // result is 20

func Map

func Map(arr []interface{}, mapFunc MapFunc) []interface{}

Invokes `MapFunc` for each element in the array. Creates a new array containing the values returned by `MapFunc`

Example:

func MyMapFunc(v interface{}) interface{} {
	return v.(int) * 3
}
var myArray = []interface{}{1, 2, 3, 4}
result := arrayutil.Map(myArray, MyMapFunc)  // [3, 6, 9, 12]

func Reject

func Reject(arr []interface{}, rejectFunc RejectFunc) []interface{}

Invokes `RejectFunc` for each element in the array, deleting elements for which the function returns true. Opposite of Select().

Example:

func Even(v interface{}) bool {
	return v.(int)%2 == 0
}
var myArray = []interface{}{1, 2, 3, 4}
result := Reject(myArray, Even)  // [1, 3]

func Select

func Select(arr []interface{}, selectFunc SelectFunc) []interface{}

Invokes `SelectFunc` for each element in the array, keeping elements for which the function returns true. Opposite of Reject().

Example:

func Even(v interface{}) bool {
	return v.(int)%2 == 0
}
var myArray = []interface{}{1, 2, 3, 4}
result := Select(myArray, Even)  // [2, 4]

func Unique

func Unique(arr []interface{}) []interface{}

Returns a new array with duplicates removed.

Example:

var myArray = []interface{}{1, 2, 3, 3, 4}
result := Unique(myArray)  // [1, 2, 3, 4]

Types

type InjectFunc

type InjectFunc func(interface{}, interface{}) interface{}

type MapFunc

type MapFunc func(interface{}) interface{}

type RejectFunc

type RejectFunc func(interface{}) bool

type SelectFunc

type SelectFunc func(interface{}) bool

Jump to

Keyboard shortcuts

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