enumerable

package module
v0.0.0-...-5356b56 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2019 License: MIT Imports: 3 Imported by: 0

README

Enumerable: An experiment in Go

The idea is to see how feasible it is to create something like Ruby's Enumerable in Go. In order to do this in a generic way requires use of Go's reflect package.

Goals

  • a better understanding of reflect and the related performance issues
  • an easy to use package which at the very least could be used when some performance can be traded for ease of use and readability (i.e. when IO is already the bottleneck, this will probably be fast enough)
  • where possible, fast implementations will be provided so that they can be used even when top performance is required

Examples

  • Count total letters
var list = []string{"one", "two", "three"}
letterCount := enumerable.New(list).Map(func(item string) int {
  return len(item)
}).Reduce(0, func(memo, l int) int {
  return memo + l
} // letterCount = 11
  • get a sorted list of unique words in files
nonAlphabet := regexp.MustCompile("[^a-zA-Z]")
files, _ := filepath.Glob("*.txt")
words := enumerable.New(files).Map(func(file string) []string {
  contents, err := ioutil.ReadFile(file)
  if err != nil {
    panic(err)
  }
  return nonAlphabet.Split(string(contents), -1)
}).Flatten().Uniq().Sort().JoinAsString(", ")

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/syoder/enumerable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

This package is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Enumerable project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Documentation

Index

Constants

View Source
const NotFound = -1

NotFound is returned by `Find` when no matching item is found

Variables

This section is empty.

Functions

func Any

func Any(slice AnySlice, f IterFunc) bool

Any returns true if the given function returns true for any items in the slice

func Avg

func Avg(slice AnySlice) float64

Avg returns the average value of items in the slice

func FilterBools

func FilterBools(in []bool, f func(bool) bool) []bool

FilterBools returns a slice of bool where `f` returns true

func FilterFloat64s

func FilterFloat64s(in []float64, f func(float64) bool) []float64

FilterFloat64s returns a slice of float64 where `f` returns true

func FilterInts

func FilterInts(in []int, f func(int) bool) []int

FilterInts returns a slice of int where `f` returns true

func FilterStrings

func FilterStrings(in []string, f func(string) bool) []string

FilterStrings returns a slice of string where `f` returns true

func JoinAsString

func JoinAsString(slice AnySlice, sep string) string

JoinAsString returns a string with each item converted to string and joined with `sep`.

func JoinAsStringWithFormat

func JoinAsStringWithFormat(slice AnySlice, format, sep string) string

JoinAsStringWithFormat returns a string with each item converted to string using the given format string (see fmt.Printf) and joined with `sep`.

func MapAnyValueToBool

func MapAnyValueToBool(in []AnyValue, f func(AnyValue) bool) []bool

MapAnyValueToBool maps a slice of AnyValue to bool

func MapAnyValueToFloat64

func MapAnyValueToFloat64(in []AnyValue, f func(AnyValue) float64) []float64

MapAnyValueToFloat64 maps a slice of AnyValue to float64

func MapAnyValueToInt

func MapAnyValueToInt(in []AnyValue, f func(AnyValue) int) []int

MapAnyValueToInt maps a slice of AnyValue to int

func MapAnyValueToString

func MapAnyValueToString(in []AnyValue, f func(AnyValue) string) []string

MapAnyValueToString maps a slice of AnyValue to string

func MapBoolToBool

func MapBoolToBool(in []bool, f func(bool) bool) []bool

MapBoolToBool maps a slice of bool to bool

func MapBoolToFloat64

func MapBoolToFloat64(in []bool, f func(bool) float64) []float64

MapBoolToFloat64 maps a slice of bool to float64

func MapBoolToInt

func MapBoolToInt(in []bool, f func(bool) int) []int

MapBoolToInt maps a slice of bool to int

func MapBoolToString

func MapBoolToString(in []bool, f func(bool) string) []string

MapBoolToString maps a slice of bool to string

func MapFloat64ToBool

func MapFloat64ToBool(in []float64, f func(float64) bool) []bool

MapFloat64ToBool maps a slice of float64 to bool

func MapFloat64ToFloat64

func MapFloat64ToFloat64(in []float64, f func(float64) float64) []float64

MapFloat64ToFloat64 maps a slice of float64 to float64

func MapFloat64ToInt

func MapFloat64ToInt(in []float64, f func(float64) int) []int

MapFloat64ToInt maps a slice of float64 to int

func MapFloat64ToString

func MapFloat64ToString(in []float64, f func(float64) string) []string

MapFloat64ToString maps a slice of float64 to string

func MapIntToBool

func MapIntToBool(in []int, f func(int) bool) []bool

MapIntToBool maps a slice of int to bool

func MapIntToFloat64

func MapIntToFloat64(in []int, f func(int) float64) []float64

MapIntToFloat64 maps a slice of int to float64

func MapIntToInt

func MapIntToInt(in []int, f func(int) int) []int

MapIntToInt maps a slice of int to int

func MapIntToString

func MapIntToString(in []int, f func(int) string) []string

MapIntToString maps a slice of int to string

func MapStringToBool

func MapStringToBool(in []string, f func(string) bool) []bool

MapStringToBool maps a slice of string to bool

func MapStringToFloat64

func MapStringToFloat64(in []string, f func(string) float64) []float64

MapStringToFloat64 maps a slice of string to float64

func MapStringToInt

func MapStringToInt(in []string, f func(string) int) []int

MapStringToInt maps a slice of string to int

func MapStringToString

func MapStringToString(in []string, f func(string) string) []string

MapStringToString maps a slice of string to string

func ReduceAnyValueToBool

func ReduceAnyValueToBool(in []AnyValue, memo bool, f func(bool, AnyValue) bool) bool

ReduceAnyValueToBool reduces a slice of AnyValue to bool

func ReduceAnyValueToFloat64

func ReduceAnyValueToFloat64(in []AnyValue, memo float64, f func(float64, AnyValue) float64) float64

ReduceAnyValueToFloat64 reduces a slice of AnyValue to float64

func ReduceAnyValueToInt

func ReduceAnyValueToInt(in []AnyValue, memo int, f func(int, AnyValue) int) int

ReduceAnyValueToInt reduces a slice of AnyValue to int

func ReduceAnyValueToString

func ReduceAnyValueToString(in []AnyValue, memo string, f func(string, AnyValue) string) string

ReduceAnyValueToString reduces a slice of AnyValue to string

func ReduceBoolToBool

func ReduceBoolToBool(in []bool, memo bool, f func(bool, bool) bool) bool

ReduceBoolToBool reduces a slice of bool to bool

func ReduceBoolToFloat64

func ReduceBoolToFloat64(in []bool, memo float64, f func(float64, bool) float64) float64

ReduceBoolToFloat64 reduces a slice of bool to float64

func ReduceBoolToInt

func ReduceBoolToInt(in []bool, memo int, f func(int, bool) int) int

ReduceBoolToInt reduces a slice of bool to int

func ReduceBoolToString

func ReduceBoolToString(in []bool, memo string, f func(string, bool) string) string

ReduceBoolToString reduces a slice of bool to string

func ReduceFloat64ToBool

func ReduceFloat64ToBool(in []float64, memo bool, f func(bool, float64) bool) bool

ReduceFloat64ToBool reduces a slice of float64 to bool

func ReduceFloat64ToFloat64

func ReduceFloat64ToFloat64(in []float64, memo float64, f func(float64, float64) float64) float64

ReduceFloat64ToFloat64 reduces a slice of float64 to float64

func ReduceFloat64ToInt

func ReduceFloat64ToInt(in []float64, memo int, f func(int, float64) int) int

ReduceFloat64ToInt reduces a slice of float64 to int

func ReduceFloat64ToString

func ReduceFloat64ToString(in []float64, memo string, f func(string, float64) string) string

ReduceFloat64ToString reduces a slice of float64 to string

func ReduceIntToBool

func ReduceIntToBool(in []int, memo bool, f func(bool, int) bool) bool

ReduceIntToBool reduces a slice of int to bool

func ReduceIntToFloat64

func ReduceIntToFloat64(in []int, memo float64, f func(float64, int) float64) float64

ReduceIntToFloat64 reduces a slice of int to float64

func ReduceIntToInt

func ReduceIntToInt(in []int, memo int, f func(int, int) int) int

ReduceIntToInt reduces a slice of int to int

func ReduceIntToString

func ReduceIntToString(in []int, memo string, f func(string, int) string) string

ReduceIntToString reduces a slice of int to string

func ReduceStringToBool

func ReduceStringToBool(in []string, memo bool, f func(bool, string) bool) bool

ReduceStringToBool reduces a slice of string to bool

func ReduceStringToFloat64

func ReduceStringToFloat64(in []string, memo float64, f func(float64, string) float64) float64

ReduceStringToFloat64 reduces a slice of string to float64

func ReduceStringToInt

func ReduceStringToInt(in []string, memo int, f func(int, string) int) int

ReduceStringToInt reduces a slice of string to int

func ReduceStringToString

func ReduceStringToString(in []string, memo string, f func(string, string) string) string

ReduceStringToString reduces a slice of string to string

func Reverse

func Reverse(slice AnySlice)

Reverse reverses the order of items in the slice

func Sort

func Sort(slice AnySlice)

Sort requires a slice with elements that are some sort of int or string

func SortBy

func SortBy(slice AnySlice, f IterFunc)

SortBy sorts a slice by the return value of function which is given each item in the slice. The function's return value must be a `string` or `int`.

func SumAsFloat64

func SumAsFloat64(slice AnySlice) float64

SumAsFloat64 will sum items in slice and return value as float64.

func SumAsInt

func SumAsInt(slice AnySlice) int

SumAsInt sums all items in slice and returns value as int. Will panic if element values are floats

Types

type AnySlice

type AnySlice interface{}

AnySlice represents a slice with elements of any type

func ConvertSlice

func ConvertSlice(in AnySlice, outType AnySlice) AnySlice

func Filter

func Filter(slice AnySlice, f IterFunc) AnySlice

Filter takes a slice of any type and a function which takes that type as a paramter and returns a bool to indicate whether that value should be included in the output.

func First

func First(slice AnySlice, n int) AnySlice

First copies the first n elements of a slice into a new slice

func Flatten

func Flatten(slice AnySlice) AnySlice

Flatten returns a new slice by concatenating all the elements in `slice`

func Intersection

func Intersection(a, b AnySlice) AnySlice

Intersection returns a new slice containing only the items present in both slices

func Last

func Last(slice AnySlice, n int) AnySlice

Last copies the last n elements of a slice into a new slice

func Map

func Map(slice AnySlice, f IterFunc) AnySlice

Map takes a slice of some type and a function which takes that type as a parameter, and returns a slice of the same type as the function returns

func Uniq

func Uniq(slice AnySlice) AnySlice

Uniq returns a new slice with duplicates removed

type AnyValue

type AnyValue interface{}

AnyValue represents any value

func FilterAnyValues

func FilterAnyValues(in []AnyValue, f func(AnyValue) bool) []AnyValue

FilterAnyValues returns a slice of AnyValue where `f` returns true

func Find

func Find(slice AnySlice, f IterFunc) (int, AnyValue)

Find calls `f` for each item in the slice until `f` returns `true`. If an item is found, it returns the index of that item and the item itself. If none are found, it returns `NotFound, nil`.

func GenericMap

func GenericMap(in []AnyValue, f func(AnyValue) AnyValue) []AnyValue

GenericMap does not use reflection, but does require the input slice to be of `[]AnyValue` type, and requires the function to do type assertions as necessary. var GenericMap = MapAnyValueToAnyValue

func Group

func Group(slice AnySlice, keyFunc IterFunc) AnyValue

Group groups the slice by the return value of `keyFunc`. It returns a map where the keys are the return values of `keyFunc`, and the values are slices of the same type as the given slice

func MapAnyValueToAnyValue

func MapAnyValueToAnyValue(in []AnyValue, f func(AnyValue) AnyValue) []AnyValue

MapAnyValueToAnyValue maps a slice of AnyValue to AnyValue

func MapBoolToAnyValue

func MapBoolToAnyValue(in []bool, f func(bool) AnyValue) []AnyValue

MapBoolToAnyValue maps a slice of bool to AnyValue

func MapFloat64ToAnyValue

func MapFloat64ToAnyValue(in []float64, f func(float64) AnyValue) []AnyValue

MapFloat64ToAnyValue maps a slice of float64 to AnyValue

func MapIntToAnyValue

func MapIntToAnyValue(in []int, f func(int) AnyValue) []AnyValue

MapIntToAnyValue maps a slice of int to AnyValue

func MapStringToAnyValue

func MapStringToAnyValue(in []string, f func(string) AnyValue) []AnyValue

MapStringToAnyValue maps a slice of string to AnyValue

func Max

func Max(slice AnySlice) AnyValue

Max returns the maximum value in `slice`

func Min

func Min(slice AnySlice) AnyValue

Min returns the minimum value in `slice`

func Reduce

func Reduce(slice AnySlice, memo AnyValue, f IterFunc) AnyValue

Reduce takes a slice of some type, a memo value of some other type, and a function which takes the memo type and the slice type as parameters and returns the memo type. Reduce returns the final memo.

func ReduceAnyValueToAnyValue

func ReduceAnyValueToAnyValue(in []AnyValue, memo AnyValue, f func(AnyValue, AnyValue) AnyValue) AnyValue

ReduceAnyValueToAnyValue reduces a slice of AnyValue to AnyValue

func ReduceBoolToAnyValue

func ReduceBoolToAnyValue(in []bool, memo AnyValue, f func(AnyValue, bool) AnyValue) AnyValue

ReduceBoolToAnyValue reduces a slice of bool to AnyValue

func ReduceFloat64ToAnyValue

func ReduceFloat64ToAnyValue(in []float64, memo AnyValue, f func(AnyValue, float64) AnyValue) AnyValue

ReduceFloat64ToAnyValue reduces a slice of float64 to AnyValue

func ReduceIntToAnyValue

func ReduceIntToAnyValue(in []int, memo AnyValue, f func(AnyValue, int) AnyValue) AnyValue

ReduceIntToAnyValue reduces a slice of int to AnyValue

func ReduceStringToAnyValue

func ReduceStringToAnyValue(in []string, memo AnyValue, f func(AnyValue, string) AnyValue) AnyValue

ReduceStringToAnyValue reduces a slice of string to AnyValue

func ToGenericSlice

func ToGenericSlice(in AnySlice) []AnyValue

type Enumerable

type Enumerable struct {
	Slice AnySlice
}

Enumerable makes it easy to map, filter, reduce, etc using any slice

func New

func New(slice AnySlice) Enumerable

New creates a new Enumerable struct

func (Enumerable) Any

func (e Enumerable) Any(f IterFunc) bool

Any returns true if the given function returns true for any items in the slice

func (Enumerable) Avg

func (e Enumerable) Avg() float64

Avg returns the average value of items in the slice

func (Enumerable) Convert

func (e Enumerable) Convert(sliceType AnySlice) Enumerable

func (Enumerable) Filter

func (e Enumerable) Filter(f IterFunc) Enumerable

Filter returns a new Enumerable containing only the elements for which the given function returns `true`

func (Enumerable) Find

func (e Enumerable) Find(f IterFunc) (int, AnyValue)

Find calls `f` for each item in the slice until `f` returns `true`. If an item is found, it returns the index of that item and the item itself. If none are found, it returns `NotFound, nil`.

func (Enumerable) First

func (e Enumerable) First(n int) Enumerable

First returns a new Enumerable containing only the first `n` elements

func (Enumerable) Flatten

func (e Enumerable) Flatten() Enumerable

Flatten returns a new slice with each value of the slice concatenated

func (Enumerable) GenericMap

func (e Enumerable) GenericMap(f func(AnyValue) AnyValue) Enumerable

GenericMap does the same thing as `map` but requires a slice type of `[]AnyValue` and requires the function to do type assertions as necessary

func (Enumerable) GetSlice

func (e Enumerable) GetSlice(slicePtr interface{})

GetSlice sets `slicePtr` to the stored slice

func (Enumerable) Group

func (e Enumerable) Group(keyFunc IterFunc) AnyValue

func (Enumerable) JoinAsString

func (e Enumerable) JoinAsString(sep string) string

JoinAsString returns a string with each item converted to string and joined with `sep`.

func (Enumerable) JoinAsStringWithFormat

func (e Enumerable) JoinAsStringWithFormat(format, sep string) string

JoinAsStringWithFormat returns a string with each item converted to string using the given format string (see fmt.Printf) and joined with `sep`.

func (Enumerable) Last

func (e Enumerable) Last(n int) Enumerable

Last returns a new Enumerable containing only the last `n` elements

func (Enumerable) Map

func (e Enumerable) Map(f IterFunc) Enumerable

Map iterates over the slice a returns a new Enumerable

func (Enumerable) Max

func (e Enumerable) Max() AnyValue

Max returns the maximum value in the slice

func (Enumerable) Min

func (e Enumerable) Min() AnyValue

Min returns the minimum value in the slice

func (Enumerable) Reduce

func (e Enumerable) Reduce(memo AnyValue, f IterFunc) AnyValue

Reduce iterates over a slice passing the memo value into the function

func (Enumerable) ReduceToFloat64

func (e Enumerable) ReduceToFloat64(memo float64, f IterFunc) float64

ReduceToFloat64 calls reduce but forces the memo to be a `float64`

func (Enumerable) ReduceToInt

func (e Enumerable) ReduceToInt(memo int, f IterFunc) int

ReduceToInt calls reduce but forces the memo to be an `int`

func (Enumerable) Reverse

func (e Enumerable) Reverse() Enumerable

Reverse reverses the order of the items in the slice

func (Enumerable) Sort

func (e Enumerable) Sort() Enumerable

func (Enumerable) SortBy

func (e Enumerable) SortBy(f IterFunc) Enumerable

SortBy sorts an Enumerable by the return value of function which is given each item in the slice. The function's return value must be a `string` or `int`.

func (Enumerable) SumAsFloat64

func (e Enumerable) SumAsFloat64() float64

SumAsFloat64 returns the sum of items in the slice as a float64 value

func (Enumerable) SumAsInt

func (e Enumerable) SumAsInt() int

SumAsInt returns the sum of items in the slice as an int. It will panic if the values are float values

func (Enumerable) ToGeneric

func (e Enumerable) ToGeneric() Enumerable

ToGeneric converts the slice to be of type `[]AnyType`

func (Enumerable) Uniq

func (e Enumerable) Uniq() Enumerable

Uniq returns a new slice with duplicates removed

type IterFunc

type IterFunc interface{}

IterFunc represents a function which will be called when iterating through a slice

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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