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 AbsWithContext[T Number](ctx context.Context, c <-chan T) <-chan T
- func Add[T Number](ac, bc <-chan T) <-chan T
- func AddWithContext[T Number](ctx context.Context, ac, bc <-chan T) <-chan T
- func AppendOrWriteToCsvFile[T any](fileName string, rows <-chan *T, options ...CsvOption[T]) error
- func AppendOrWriteToCsvFileWithContext[T any](ctx context.Context, fileName string, rows <-chan *T, options ...CsvOption[T]) error
- func Apply[T Number](c <-chan T, f func(T) T) <-chan T
- func ApplyWithContext[T Number](ctx context.Context, c <-chan T, f func(T) T) <-chan T
- func Buffered[T any](c <-chan T, size int) <-chan T
- func BufferedWithContext[T any](ctx context.Context, 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 ChangePercentWithContext[T Number](ctx context.Context, c <-chan T, before int) <-chan T
- func ChangeRatio[T Number](c <-chan T, before int) <-chan T
- func ChangeRatioWithContext[T Number](ctx context.Context, c <-chan T, before int) <-chan T
- func ChangeWithContext[T Number](ctx context.Context, 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 CountWithContext[T Number, O any](ctx context.Context, 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 DecrementByWithContext[T Number](ctx context.Context, 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 DivideByWithContext[T Number](ctx context.Context, c <-chan T, d T) <-chan T
- func DivideWithContext[T Number](ctx context.Context, ac, bc <-chan T) <-chan T
- func Drain[T any](c <-chan T)
- func DrainWithContext[T any](ctx context.Context, c <-chan T)
- func Duplicate[T any](input <-chan T, count int) []<-chan T
- func DuplicateWithContext[T any](ctx context.Context, input <-chan T, count int) []<-chan T
- func Echo[T any](input <-chan T, last, count int) <-chan T
- func EchoWithContext[T any](ctx context.Context, input <-chan T, last, count int) <-chan T
- func Field[T, S any](c <-chan *S, name string) (<-chan T, error)
- func FieldWithContext[T, S any](ctx context.Context, c <-chan *S, name string) (<-chan T, error)
- func Filter[T any](c <-chan T, p func(T) bool) <-chan T
- func FilterWithContext[T any](ctx context.Context, c <-chan T, p func(T) bool) <-chan T
- func First[T any](c <-chan T, count int) <-chan T
- func FirstWithContext[T any](ctx context.Context, c <-chan T, count int) <-chan T
- func Gcd(values ...int) int
- func Head[T Number](c <-chan T, count int) <-chan T
- func HeadWithContext[T Number](ctx context.Context, c <-chan T, count int) <-chan T
- func Highest[T Number](c <-chan T, w int) <-chan T
- func HighestWithContext[T Number](ctx context.Context, c <-chan T, w int) <-chan T
- func IncrementBy[T Number](c <-chan T, i T) <-chan T
- func IncrementByWithContext[T Number](ctx context.Context, c <-chan T, i T) <-chan T
- func JSONToChan[T any](r io.Reader) <-chan T
- func JSONToChanWithContext[T any](ctx context.Context, r io.Reader) <-chan T
- func JSONToChanWithLogger[T any](r io.Reader, logger *slog.Logger) <-chan T
- func JSONToChanWithLoggerWithContext[T any](ctx context.Context, r io.Reader, logger *slog.Logger) <-chan T
- func KeepNegatives[T Number](c <-chan T) <-chan T
- func KeepNegativesWithContext[T Number](ctx context.Context, c <-chan T) <-chan T
- func KeepPositives[T Number](c <-chan T) <-chan T
- func KeepPositivesWithContext[T Number](ctx context.Context, c <-chan T) <-chan T
- func Last[T any](c <-chan T, count int) <-chan T
- func LastWithContext[T any](ctx context.Context, c <-chan T, count int) <-chan T
- func Lcm(values ...int) int
- func Lowest[T Number](c <-chan T, w int) <-chan T
- func LowestWithContext[T Number](ctx context.Context, c <-chan T, w int) <-chan T
- func Map[F, T any](c <-chan F, f func(F) T) <-chan T
- func MapWithContext[F, T any](ctx context.Context, 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 MapWithPreviousWithContext[F, T any](ctx context.Context, 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 MultiplyByWithContext[T Number](ctx context.Context, c <-chan T, m T) <-chan T
- func MultiplyWithContext[T Number](ctx context.Context, ac, bc <-chan 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 Operate3WithContext[A any, B any, C any, R any](ctx context.Context, 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 Operate4WithContext[A any, B any, C any, D any, R any](ctx context.Context, 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 Operate5WithContext[A any, B any, C any, D any, E any, R any](ctx context.Context, 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 OperateWithContext[A any, B any, R any](ctx context.Context, ac <-chan A, bc <-chan B, o func(A, B) R) <-chan R
- func PercentRank[T Number](c <-chan T, period int) <-chan T
- func PercentRankWithContext[T Number](ctx context.Context, c <-chan T, period int) <-chan T
- func Pipe[T any](f <-chan T, t chan<- T)
- func PipeWithContext[T any](ctx context.Context, f <-chan T, t chan<- T)
- func Pow[T Number](c <-chan T, y T) <-chan T
- func PowWithContext[T Number](ctx context.Context, c <-chan T, y T) <-chan T
- func ReadFromCsvFile[T any](fileName string, options ...CsvOption[T]) (<-chan *T, error)
- func ReadFromCsvFileWithContext[T any](ctx context.Context, 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 RoundDigitsWithContext[T Number](ctx context.Context, c <-chan T, d int) <-chan T
- func Seq[T Number](from, to, increment T) <-chan T
- func SeqWithContext[T Number](ctx context.Context, from, to, increment T) <-chan T
- func Shift[T any](c <-chan T, count int, fill T) <-chan T
- func ShiftWithContext[T any](ctx context.Context, c <-chan T, count int, fill T) <-chan T
- func Sign[T Number](c <-chan T) <-chan T
- func SignWithContext[T Number](ctx context.Context, 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 SkipLastWithContext[T any](ctx context.Context, c <-chan T, count int) <-chan T
- func SkipWithContext[T any](ctx context.Context, c <-chan T, count int) <-chan T
- func SliceToChan[T any](slice []T) <-chan T
- func SliceToChanWithContext[T any](ctx context.Context, 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 SortedPercentRankWithContext[T Number](ctx context.Context, c <-chan T, period int) <-chan T
- func Sqrt[T Number](c <-chan T) <-chan T
- func SqrtWithContext[T Number](ctx context.Context, c <-chan T) <-chan T
- func Subtract[T Number](ac, bc <-chan T) <-chan T
- func SubtractWithContext[T Number](ctx context.Context, 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 WaitableWithContext[T any](ctx context.Context, wg *sync.WaitGroup, c <-chan T) <-chan T
- func Window[T any](c <-chan T, f func([]T, int) T, w int) <-chan T
- func WindowWithContext[T any](ctx context.Context, 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]) AppendToFileWithContext(ctx context.Context, fileName string, rows <-chan *T) error
- func (c *Csv[T]) ReadFromFile(fileName string) (<-chan *T, error)
- func (c *Csv[T]) ReadFromFileWithContext(ctx context.Context, fileName string) (<-chan *T, error)
- func (c *Csv[T]) ReadFromReader(reader io.Reader) <-chan *T
- func (c *Csv[T]) ReadFromReaderWithContext(ctx context.Context, reader io.Reader) <-chan *T
- func (c *Csv[T]) WriteToFile(fileName string, rows <-chan *T) error
- func (c *Csv[T]) WriteToFileWithContext(ctx context.Context, 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 wraps AbsWithContext for backwards compatibility.
Deprecated: Use AbsWithContext instead.
func AbsWithContext
func AbsWithContext[T Number](ctx context.Context, c <-chan T) <-chan TAbsWithContext 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 wraps AddWithContext for backwards compatibility.
Deprecated: Use AddWithContext instead.
func AddWithContext
func AddWithContext[T Number](ctx context.Context, ac, bc <-chan T) <-chan TAddWithContext 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 wraps AppendOrWriteToCsvFileWithContext for backwards compatibility.
Deprecated: Use AppendOrWriteToCsvFileWithContext instead.
func AppendOrWriteToCsvFileWithContext[T any](ctx context.Context, fileName string, rows <-chan *T, options ...CsvOption[T]) errorAppendOrWriteToCsvFileWithContext 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, supporting context cancellation.
func Apply
func Apply[T Number](c <-chan T, f func(T) T) <-chan TApply wraps ApplyWithContext for backwards compatibility.
Deprecated: Use ApplyWithContext instead.
func ApplyWithContext
func ApplyWithContext[T Number](ctx context.Context, c <-chan T, f func(T) T) <-chan TApplyWithContext applies the given transformation function to each element in the input channel and returns a new channel containing the transformed values with context support.
func Buffered
func Buffered[T any](c <-chan T, size int) <-chan TBuffered wraps BufferedWithContext for backwards compatibility.
Deprecated: Use BufferedWithContext instead.
func BufferedWithContext
func BufferedWithContext[T any](ctx context.Context, c <-chan T, size int) <-chan TBufferedWithContext takes a channel of any type and returns a new channel of the same type with a buffer of the specified size with context support.
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 wraps ChangeWithContext for backwards compatibility.
Deprecated: Use ChangeWithContext instead.
func ChangePercent
func ChangePercent[T Number](c <-chan T, before int) <-chan TChangePercent wraps ChangePercentWithContext for backwards compatibility.
Deprecated: Use ChangePercentWithContext instead.
func ChangePercentWithContext[T Number](ctx context.Context, c <-chan T, before int) <-chan TChangePercentWithContext 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 wraps ChangeRatioWithContext for backwards compatibility.
Deprecated: Use ChangeRatioWithContext instead.
func ChangeRatioWithContext[T Number](ctx context.Context, c <-chan T, before int) <-chan TChangeRatioWithContext 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 ChangeWithContext
func ChangeWithContext[T Number](ctx context.Context, c <-chan T, before int) <-chan TChangeWithContext 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 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 wraps CountWithContext for backwards compatibility.
Deprecated: Use CountWithContext instead.
func CountWithContext
func CountWithContext[T Number, O any](ctx context.Context, from T, other <-chan O) <-chan TCountWithContext generates a sequence of numbers starting with a specified value, from, and incrementing by one until the given other channel continues to produce values, supporting context cancellation.
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 wraps DecrementByWithContext for backwards compatibility.
Deprecated: Use DecrementByWithContext instead.
func DecrementByWithContext[T Number](ctx context.Context, c <-chan T, d T) <-chan TDecrementByWithContext 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 wraps DivideWithContext for backwards compatibility.
Deprecated: Use DivideWithContext instead.
func DivideBy
func DivideBy[T Number](c <-chan T, d T) <-chan TDivideBy wraps DivideByWithContext for backwards compatibility.
Deprecated: Use DivideByWithContext instead.
func DivideByWithContext
func DivideByWithContext[T Number](ctx context.Context, c <-chan T, d T) <-chan TDivideByWithContext 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 DivideWithContext
func DivideWithContext[T Number](ctx context.Context, ac, bc <-chan T) <-chan TDivideWithContext 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 Drain
func Drain[T any](c <-chan T)Drain wraps DrainWithContext for backwards compatibility.
Deprecated: Use DrainWithContext instead.
func DrainWithContext
func DrainWithContext[T any](ctx context.Context, c <-chan T)DrainWithContext drains the given channel with context support. It blocks the caller.
func Duplicate
func Duplicate[T any](input <-chan T, count int) []<-chan TDuplicate wraps DuplicateWithContext for backwards compatibility.
Deprecated: Use DuplicateWithContext instead.
func DuplicateWithContext
func DuplicateWithContext[T any](ctx context.Context, input <-chan T, count int) []<-chan TDuplicateWithContext 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, supporting context cancellation.
func Echo
func Echo[T any](input <-chan T, last, count int) <-chan TEcho wraps EchoWithContext for backwards compatibility.
Deprecated: Use EchoWithContext instead.
func EchoWithContext
func EchoWithContext[T any](ctx context.Context, input <-chan T, last, count int) <-chan TEchoWithContext takes a channel of numbers, repeats the specified count of numbers at the end by the specified count, supporting context cancellation.
func Field
func Field[T, S any](c <-chan *S, name string) (<-chan T, error)Field wraps FieldWithContext for backwards compatibility.
Deprecated: Use FieldWithContext instead.
func FieldWithContext
func FieldWithContext[T, S any](ctx context.Context, c <-chan *S, name string) (<-chan T, error)FieldWithContext extracts a specific field from a channel of struct pointers and delivers it through a new channel, supporting context cancellation.
func Filter
func Filter[T any](c <-chan T, p func(T) bool) <-chan TFilter wraps FilterWithContext for backwards compatibility.
Deprecated: Use FilterWithContext instead.
func FilterWithContext
func FilterWithContext[T any](ctx context.Context, c <-chan T, p func(T) bool) <-chan TFilterWithContext filters the items from the input channel based on the provided predicate function, supporting context cancellation.
func First
func First[T any](c <-chan T, count int) <-chan TFirst wraps FirstWithContext for backwards compatibility.
Deprecated: Use FirstWithContext instead.
func FirstWithContext
func FirstWithContext[T any](ctx context.Context, c <-chan T, count int) <-chan TFirstWithContext takes a channel of values and returns a new channel containing the first N values, supporting context cancellation.
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 wraps HeadWithContext for backwards compatibility.
Deprecated: Use HeadWithContext instead.
func HeadWithContext
func HeadWithContext[T Number](ctx context.Context, c <-chan T, count int) <-chan THeadWithContext retrieves the specified number of elements from the given channel of type T values and delivers them through a new channel, supporting context cancellation.
func Highest
func Highest[T Number](c <-chan T, w int) <-chan THighest wraps HighestWithContext for backwards compatibility.
Deprecated: Use HighestWithContext instead.
func HighestWithContext
func HighestWithContext[T Number](ctx context.Context, c <-chan T, w int) <-chan THighestWithContext 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 wraps IncrementByWithContext for backwards compatibility.
Deprecated: Use IncrementByWithContext instead.
func IncrementByWithContext[T Number](ctx context.Context, c <-chan T, i T) <-chan TIncrementByWithContext 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 wraps JSONToChanWithContext for backwards compatibility.
Deprecated: Use JSONToChanWithContext instead.
func JSONToChanWithContext[T any](ctx context.Context, r io.Reader) <-chan TJSONToChanWithContext reads values from the specified reader in JSON format into a channel of values, supporting context cancellation.
func JSONToChanWithLogger
func JSONToChanWithLogger[T any](r io.Reader, logger *slog.Logger) <-chan TJSONToChanWithLogger wraps JSONToChanWithLoggerWithContext for backwards compatibility.
Deprecated: Use JSONToChanWithLoggerWithContext instead.
func JSONToChanWithLoggerWithContext[T any](ctx context.Context, r io.Reader, logger *slog.Logger) <-chan TJSONToChanWithLoggerWithContext reads values from the specified reader in JSON format into a channel of values with logger and context.
func KeepNegatives
func KeepNegatives[T Number](c <-chan T) <-chan TKeepNegatives wraps KeepNegativesWithContext for backwards compatibility.
Deprecated: Use KeepNegativesWithContext instead.
func KeepNegativesWithContext[T Number](ctx context.Context, c <-chan T) <-chan TKeepNegativesWithContext 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 wraps KeepPositivesWithContext for backwards compatibility.
Deprecated: Use KeepPositivesWithContext instead.
func KeepPositivesWithContext[T Number](ctx context.Context, c <-chan T) <-chan TKeepPositivesWithContext 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 wraps LastWithContext for backwards compatibility.
Deprecated: Use LastWithContext instead.
func LastWithContext
func LastWithContext[T any](ctx context.Context, c <-chan T, count int) <-chan TLastWithContext takes a channel of values and returns a new channel containing the last N values, supporting context cancellation.
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 wraps LowestWithContext for backwards compatibility.
Deprecated: Use LowestWithContext instead.
func LowestWithContext
func LowestWithContext[T Number](ctx context.Context, c <-chan T, w int) <-chan TLowestWithContext 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 wraps MapWithContext for backwards compatibility.
Deprecated: Use MapWithContext instead.
func MapWithContext
func MapWithContext[F, T any](ctx context.Context, c <-chan F, f func(F) T) <-chan TMapWithContext applies the given transformation function to each element in the input channel and returns a new channel containing the transformed values, supporting context cancellation.
func MapWithPrevious
func MapWithPrevious[F, T any](c <-chan F, f func(T, F) T, previous T) <-chan TMapWithPrevious wraps MapWithPreviousWithContext for backwards compatibility.
Deprecated: Use MapWithPreviousWithContext instead.
func MapWithPreviousWithContext[F, T any](ctx context.Context, c <-chan F, f func(T, F) T, previous T) <-chan TMapWithPreviousWithContext applies a transformation function to each element in an input channel, creating a new channel with the transformed values, supporting context cancellation.
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 wraps MultiplyWithContext for backwards compatibility.
Deprecated: Use MultiplyWithContext instead.
func MultiplyBy
func MultiplyBy[T Number](c <-chan T, m T) <-chan TMultiplyBy wraps MultiplyByWithContext for backwards compatibility.
Deprecated: Use MultiplyByWithContext instead.
func MultiplyByWithContext[T Number](ctx context.Context, c <-chan T, m T) <-chan TMultiplyByWithContext 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 MultiplyWithContext
func MultiplyWithContext[T Number](ctx context.Context, ac, bc <-chan T) <-chan TMultiplyWithContext 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 Operate
func Operate[A any, B any, R any](ac <-chan A, bc <-chan B, o func(A, B) R) <-chan ROperate wraps OperateWithContext for backwards compatibility.
Deprecated: Use OperateWithContext instead.
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 wraps Operate3WithContext for backwards compatibility.
Deprecated: Use Operate3WithContext instead.
func Operate3WithContext
func Operate3WithContext[A any, B any, C any, R any](ctx context.Context, ac <-chan A, bc <-chan B, cc <-chan C, o func(A, B, C) R) <-chan ROperate3WithContext applies the provided operate function to corresponding values from three numeric input channels and sends the resulting values to an output channel, supporting context cancellation.
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 wraps Operate4WithContext for backwards compatibility.
Deprecated: Use Operate4WithContext instead.
func Operate4WithContext
func Operate4WithContext[A any, B any, C any, D any, R any](ctx context.Context, ac <-chan A, bc <-chan B, cc <-chan C, dc <-chan D, o func(A, B, C, D) R) <-chan ROperate4WithContext applies the provided operate function to corresponding values from four numeric input channels and sends the resulting values to an output channel, supporting context cancellation.
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 wraps Operate5WithContext for backwards compatibility.
Deprecated: Use Operate5WithContext instead.
func Operate5WithContext
func Operate5WithContext[A any, B any, C any, D any, E any, R any](ctx context.Context, ac <-chan A, bc <-chan B, cc <-chan C, dc <-chan D, ec <-chan E, o func(A, B, C, D, E) R) <-chan ROperate5WithContext applies the provided operate function to corresponding values from five numeric input channels and sends the resulting values to an output channel, supporting context cancellation.
func OperateWithContext
func OperateWithContext[A any, B any, R any](ctx context.Context, ac <-chan A, bc <-chan B, o func(A, B) R) <-chan ROperateWithContext applies the provided operate function to corresponding values from two numeric input channels and sends the resulting values to an output channel, supporting context cancellation.
func PercentRank
func PercentRank[T Number](c <-chan T, period int) <-chan TPercentRank wraps PercentRankWithContext for backwards compatibility.
Deprecated: Use PercentRankWithContext instead.
func PercentRankWithContext[T Number](ctx context.Context, c <-chan T, period int) <-chan TPercentRankWithContext returns a channel that emits the percentile rank of each value compared to the previous period-1 values, supporting context cancellation.
func Pipe
func Pipe[T any](f <-chan T, t chan<- T)Pipe wraps PipeWithContext for backwards compatibility.
Deprecated: Use PipeWithContext instead.
func PipeWithContext
func PipeWithContext[T any](ctx context.Context, f <-chan T, t chan<- T)PipeWithContext copies all elements from the input channel into the output channel with context support.
func Pow
func Pow[T Number](c <-chan T, y T) <-chan TPow wraps PowWithContext for backwards compatibility.
Deprecated: Use PowWithContext instead.
func PowWithContext
func PowWithContext[T Number](ctx context.Context, c <-chan T, y T) <-chan TPowWithContext 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 wraps ReadFromCsvFileWithContext for backwards compatibility.
Deprecated: Use ReadFromCsvFileWithContext instead.
func ReadFromCsvFileWithContext[T any](ctx context.Context, fileName string, options ...CsvOption[T]) (<-chan *T, error)ReadFromCsvFileWithContext creates a CSV instance, parses CSV data from the provided filename, maps the data to corresponding struct fields, and delivers it through the channel, supporting context cancellation.
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 wraps RoundDigitsWithContext for backwards compatibility.
Deprecated: Use RoundDigitsWithContext instead.
func RoundDigitsWithContext[T Number](ctx context.Context, c <-chan T, d int) <-chan TRoundDigitsWithContext 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 wraps SeqWithContext for backwards compatibility.
Deprecated: Use SeqWithContext instead.
func SeqWithContext
func SeqWithContext[T Number](ctx context.Context, from, to, increment T) <-chan TSeqWithContext generates a sequence of numbers, supporting context cancellation.
func Shift
func Shift[T any](c <-chan T, count int, fill T) <-chan TShift wraps ShiftWithContext for backwards compatibility.
Deprecated: Use ShiftWithContext instead.
func ShiftWithContext
func ShiftWithContext[T any](ctx context.Context, c <-chan T, count int, fill T) <-chan TShiftWithContext 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, supporting context cancellation.
func Sign
func Sign[T Number](c <-chan T) <-chan TSign wraps SignWithContext for backwards compatibility.
Deprecated: Use SignWithContext instead.
func SignWithContext
func SignWithContext[T Number](ctx context.Context, c <-chan T) <-chan TSignWithContext 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 wraps SkipWithContext for backwards compatibility.
Deprecated: Use SkipWithContext instead.
func SkipLast
func SkipLast[T any](c <-chan T, count int) <-chan TSkipLast wraps SkipLastWithContext for backwards compatibility.
Deprecated: Use SkipLastWithContext instead.
func SkipLastWithContext
func SkipLastWithContext[T any](ctx context.Context, c <-chan T, count int) <-chan TSkipLastWithContext skips the specified number of elements from the end of the given channel, supporting context cancellation.
func SkipWithContext
func SkipWithContext[T any](ctx context.Context, c <-chan T, count int) <-chan TSkipWithContext skips the specified number of elements from the given channel of type T, supporting context cancellation.
func SliceToChan
func SliceToChan[T any](slice []T) <-chan TSliceToChan wraps SliceToChanWithContext for backwards compatibility.
Deprecated: Use SliceToChanWithContext instead.
func SliceToChanWithContext[T any](ctx context.Context, slice []T) <-chan TSliceToChanWithContext converts a slice of type T to a channel of type T, supporting context cancellation.
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 wraps SortedPercentRankWithContext for backwards compatibility.
Deprecated: Use SortedPercentRankWithContext instead.
func SortedPercentRankWithContext[T Number](ctx context.Context, c <-chan T, period int) <-chan TSortedPercentRankWithContext returns a channel that emits the percentile rank by sorting the window values, supporting context cancellation.
func Sqrt
func Sqrt[T Number](c <-chan T) <-chan TSqrt wraps SqrtWithContext for backwards compatibility.
Deprecated: Use SqrtWithContext instead.
func SqrtWithContext
func SqrtWithContext[T Number](ctx context.Context, c <-chan T) <-chan TSqrtWithContext 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 wraps SubtractWithContext for backwards compatibility.
Deprecated: Use SubtractWithContext instead.
func SubtractWithContext
func SubtractWithContext[T Number](ctx context.Context, ac, bc <-chan T) <-chan TSubtractWithContext 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 wraps WaitableWithContext for backwards compatibility.
Deprecated: Use WaitableWithContext instead.
func WaitableWithContext
func WaitableWithContext[T any](ctx context.Context, wg *sync.WaitGroup, c <-chan T) <-chan TWaitableWithContext increments the wait group before reading from the channel and signals completion when the channel is closed, supporting context cancellation.
func Window
func Window[T any](c <-chan T, f func([]T, int) T, w int) <-chan TWindow wraps WindowWithContext for backwards compatibility.
Deprecated: Use WindowWithContext instead.
func WindowWithContext
func WindowWithContext[T any](ctx context.Context, c <-chan T, f func([]T, int) T, w int) <-chan TWindowWithContext returns a channel that emits the passed function result within a sliding window of size w from the input channel c, supporting context cancellation.
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.
func (*Csv[T]) AppendToFileWithContext
func (c *Csv[T]) AppendToFileWithContext(ctx context.Context, fileName string, rows <-chan *T) errorAppendToFileWithContext appends the provided rows of data to the end of the specified file, creating the file if it doesn't exist, supporting context cancellation.
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]) ReadFromFileWithContext
func (c *Csv[T]) ReadFromFileWithContext(ctx context.Context, fileName string) (<-chan *T, error)ReadFromFileWithContext parses the CSV data from the provided file name, maps the data to corresponding struct fields, and delivers the resulting rows through the channel, supporting context cancellation.
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]) ReadFromReaderWithContext
func (c *Csv[T]) ReadFromReaderWithContext(ctx context.Context, reader io.Reader) <-chan *TReadFromReaderWithContext parses the CSV data from the provided reader, maps the data to corresponding struct fields, and delivers the resulting it through the channel, supporting context cancellation.
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.
func (*Csv[T]) WriteToFileWithContext
func (c *Csv[T]) WriteToFileWithContext(ctx context.Context, fileName string, rows <-chan *T) errorWriteToFileWithContext creates a new file with the given name and writes the provided rows of data to it, overwriting any existing content, supporting context cancellation.
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