Documentation
¶
Overview ¶
Package rangechain chains together lazily computed modifications to range-able containers. E.g. slices, arrays, maps, and channels.
To start the chain, use the `From*` functions. They take a range-able container, and you'll pick the appropriate one for the type of container.
container := []int{987, 3, 26}
chain := rangechain.FromSlice(container)
From there, one can call a plethora of additional methods to modify the container passed in originally. The methods are outlined below. The methods fall into one of two categories: chaining or terminating.
Value Types ¶
Because Go does not support generics (yet), this library operates using `interface{}`. One using this library may need to use type assertions.
For example, something like `actualValue := value.(int)`.
For another example...
stringSlice := []string{"DogCows", "goes", "Moof", "Do", "you", "like", "Clarus", "the", "DogCow"}
chain := FromSlice(stringSlice)
outputSlice, _ := chain.
Map(func(value interface{}) (interface{}, error) {
stringValue := value.(string)
return stringValue + " not", nil
}).Slice()
Notice `stringValue := value.(string)` above. This allows one to do the string concatenation on the next line because the `+` operator doesn't work on an `interface{}` type.
Continuing the Chain ¶
Chaining methods apply some modification to the values in the container values, but keeps the chain alive. This allows additional chaining methods to be subsequently called on the result. The subsequent chain methods operate on any changes performed by the previous chain method. Because modifications are lazily computed, none of the modifications from chaining methods are applied until _after_ a terminating method is called.
Terminating the Chain ¶
Terminating methods also apply some modification, requests some information, or executes something on the values. They stop the chaining by returning an actual value. This value will depend on all the previous chaining methods being executed first.
Index ¶
- type Link
- func (receiver *Link) AllMatch(allMatchFunction func(interface{}) (bool, error)) (bool, error)
- func (receiver *Link) AnyMatch(anyMatchFunction func(interface{}) (bool, error)) (bool, error)
- func (receiver *Link) Channel() (<-chan interface{}, <-chan error)
- func (receiver *Link) Count() (int, error)
- func (receiver *Link) Distinct() *Link
- func (receiver *Link) Filter(filterFunction func(interface{}) (bool, error)) *Link
- func (receiver *Link) FilterParallel(filterFunction func(interface{}) (bool, error)) *Link
- func (receiver *Link) First() (*interface{}, error)
- func (receiver *Link) Flatten() *Link
- func (receiver *Link) ForEach(forEachFunction func(interface{})) error
- func (receiver *Link) ForEachParallel(forEachFunction func(interface{})) error
- func (receiver *Link) Last() (*interface{}, error)
- func (receiver *Link) Limit(keepSize int) *Link
- func (receiver *Link) Map(mapFunction func(interface{}) (interface{}, error)) *Link
- func (receiver *Link) MapParallel(mapFunction func(interface{}) (interface{}, error)) *Link
- func (receiver *Link) NoneMatch(noneMatchFunction func(interface{}) (bool, error)) (bool, error)
- func (receiver *Link) Reduce(reduceFunction func(interface{}, interface{}) (interface{}, error)) (*interface{}, error)
- func (receiver *Link) ReduceWithInitialValue(reduceFunction func(interface{}, interface{}) (interface{}, error), ...) (interface{}, error)
- func (receiver *Link) Reverse() *Link
- func (receiver *Link) Skip(skipNumber int) *Link
- func (receiver *Link) Slice() ([]interface{}, error)
- func (receiver *Link) Sort(returnLessFunction func([]interface{}) func(int, int) bool) *Link
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Link ¶
type Link struct {
// contains filtered or unexported fields
}
Link is not meant to be initialized directly by external users. Use the `From*` functions.
func FromArray ¶
func FromArray(array interface{}) *Link
FromArray starts the chain with the supplied array. Chaining and terminating methods can now be called on the result.
func FromChannel ¶
func FromChannel(channel interface{}) *Link
FromChannel starts the chain with the supplied channel. Chaining and terminating methods can now be called on the result.
func FromMap ¶
func FromMap(aMap interface{}) *Link
FromMap starts the chain with the supplied map. Chaining and terminating methods can now be called on the result. The singular value used to represent the key and value pairs is `keyvalue.KeyValuer` of `github.com/halprin/rangechain/keyvalue`.
func FromSlice ¶
func FromSlice(slice interface{}) *Link
FromSlice starts the chain with the supplied slice. Chaining and terminating methods can now be called on the result.
func (*Link) AllMatch ¶
AllMatch will run the `allMatchFunction` parameter function across all the values in the chain. If every `allMatchFunction` function invocation returns true, this method returns true. If a single `allMatchFunction` function invocation returns false, this method returns false. Also returns false and an error if any previous chain method generated an error or if an error is returned from the `allMatchFunction` function.
func (*Link) AnyMatch ¶
AnyMatch will run the `anyMatchFunction` parameter function across all the values in the chain. If any `anyMatchFunction` function invocation returns true, this method returns true. If every invocation `anyMatchFunction` invocation returns false, this method returns false. Also returns false and an error if any previous chain method generated an error or if an error is returned from the `anyMatchFunction` function.
func (*Link) Channel ¶
Channel serializes the chain into a channel. Also returns any errors in a channel if any previous chain method generated an error. If an error is returned, the value channel is closed, the error is sent on the error channel, and the error channel is closed.
func (*Link) Count ¶
Count returns the number of values in the chain. Also returns an error if any previous chain method generated an error. Count returns an accurate number even if an error is encountered.
func (*Link) Filter ¶
Filter will run the `filterFunction` parameter function against all the values in the chain. In that function, on return of true, the value will stay, or on false, the value will be removed.
func (*Link) FilterParallel ¶
FilterParallel will run the `filterFunction` parameter function against all the values in the chain in parallel. In that function, on return of true, the value will stay in the chain, or on false, the value will be removed from the chain. There is overhead to running in parallel so benchmark to ensure you benefit from this version.
func (*Link) First ¶
First returns just a pointer to the first value in the chain. If the chain is empty, returns `nil`. Also returns an error if any previous chain method generated an error that affects the first value.
func (*Link) Flatten ¶
Flatten will iterate over all the values in the chain, but any value encountered that is a range-able container itself will also have its values iterated over first before continuing with the remaining values in the chain. Maps flatten to its `keyvalue.KeyValuer` key and value pairs.
func (*Link) ForEach ¶
ForEach will run the `forEachFunction` parameter function across all the values in the chain. Also returns an error if any previous chain method generated an error. If an error is encountered, the function stops executing against the remaining chain.
func (*Link) ForEachParallel ¶
ForEachParallel will run the `forEachFunction` parameter function across all the values in the chain in parallel. Also returns an error if any previous chain method generated an error. If an error is encountered, the function stops executing against the remaining chain. There is overhead to running in parallel so benchmark to ensure you benefit from this version.
func (*Link) Last ¶
Last returns just a pointer to the last value in the chain. If the chain is empty, returns `nil`. Also returns an error if any previous chain method generated an error that affects the last value.
func (*Link) Limit ¶
Limit stops the chain after the parameter `keepSize` number of values. Any elements afterward are effectively removed.
func (*Link) Map ¶
Map will run the `mapFunction` parameter function parameter against all the values in the chain. In that function, return what you want to change the value into or an optional error if an error is encountered.
func (*Link) MapParallel ¶
MapParallel will run the `mapFunction` parameter function against all the values in the chain in parallel. In that function, return what you want to change the value into or an optional error if an error is encountered. There is overhead to running in parallel so benchmark to ensure you benefit from this version.
func (*Link) NoneMatch ¶
NoneMatch will do the exact opposite of `AnyMatch` when it comes to the boolean return value. Returns an error for the same reasons as `AnyMatch`.
func (*Link) Reduce ¶
func (receiver *Link) Reduce(reduceFunction func(interface{}, interface{}) (interface{}, error)) (*interface{}, error)
Reduce applies the `reduceFunction` parameter function to two values in the chain cumulatively. Subsequent calls to `reduceFunction` uses the previous return value from `reduceFunction` as the first argument and the next value in the chain as the second argument. A pointer to the final value is returned. If the chain is empty, `nil` is returned. Also returns an error if any previous chain method generated an error or if an error is returned from the `reduceFunction` function.
func (*Link) ReduceWithInitialValue ¶
func (receiver *Link) ReduceWithInitialValue(reduceFunction func(interface{}, interface{}) (interface{}, error), initialValue interface{}) (interface{}, error)
ReduceWithInitialValue applies the `reduceFunction` parameter function to two values in the chain cumulatively. Subsequent calls to `reduceFunction` uses the previous return value from `reduceFunction` as the first argument and the next value in the chain as the second argument. The parameter `initialValue` is placed before the entire chain and therefore is the first argument on the first invocation of `initialValue`. The final value is returned. If the chain is empty, `initialValue` is returned. Also returns an error if any previous chain method generated an error or if an error is returned from the `reduceFunction` function.
func (*Link) Reverse ¶
Reverse reverses the order of the chain. The last item will be first, and the first item will be last. This method is expensive because it must serialize all the values into a slice first.
func (*Link) Skip ¶
Skip skips over the parameter `skipNumber` number of values and effectively removes them from the chain. Also skips over any errors previously generated.
func (*Link) Slice ¶
Slice serializes the chain into a slice and returns it. Also returns an error if any previous chain method generated an error. If an error is returned, the slice is filled in until the error was encountered.
func (*Link) Sort ¶
Sort sorts the chain given the `Less` function returned from the `returnLessFunction` function parameter. The `returnLessFunction` function is called with the entire serialized chain as a slice and _returns_ a function that satisfies the same requirements as the Interface type's `Less` function (https://pkg.go.dev/sort#Interface). This method is expensive because it must serialize all the values into a slice first.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
generator
Package generator has many helper methods for generating values in a chain.
|
Package generator has many helper methods for generating values in a chain. |
|
Package keyvalue exists to specify the interface of `KeyValuer`.
|
Package keyvalue exists to specify the interface of `KeyValuer`. |