import "github.com/cinar/indicator/v2/helper"Package helper contains the helper functions.
This package belongs to the Indicator project. Indicator is a Golang module that supplies a variety of technical indicators, strategies, and a backtesting framework for analysis.
Copyright (c) 2021-2026 Onur Cinar.
The source code is provided under GNU AGPLv3 License.
https://github.com/cinar/indicator
The information provided on this project is strictly for informational purposes and is not to be construed as advice or solicitation to buy or sell any security.
- Constants
- func Abs[T Number](c <-chan T) <-chan T
- func Add[T Number](ac, bc <-chan T) <-chan T
- func AppendOrWriteToCsvFile[T any](fileName string, rows <-chan *T, options ...CsvOption[T]) error
- func Apply[T Number](c <-chan T, f func(T) T) <-chan T
- func Buffered[T any](c <-chan T, size int) <-chan T
- func ChanToJSON[T any](c <-chan T, w io.Writer) error
- func ChanToSlice[T any](c <-chan T) []T
- func Change[T Number](c <-chan T, before int) <-chan T
- func ChangePercent[T Number](c <-chan T, before int) <-chan T
- func ChangeRatio[T Number](c <-chan T, before int) <-chan T
- func CheckEquals[T comparable](inputs ...<-chan T) error
- func CloseAndLogError(closer io.Closer, message string)
- func CloseAndLogErrorWithLogger(closer io.Closer, message string, logger *slog.Logger)
- func CloseDatabaseRows(rows *sql.Rows)
- func CloseDatabaseWithError(db *sql.DB, err error) error
- func CommonPeriod(periods ...int) int
- func Count[T Number, O any](from T, other <-chan O) <-chan T
- func DaysBetween(from, to time.Time) int
- func DecrementBy[T Number](c <-chan T, d T) <-chan T
- func Divide[T Number](ac, bc <-chan T) <-chan T
- func DivideBy[T Number](c <-chan T, d T) <-chan T
- func Drain[T any](c <-chan T)
- func Duplicate[T any](input <-chan T, count int) []<-chan T
- func Echo[T any](input <-chan T, last, count int) <-chan T
- func Field[T, S any](c <-chan *S, name string) (<-chan T, error)
- func Filter[T any](c <-chan T, p func(T) bool) <-chan T
- func First[T any](c <-chan T, count int) <-chan T
- func Gcd(values ...int) int
- func Head[T Number](c <-chan T, count int) <-chan T
- func Highest[T Number](c <-chan T, w int) <-chan T
- func IncrementBy[T Number](c <-chan T, i T) <-chan T
- func JSONToChan[T any](r io.Reader) <-chan T
- func JSONToChanWithLogger[T any](r io.Reader, logger *slog.Logger) <-chan T
- func KeepNegatives[T Number](c <-chan T) <-chan T
- func KeepPositives[T Number](c <-chan T) <-chan T
- func Last[T any](c <-chan T, count int) <-chan T
- func Lcm(values ...int) int
- func Lowest[T Number](c <-chan T, w int) <-chan T
- func Map[F, T any](c <-chan F, f func(F) T) <-chan T
- func MapWithPrevious[F, T any](c <-chan F, f func(T, F) T, previous T) <-chan T
- func MaxSince[T Number](c <-chan T, w int) <-chan T
- func MinSince[T Number](c <-chan T, w int) <-chan T
- func Multiply[T Number](ac, bc <-chan T) <-chan T
- func MultiplyBy[T Number](c <-chan T, m T) <-chan T
- func Operate[A any, B any, R any](ac <-chan A, bc <-chan B, o func(A, B) R) <-chan R
- func Operate3[A any, B any, C any, R any](ac <-chan A, bc <-chan B, cc <-chan C, o func(A, B, C) R) <-chan R
- func Operate4[A any, B any, C any, D any, R any](ac <-chan A, bc <-chan B, cc <-chan C, dc <-chan D, o func(A, B, C, D) R) <-chan R
- func Operate5[A any, B any, C any, D any, E any, R any](ac <-chan A, bc <-chan B, cc <-chan C, dc <-chan D, ec <-chan E, o func(A, B, C, D, E) R) <-chan R
- func PercentRank[T Number](c <-chan T, period int) <-chan T
- func Pipe[T any](f <-chan T, t chan<- T)
- func Pow[T Number](c <-chan T, y T) <-chan T
- func ReadFromCsvFile[T any](fileName string, options ...CsvOption[T]) (<-chan *T, error)
- func Remove(t *testing.T, name string)
- func RemoveAll(t *testing.T, path string)
- func RoundDigit[T Number](n T, d int) T
- func RoundDigits[T Number](c <-chan T, d int) <-chan T
- func Seq[T Number](from, to, increment T) <-chan T
- func Shift[T any](c <-chan T, count int, fill T) <-chan T
- func Sign[T Number](c <-chan T) <-chan T
- func Since[T comparable, R Number](c <-chan T) <-chan R
- func Skip[T any](c <-chan T, count int) <-chan T
- func SkipLast[T any](c <-chan T, count int) <-chan T
- func SliceToChan[T any](slice []T) <-chan T
- func SlicesReverse[T any](r []T, i int, f func(T) bool)
- func SortedPercentRank[T Number](c <-chan T, period int) <-chan T
- func Sqrt[T Number](c <-chan T) <-chan T
- func Subtract[T Number](ac, bc <-chan T) <-chan T
- func SyncPeriod[T any](commonPeriod, period int, c <-chan T) <-chan T
- func Waitable[T any](wg *sync.WaitGroup, c <-chan T) <-chan T
- func Window[T any](c <-chan T, f func([]T, int) T, w int) <-chan T
- type Bst
- type BstNode
- type Csv
- func NewCsv[T any](options ...CsvOption[T]) (*Csv[T], error)
- func (c *Csv[T]) AppendToFile(fileName string, rows <-chan *T) error
- func (c *Csv[T]) ReadFromFile(fileName string) (<-chan *T, error)
- func (c *Csv[T]) ReadFromReader(reader io.Reader) <-chan *T
- func (c *Csv[T]) WriteToFile(fileName string, rows <-chan *T) error
- type CsvOption
- type Float
- type Integer
- type Number
- type Report
- type ReportColumn
- type Ring
const (
// CsvHeaderTag represents the parameter name for the column header.
CsvHeaderTag = "header"
// CsvFormatTag represents the parameter name for the column format.
CsvFormatTag = "format"
// DefaultDateTimeFormat denotes the default format of a date and time column.
DefaultDateTimeFormat = "2006-01-02"
)const (
// DefaultReportDateFormat is the default date format used in the report.
DefaultReportDateFormat = "2006-01-02"
)func Abs
func Abs[T Number](c <-chan T) <-chan TAbs calculates the absolute value of each value in a channel of type T.
Example:
abs := helper.Abs(helper.SliceToChan([]int{-10, 20, -4, -5}))
fmt.Println(helper.ChanToSlice(abs)) // [10, 20, 4, 5]
func Add
func Add[T Number](ac, bc <-chan T) <-chan TAdd adds each pair of values from the two input channels of type T and returns a new channel containing the sums.
Example:
ac := helper.SliceToChan([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
bc := helper.SliceToChan([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
actual := helper.ChanToSlice(helper.Add(ac, bc))
fmt.Println(actual) // [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
func AppendOrWriteToCsvFile[T any](fileName string, rows <-chan *T, options ...CsvOption[T]) errorAppendOrWriteToCsvFile writes the provided rows of data to the specified file, appending to the existing file if it exists or creating a new one if it doesn't. In append mode, the function assumes that the existing file's column order matches the field order of the given row struct to ensure consistent data structure.
func Apply
func Apply[T Number](c <-chan T, f func(T) T) <-chan TApply applies the given transformation function to each element in the input channel and returns a new channel containing the transformed values. The transformation function takes a value of type T as input and returns a value of type T as output.
Example:
timesTwo := helper.Apply(c, func(n int) int {
return n * 2
})
func Buffered
func Buffered[T any](c <-chan T, size int) <-chan TBuffered takes a channel of any type and returns a new channel of the same type with a buffer of the specified size. This allows the original channel to continue sending data even if the receiving end is temporarily unavailable.
Example:
func ChanToJSON
func ChanToJSON[T any](c <-chan T, w io.Writer) errorChanToJSON converts a channel of values into JSON format and writes it to the specified writer.
Example:
input := helper.SliceToChan([]int{2, 4, 6, 8})
var buffer bytes.Buffer
err := helper.ChanToJSON(input, &buffer)
fmt.Println(buffer.String())
// Output: [2,4,6,8,9]
func ChanToSlice
func ChanToSlice[T any](c <-chan T) []TChanToSlice converts a channel of type T to a slice of type T.
Example:
c := make(chan int, 4)
c <- 1
c <- 2
c < -3
c <- 4
close(c)
fmt.Println(helper.ChanToSlice(c)) // [1, 2, 3, 4]
func Change
func Change[T Number](c <-chan T, before int) <-chan TChange calculates the difference between the current value and the value N before.
Example:
input := []int{1, 2, 5, 5, 8, 2, 1, 1, 3, 4}
output := helper.Change(helper.SliceToChan(input), 2)
fmt.Println(helper.ChanToSlice(output)) // [4, 3, 3, -3, -7, -1, 2, 3]
func ChangePercent
func ChangePercent[T Number](c <-chan T, before int) <-chan TChangePercent calculates the percentage change between the current value and the value N positions before.
Example:
c := helper.ChanToSlice([]float64{1, 2, 5, 5, 8, 2, 1, 1, 3, 4})
actual := helper.ChangePercent(c, 2))
fmt.Println(helper.ChanToSlice(actual)) // [400, 150, 60, -60, -87.5, -50, 200, 300]
func ChangeRatio
func ChangeRatio[T Number](c <-chan T, before int) <-chan TChangeRatio calculates the ratio change between the current value and the value N positions before.
Example:
c := helper.ChanToSlice([]float64{1, 2, 5, 5, 8, 2, 1, 1, 3, 4})
actual := helper.ChangeRatio(c, 2))
fmt.Println(helper.ChanToSlice(actual)) // [400, 150, 60, -60, -87.5, -50, 200, 300]
func CheckEquals
func CheckEquals[T comparable](inputs ...<-chan T) errorCheckEquals determines whether the two channels are equal.
func CloseAndLogError
func CloseAndLogError(closer io.Closer, message string)CloseAndLogError attempts to close the closer and logs any error.
func CloseAndLogErrorWithLogger(closer io.Closer, message string, logger *slog.Logger)CloseAndLogErrorWithLogger attempts to close the closer and logs any error to the given logger.
func CloseDatabaseRows
func CloseDatabaseRows(rows *sql.Rows)CloseDatabaseRows closes the database rows.
func CloseDatabaseWithError(db *sql.DB, err error) errorCloseDatabaseWithError closes the database after an error.
func CommonPeriod
func CommonPeriod(periods ...int) intCommonPeriod calculates the smallest period at which all data channels can be synchronized
Example:
// Synchronize channels with periods 4, 2, and 3.
commonPeriod := helper.CommonPeriod(4, 2, 3) // commonPeriod = 4
// Synchronize the first channel
c1 := helper.Sync(commonPeriod, 4, c1)
// Synchronize the second channel
c2 := helper.Sync(commonPeriod, 2, c2)
// Synchronize the third channel
c3 := helper.Sync(commonPeriod, 3, c3)
func Count
func Count[T Number, O any](from T, other <-chan O) <-chan TCount generates a sequence of numbers starting with a specified value, from, and incrementing by one until the given other channel continues to produce values.
Example:
other := make(chan int, 4)
other <- 1
other <- 1
other <- 1
other <- 1
close(other)
c := Count(0, other)
fmt.Println(<- s) // 1
fmt.Println(<- s) // 2
fmt.Println(<- s) // 3
fmt.Println(<- s) // 4
func DaysBetween
func DaysBetween(from, to time.Time) intDaysBetween calculates the days between the given two times.
func DecrementBy
func DecrementBy[T Number](c <-chan T, d T) <-chan TDecrementBy decrements each element in the input channel by the specified decrement value and returns a new channel containing the decremented values.
Example:
input := helper.SliceToChan([]int{1, 2, 3, 4})
substractOne := helper.DecrementBy(input, 1)
fmt.Println(helper.ChanToSlice(substractOne)) // [0, 1, 2, 3]
func Divide
func Divide[T Number](ac, bc <-chan T) <-chan TDivide takes two channels of type T and divides the values from the first channel with the values from the second one. It returns a new channel containing the results of the division.
Example:
ac := helper.SliceToChan([]int{2, 4, 6, 8, 10})
bc := helper.SliceToChan([]int{2, 1, 3, 2, 5})
division := helper.Divide(ac, bc)
fmt.Println(helper.ChanToSlice(division)) // [1, 4, 2, 4, 2]
func DivideBy
func DivideBy[T Number](c <-chan T, d T) <-chan TDivideBy divides each element in the input channel of type T values by the given divider and returns a new channel containing the divided values.
Example:
half := helper.DivideBy(helper.SliceToChan([]int{2, 4, 6, 8}), 2)
fmt.Println(helper.ChanToSlice(half)) // [1, 2, 3, 4]
func Drain
func Drain[T any](c <-chan T)Drain drains the given channel. It blocks the caller.
func Duplicate
func Duplicate[T any](input <-chan T, count int) []<-chan TDuplicate duplicates a given receive-only channel by reading each value coming out of that channel and sending them on requested number of new output channels.
Example:
expected := helper.SliceToChan([]float64{-10, 20, -4, -5})
outputs := helper.Duplicates[float64](helper.SliceToChan(expected), 2)
fmt.Println(helper.ChanToSlice(outputs[0])) // [-10, 20, -4, -5]
fmt.Println(helper.ChanToSlice(outputs[1])) // [-10, 20, -4, -5]
func Echo
func Echo[T any](input <-chan T, last, count int) <-chan TEcho takes a channel of numbers, repeats the specified count of numbers at the end by the specified count.
Example:
input := helper.SliceToChan([]int{2, 4, 6, 8})
output := helper.Echo(input, 2, 4))
fmt.Println(helper.ChanToSlice(output)) // [2, 4, 6, 8, 6, 8, 6, 8, 6, 8, 6, 8]
func Field
func Field[T, S any](c <-chan *S, name string) (<-chan T, error)Field extracts a specific field from a channel of struct pointers and delivers it through a new channel.
func Filter
func Filter[T any](c <-chan T, p func(T) bool) <-chan TFilter filters the items from the input channel based on the provided predicate function. The predicate function takes a value of type T as input and returns a boolean value indicating whether the value should be included in the output channel.
Example:
even := helper.Filter(c, func(n int) bool {
return n%2 == 0
})
func First
func First[T any](c <-chan T, count int) <-chan TFirst takes a channel of values and returns a new channel containing the first N values.
func Gcd
func Gcd(values ...int) intGcd calculates the Greatest Common Divisor of the given numbers.
func Head
func Head[T Number](c <-chan T, count int) <-chan THead retrieves the specified number of elements from the given channel of type T values and delivers them through a new channel.
Example:
c := helper.SliceToChan([]int{2, 4, 6, 8})
actual := helper.Head(c, 2)
fmt.Println(helper.ChanToSlice(actual)) // [2, 4]
func Highest
func Highest[T Number](c <-chan T, w int) <-chan THighest returns a channel that emits the highest value within a sliding window of size w from the input channel c.
func IncrementBy
func IncrementBy[T Number](c <-chan T, i T) <-chan TIncrementBy increments each element in the input channel by the specified increment value and returns a new channel containing the incremented values.
Example:
input := []int{1, 2, 3, 4}
actual := helper.IncrementBy(helper.SliceToChan(input), 1)
fmt.Println(helper.ChanToSlice(actual)) // [2, 3, 4, 5]
func JSONToChan
func JSONToChan[T any](r io.Reader) <-chan TJSONToChan reads values from the specified reader in JSON format into a channel of values.
func JSONToChanWithLogger
func JSONToChanWithLogger[T any](r io.Reader, logger *slog.Logger) <-chan TJSONToChanWithLogger reads values from the specified reader in JSON format into a channel of values.
func KeepNegatives
func KeepNegatives[T Number](c <-chan T) <-chan TKeepNegatives processes a stream of type T values, retaining negative values unchanged and replacing positive values with zero.
Example:
c := helper.SliceToChan([]int{-10, 20, 4, -5})
negatives := helper.KeepPositives(c)
fmt.Println(helper.ChanToSlice(negatives)) // [-10, 0, 0, -5]
func KeepPositives
func KeepPositives[T Number](c <-chan T) <-chan TKeepPositives processes a stream of type T values, retaining positive values unchanged and replacing negative values with zero.
Example:
c := helper.SliceToChan([]int{-10, 20, 4, -5})
positives := helper.KeepPositives(c)
fmt.Println(helper.ChanToSlice(positives)) // [0, 20, 4, 0]
func Last
func Last[T any](c <-chan T, count int) <-chan TLast takes a channel of values and returns a new channel containing the last N values.
func Lcm
func Lcm(values ...int) intLcm calculates the Least Common Multiple of the given numbers.
func Lowest
func Lowest[T Number](c <-chan T, w int) <-chan TLowest returns a channel that emits the lowest value within a sliding window of size w from the input channel c.
func Map
func Map[F, T any](c <-chan F, f func(F) T) <-chan TMap applies the given transformation function to each element in the input channel and returns a new channel containing the transformed values. The transformation function takes a value of type F as input and returns a value of type T as output.
Example:
timesTwo := helper.Map(c, func(n int) int {
return n * 2
})
func MapWithPrevious
func MapWithPrevious[F, T any](c <-chan F, f func(T, F) T, previous T) <-chan TMapWithPrevious applies a transformation function to each element in an input channel, creating a new channel with the transformed values. It maintains a "memory" of the previous result, allowing the transformation function to consider both the current element and the outcome of the previous transformation. This enables functions that rely on accumulated state or sequential dependencies between elements.
Example:
sum := helper.MapWithPrevious(c, func(p, c int) int {
return p + c
}, 0)
func MaxSince
func MaxSince[T Number](c <-chan T, w int) <-chan TMaxSince returns a channel of T indicating since when (number of previous values) the respective value was the maximum within the window of size w.
func MinSince
func MinSince[T Number](c <-chan T, w int) <-chan TMinSince returns a channel of T indicating since when (number of previous values) the respective value was the minimum.
func Multiply
func Multiply[T Number](ac, bc <-chan T) <-chan TMultiply takes two channels of type T and multiples the values from the first channel with the values from the second channel. It returns a new channel containing the results of the multiplication.
Example:
ac := helper.SliceToChan([]int{1, 4, 2, 4, 2})
bc := helper.SliceToChan([]int{2, 1, 3, 2, 5})
multiplication := helper.Multiply(ac, bc)
fmt.Println(helper.ChanToSlice(multiplication)) // [2, 4, 6, 8, 10]
func MultiplyBy
func MultiplyBy[T Number](c <-chan T, m T) <-chan TMultiplyBy multiplies each element in the input channel of type T values by the given multiplier and returns a new channel containing the multiplied values.
Example:
c := helper.SliceToChan([]int{1, 2, 3, 4})
twoTimes := helper.MultiplyBy(c, 2)
fmt.Println(helper.ChanToSlice(twoTimes)) // [2, 4, 6, 8]
func Operate
func Operate[A any, B any, R any](ac <-chan A, bc <-chan B, o func(A, B) R) <-chan ROperate applies the provided operate function to corresponding values from two numeric input channels and sends the resulting values to an output channel.
Example:
add := helper.Operate(ac, bc, func(a, b int) int {
return a + b
})
func Operate3
func Operate3[A any, B any, C any, R any](ac <-chan A, bc <-chan B, cc <-chan C, o func(A, B, C) R) <-chan ROperate3 applies the provided operate function to corresponding values from three numeric input channels and sends the resulting values to an output channel.
Example:
add := helper.Operate3(ac, bc, cc, func(a, b, c int) int {
return a + b + c
})
func Operate4
func Operate4[A any, B any, C any, D any, R any](ac <-chan A, bc <-chan B, cc <-chan C, dc <-chan D, o func(A, B, C, D) R) <-chan ROperate4 applies the provided operate function to corresponding values from four numeric input channels and sends the resulting values to an output channel.
Example:
add := helper.Operate4(ac, bc, cc, dc, func(a, b, c, d int) int {
return a + b + c + d
})
func Operate5
func Operate5[A any, B any, C any, D any, E any, R any](ac <-chan A, bc <-chan B, cc <-chan C, dc <-chan D, ec <-chan E, o func(A, B, C, D, E) R) <-chan ROperate5 applies the provided operate function to corresponding values from five numeric input channels and sends the resulting values to an output channel.
Example:
result := helper.Operate5(ac, bc, cc, dc, ec, func(a, b, c, d, e int) int {
return a + b + c + d + e
})
func PercentRank
func PercentRank[T Number](c <-chan T, period int) <-chan TPercentRank returns a channel that emits the percentile rank of each value compared to the previous period-1 values. The rank is between 0 and 100.
func Pipe
func Pipe[T any](f <-chan T, t chan<- T)Pipe function takes an input channel and an output channel and copies all elements from the input channel into the output channel.
Example:
input := helper.SliceToChan([]int{2, 4, 6, 8})
output := make(chan int)
helper.Pipe(input, output)
fmt.println(helper.ChanToSlice(output)) // [2, 4, 6, 8]
func Pow
func Pow[T Number](c <-chan T, y T) <-chan TPow takes a channel of type T values and returns the element-wise base-value exponential of y.
Example:
c := helper.SliceToChan([]int{2, 3, 5, 10})
squared := helper.Pow(c, 2)
fmt.Println(helper.ChanToSlice(squared)) // [4, 9, 25, 100]
func ReadFromCsvFile
func ReadFromCsvFile[T any](fileName string, options ...CsvOption[T]) (<-chan *T, error)ReadFromCsvFile creates a CSV instance, parses CSV data from the provided filename, maps the data to corresponding struct fields, and delivers it through the channel.
func Remove
func Remove(t *testing.T, name string)Remove removes the file with the given name.
func RemoveAll
func RemoveAll(t *testing.T, path string)RemoveAll removes the files with the given path.
func RoundDigit
func RoundDigit[T Number](n T, d int) TRoundDigit rounds the given float64 number to d decimal places.
Example:
n := helper.RoundDigit(10.1234, 2)
fmt.Println(n) // 10.12
func RoundDigits
func RoundDigits[T Number](c <-chan T, d int) <-chan TRoundDigits takes a channel of type T numbers and rounds them to d decimal places.
Example:
c := helper.SliceToChan([]float64{10.1234, 5.678, 6.78, 8.91011})
rounded := helper.RoundDigits(c, 2)
fmt.Println(helper.ChanToSlice(rounded)) // [10.12, 5.68, 6.78, 8.91]
func Seq
func Seq[T Number](from, to, increment T) <-chan TSeq generates a sequence of numbers starting with a specified value, from, and incrementing by a specified amount, increment, until a specified value, to, is reached or exceeded. The sequence includes from, but not to.
Example:
s := Seq(1, 5, 1)
defer close(s)
fmt.Println(<- s) // 1
fmt.Println(<- s) // 2
fmt.Println(<- s) // 3
fmt.Println(<- s) // 4
func Shift
func Shift[T any](c <-chan T, count int, fill T) <-chan TShift takes a channel of numbers, shifts them to the right by the specified count, and fills in any missing values with the provided fill value.
Example:
input := helper.SliceToChan([]int{2, 4, 6, 8})
output := helper.Shift(input, 4, 0)
fmt.Println(helper.ChanToSlice(output)) // [0, 0, 0, 0, 2, 4, 6, 8]
func Sign
func Sign[T Number](c <-chan T) <-chan TSign takes a channel of type T values and returns their signs as -1 for negative, 0 for zero, and 1 for positive.
Example:
c := helper.SliceToChan([]int{-10, 20, -4, 0})
sign := helper.Sign(c)
fmt.Println(helper.ChanToSlice(sign)) // [-1, 1, -1, 0]
func Since
func Since[T comparable, R Number](c <-chan T) <-chan RSince counts the number of periods since the last change of value in a channel of numbers.
func Skip
func Skip[T any](c <-chan T, count int) <-chan TSkip skips the specified number of elements from the given channel of type T.
Example:
c := helper.SliceToChan([]int{2, 4, 6, 8})
actual := helper.Skip(c, 2)
fmt.Println(helper.ChanToSlice(actual)) // [6, 8]
func SkipLast
func SkipLast[T any](c <-chan T, count int) <-chan TSkipLast skips the specified number of elements from the end of the given channel.
Example:
c := helper.SliceToChan([]int{2, 4, 6, 8})
actual := helper.SkipLast(c, 2)
fmt.Println(helper.ChanToSlice(actual)) // [2, 4]
func SliceToChan
func SliceToChan[T any](slice []T) <-chan TSliceToChan converts a slice of type T to a channel of type T.
Example:
slice := []float64{2, 4, 6, 8}
c := helper.SliceToChan(slice)
fmt.Println(<- c) // 2
fmt.Println(<- c) // 4
fmt.Println(<- c) // 6
fmt.Println(<- c) // 8
func SlicesReverse
func SlicesReverse[T any](r []T, i int, f func(T) bool)SlicesReverse loops through a slice in reverse order starting from the given index. The given function is called for each element in the slice. If the function returns false, the loop is terminated.
func SortedPercentRank
func SortedPercentRank[T Number](c <-chan T, period int) <-chan TSortedPercentRank returns a channel that emits the percentile rank by sorting the window values. This is more accurate but slower.
func Sqrt
func Sqrt[T Number](c <-chan T) <-chan TSqrt calculates the square root of each value in a channel of type T.
Example:
c := helper.SliceToChan([]int{9, 81, 16, 100})
sqrt := helper.Sqrt(c)
fmt.Println(helper.ChanToSlice(sqrt)) // [3, 9, 4, 10]
func Subtract
func Subtract[T Number](ac, bc <-chan T) <-chan TSubtract takes two channels of type T and subtracts the values from the second channel from the first one. It returns a new channel containing the results of the subtractions.
Example:
ac := helper.SliceToChan([]int{2, 4, 6, 8, 10})
bc := helper.SliceToChan([]int{1, 2, 3, 4, 5})
actual := helper.Subtract(ac, bc)
fmt.Println(helper.ChanToSlice(actual)) // [1, 2, 3, 4, 5]
func SyncPeriod
func SyncPeriod[T any](commonPeriod, period int, c <-chan T) <-chan TSyncPeriod adjusts the given channel to match the given common period.
func Waitable
func Waitable[T any](wg *sync.WaitGroup, c <-chan T) <-chan TWaitable increments the wait group before reading from the channel and signals completion when the channel is closed.
func Window
func Window[T any](c <-chan T, f func([]T, int) T, w int) <-chan TWindow returns a channel that emits the passed function result within a sliding window of size w from the input channel c. Note: the slice is in the same order than in source channel but the 1st element may not be 0, use modulo window size if order is important.
type Bst
Bst represents the binary search tree.
type Bst[T Number] struct {
// contains filtered or unexported fields
}func NewBst
func NewBst[T Number]() *Bst[T]NewBst creates a new binary search tree.
func (*Bst[T]) Contains
func (b *Bst[T]) Contains(value T) boolContains checks whether the given value exists in the binary search tree.
func (*Bst[T]) Insert
func (b *Bst[T]) Insert(value T)Insert adds a new value to the binary search tree.
func (*Bst[T]) Max
func (b *Bst[T]) Max() TMax function returns the maximum value in the binary search tree.
func (*Bst[T]) Min
func (b *Bst[T]) Min() TMin function returns the minimum value in the binary search tree.
func (*Bst[T]) Remove
func (b *Bst[T]) Remove(value T) boolRemove removes the specified value from the binary search tree and rebalances the tree.
type BstNode
BstNode represents the binary search tree node.
type BstNode[T Number] struct {
// contains filtered or unexported fields
}type Csv
Csv represents the configuration for CSV reader and writer.
type Csv[T any] struct {
// Logger is the slog logger instance.
Logger *slog.Logger
// contains filtered or unexported fields
}func NewCsv
func NewCsv[T any](options ...CsvOption[T]) (*Csv[T], error)NewCsv creates a new CSV instance with the provided options.
func (*Csv[T]) AppendToFile
func (c *Csv[T]) AppendToFile(fileName string, rows <-chan *T) errorAppendToFile appends the provided rows of data to the end of the specified file, creating the file if it doesn't exist. In append mode, the function assumes that the existing file's column order matches the field order of the given row struct to ensure consistent data structure.
func (*Csv[T]) ReadFromFile
func (c *Csv[T]) ReadFromFile(fileName string) (<-chan *T, error)ReadFromFile parses the CSV data from the provided file name, maps the data to corresponding struct fields, and delivers the resulting rows through the channel.
func (*Csv[T]) ReadFromReader
func (c *Csv[T]) ReadFromReader(reader io.Reader) <-chan *TReadFromReader parses the CSV data from the provided reader, maps the data to corresponding struct fields, and delivers the resulting it through the channel.
func (*Csv[T]) WriteToFile
func (c *Csv[T]) WriteToFile(fileName string, rows <-chan *T) errorWriteToFile creates a new file with the given name and writes the provided rows of data to it, overwriting any existing content.
type CsvOption
CsvOption represents a functional option for configuring the CSV instance.
type CsvOption[T any] func(*Csv[T])func WithCsvDefaultDateTimeFormat[T any](format string) CsvOption[T]WithCsvDefaultDateTimeFormat sets the default date and time format for the CSV instance.
func WithCsvLogger
func WithCsvLogger[T any](logger *slog.Logger) CsvOption[T]WithCsvLogger sets the logger for the CSV instance.
func WithoutCsvHeader
func WithoutCsvHeader[T any]() CsvOption[T]WithoutCsvHeader disables the header row in the CSV.
type Float
Float refers to any float type.
type Float interface {
// contains filtered or unexported methods
}type Integer
Integer refers to any integer type.
type Integer interface {
// contains filtered or unexported methods
}type Number
Number refers to any numeric type.
type Number interface {
// contains filtered or unexported methods
}type Report
Report generates an HTML file containing an interactive chart that visually represents the provided data and annotations.
The generated HTML file can be opened in a web browser to explore the data visually, interact with the chart elements, and view the associated annotations.
type Report struct {
Title string
Date <-chan time.Time
Columns []ReportColumn
Views [][]int
DateFormat string
GeneratedOn string
}func NewReport
func NewReport(title string, date <-chan time.Time) *ReportNewReport takes a channel of time as the time axis and returns a new instance of the Report struct. This instance can later be used to add data and annotations and subsequently generate a report.
func (*Report) AddChart
func (r *Report) AddChart() intAddChart adds a new chart to the report and returns its unique identifier. This identifier can be used later to refer to the chart and add columns to it.
func (*Report) AddColumn
func (r *Report) AddColumn(column ReportColumn, charts ...int)AddColumn adds a new data column to the specified charts. If no chart is specified, it will be added to the main chart.
func (*Report) WriteToFile
func (r *Report) WriteToFile(fileName string) errorWriteToFile writes the generated report content to a file with the specified name. This allows users to conveniently save the report for later viewing or analysis.
func (*Report) WriteToWriter
func (r *Report) WriteToWriter(writer io.Writer) errorWriteToWriter writes the report content to the provided io.Writer. This allows the report to be sent to various destinations, such as a file, a network socket, or even the standard output.
type ReportColumn
ReportColumn defines the interface that all report data columns must implement. This interface ensures that different types of data columns can be used consistently within the report generation process.
type ReportColumn interface {
// Name returns the name of the report column.
Name() string
// Type returns the data type of the report column.
Type() string
// Role returns the role of the report column.
Role() string
// Value returns the next data value for the report column.
Value() string
}func NewAnnotationReportColumn(values <-chan string) ReportColumnNewAnnotationReportColumn returns a new instance of an annotation column for a report.
func NewNumericReportColumn[T Number](name string, values <-chan T) ReportColumnNewNumericReportColumn returns a new instance of a numeric data column for a report.
type Ring
Ring represents a ring structure that can be instantiated using the NewRing function.
Example:
ring := helper.NewRing[int](2)
fmt.Println(ring.Insert(1)) // 0
fmt.Println(ring.Insert(2)) // 0
fmt.Println(ring.Insert(3)) // 1
fmt.Println(ring.Insert(4)) // 2
type Ring[T any] struct {
// contains filtered or unexported fields
}func NewRing
func NewRing[T any](size int) *Ring[T]NewRing creates a new ring instance with the given size.
func (*Ring[T]) At
func (r *Ring[T]) At(index int) TAt returns the value at the given index.
func (*Ring[T]) Get
func (r *Ring[T]) Get() (T, bool)Get retrieves the available value from the ring buffer. If empty, it returns the default value (T) and false.
func (*Ring[T]) IsEmpty
func (r *Ring[T]) IsEmpty() boolIsEmpty checks if the current ring buffer is empty.
func (*Ring[T]) IsFull
func (r *Ring[T]) IsFull() boolIsFull checks if the current ring buffer is full.
func (*Ring[T]) Put
func (r *Ring[T]) Put(t T) TPut inserts the specified value into the ring and returns the value that was previously stored at that index.
Generated by gomarkdoc