Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Outline

helper

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.

License

Copyright (c) 2021-2026 Onur Cinar.
The source code is provided under GNU AGPLv3 License.
https://github.com/cinar/indicator

Disclaimer

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.

Index

Constants

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 T

Abs wraps AbsWithContext for backwards compatibility.

Deprecated: Use AbsWithContext instead.

func AbsWithContext[T Number](ctx context.Context, c <-chan T) <-chan T

AbsWithContext 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 T

Add wraps AddWithContext for backwards compatibility.

Deprecated: Use AddWithContext instead.

func AddWithContext[T Number](ctx context.Context, ac, bc <-chan T) <-chan T

AddWithContext 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]) error

AppendOrWriteToCsvFile wraps AppendOrWriteToCsvFileWithContext for backwards compatibility.

Deprecated: Use AppendOrWriteToCsvFileWithContext instead.

func AppendOrWriteToCsvFileWithContext[T any](ctx context.Context, fileName string, rows <-chan *T, options ...CsvOption[T]) error

AppendOrWriteToCsvFileWithContext 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 T

Apply wraps ApplyWithContext for backwards compatibility.

Deprecated: Use ApplyWithContext instead.

func ApplyWithContext[T Number](ctx context.Context, c <-chan T, f func(T) T) <-chan T

ApplyWithContext 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[T any](c <-chan T, size int) <-chan T

Buffered wraps BufferedWithContext for backwards compatibility.

Deprecated: Use BufferedWithContext instead.

func BufferedWithContext[T any](ctx context.Context, c <-chan T, size int) <-chan T

BufferedWithContext 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[T any](c <-chan T, w io.Writer) error

ChanToJSON 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[T any](c <-chan T) []T

ChanToSlice 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 T

Change wraps ChangeWithContext for backwards compatibility.

Deprecated: Use ChangeWithContext instead.

func ChangePercent[T Number](c <-chan T, before int) <-chan T

ChangePercent wraps ChangePercentWithContext for backwards compatibility.

Deprecated: Use ChangePercentWithContext instead.

func ChangePercentWithContext[T Number](ctx context.Context, c <-chan T, before int) <-chan T

ChangePercentWithContext 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[T Number](c <-chan T, before int) <-chan T

ChangeRatio wraps ChangeRatioWithContext for backwards compatibility.

Deprecated: Use ChangeRatioWithContext instead.

func ChangeRatioWithContext[T Number](ctx context.Context, c <-chan T, before int) <-chan T

ChangeRatioWithContext 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[T Number](ctx context.Context, c <-chan T, before int) <-chan T

ChangeWithContext 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[T comparable](inputs ...<-chan T) error

CheckEquals determines whether the two channels are equal.

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(rows *sql.Rows)

CloseDatabaseRows closes the database rows.

func CloseDatabaseWithError(db *sql.DB, err error) error

CloseDatabaseWithError closes the database after an error.

func CommonPeriod(periods ...int) int

CommonPeriod 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 T

Count wraps CountWithContext for backwards compatibility.

Deprecated: Use CountWithContext instead.

func CountWithContext[T Number, O any](ctx context.Context, from T, other <-chan O) <-chan T

CountWithContext 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(from, to time.Time) int

DaysBetween calculates the days between the given two times.

func DecrementBy[T Number](c <-chan T, d T) <-chan T

DecrementBy wraps DecrementByWithContext for backwards compatibility.

Deprecated: Use DecrementByWithContext instead.

func DecrementByWithContext[T Number](ctx context.Context, c <-chan T, d T) <-chan T

DecrementByWithContext 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 T

Divide wraps DivideWithContext for backwards compatibility.

Deprecated: Use DivideWithContext instead.

func DivideBy[T Number](c <-chan T, d T) <-chan T

DivideBy wraps DivideByWithContext for backwards compatibility.

Deprecated: Use DivideByWithContext instead.

func DivideByWithContext[T Number](ctx context.Context, c <-chan T, d T) <-chan T

DivideByWithContext 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[T Number](ctx context.Context, ac, bc <-chan T) <-chan T

DivideWithContext 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[T any](ctx context.Context, c <-chan T)

DrainWithContext drains the given channel with context support. It blocks the caller.

func Duplicate[T any](input <-chan T, count int) []<-chan T

Duplicate wraps DuplicateWithContext for backwards compatibility.

Deprecated: Use DuplicateWithContext instead.

func DuplicateWithContext[T any](ctx context.Context, input <-chan T, count int) []<-chan T

DuplicateWithContext 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 T

Echo wraps EchoWithContext for backwards compatibility.

Deprecated: Use EchoWithContext instead.

func EchoWithContext[T any](ctx context.Context, input <-chan T, last, count int) <-chan T

EchoWithContext 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[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 T

Filter wraps FilterWithContext for backwards compatibility.

Deprecated: Use FilterWithContext instead.

func FilterWithContext[T any](ctx context.Context, c <-chan T, p func(T) bool) <-chan T

FilterWithContext 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 T

First wraps FirstWithContext for backwards compatibility.

Deprecated: Use FirstWithContext instead.

func FirstWithContext[T any](ctx context.Context, c <-chan T, count int) <-chan T

FirstWithContext takes a channel of values and returns a new channel containing the first N values, supporting context cancellation.

func Gcd

func Gcd(values ...int) int

Gcd calculates the Greatest Common Divisor of the given numbers.

func Head

func Head[T Number](c <-chan T, count int) <-chan T

Head wraps HeadWithContext for backwards compatibility.

Deprecated: Use HeadWithContext instead.

func HeadWithContext[T Number](ctx context.Context, c <-chan T, count int) <-chan T

HeadWithContext 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 T

Highest wraps HighestWithContext for backwards compatibility.

Deprecated: Use HighestWithContext instead.

func HighestWithContext[T Number](ctx context.Context, c <-chan T, w int) <-chan T

HighestWithContext returns a channel that emits the highest value within a sliding window of size w from the input channel c.

func IncrementBy[T Number](c <-chan T, i T) <-chan T

IncrementBy wraps IncrementByWithContext for backwards compatibility.

Deprecated: Use IncrementByWithContext instead.

func IncrementByWithContext[T Number](ctx context.Context, c <-chan T, i T) <-chan T

IncrementByWithContext 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[T any](r io.Reader) <-chan T

JSONToChan wraps JSONToChanWithContext for backwards compatibility.

Deprecated: Use JSONToChanWithContext instead.

func JSONToChanWithContext[T any](ctx context.Context, r io.Reader) <-chan T

JSONToChanWithContext reads values from the specified reader in JSON format into a channel of values, supporting context cancellation.

func JSONToChanWithLogger[T any](r io.Reader, logger *slog.Logger) <-chan T

JSONToChanWithLogger wraps JSONToChanWithLoggerWithContext for backwards compatibility.

Deprecated: Use JSONToChanWithLoggerWithContext instead.

func JSONToChanWithLoggerWithContext[T any](ctx context.Context, r io.Reader, logger *slog.Logger) <-chan T

JSONToChanWithLoggerWithContext reads values from the specified reader in JSON format into a channel of values with logger and context.

func KeepNegatives[T Number](c <-chan T) <-chan T

KeepNegatives wraps KeepNegativesWithContext for backwards compatibility.

Deprecated: Use KeepNegativesWithContext instead.

func KeepNegativesWithContext[T Number](ctx context.Context, c <-chan T) <-chan T

KeepNegativesWithContext 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[T Number](c <-chan T) <-chan T

KeepPositives wraps KeepPositivesWithContext for backwards compatibility.

Deprecated: Use KeepPositivesWithContext instead.

func KeepPositivesWithContext[T Number](ctx context.Context, c <-chan T) <-chan T

KeepPositivesWithContext 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 T

Last wraps LastWithContext for backwards compatibility.

Deprecated: Use LastWithContext instead.

func LastWithContext[T any](ctx context.Context, c <-chan T, count int) <-chan T

LastWithContext takes a channel of values and returns a new channel containing the last N values, supporting context cancellation.

func Lcm

func Lcm(values ...int) int

Lcm calculates the Least Common Multiple of the given numbers.

func Lowest

func Lowest[T Number](c <-chan T, w int) <-chan T

Lowest wraps LowestWithContext for backwards compatibility.

Deprecated: Use LowestWithContext instead.

func LowestWithContext[T Number](ctx context.Context, c <-chan T, w int) <-chan T

LowestWithContext 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 T

Map wraps MapWithContext for backwards compatibility.

Deprecated: Use MapWithContext instead.

func MapWithContext[F, T any](ctx context.Context, c <-chan F, f func(F) T) <-chan T

MapWithContext 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[F, T any](c <-chan F, f func(T, F) T, previous T) <-chan T

MapWithPrevious 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 T

MapWithPreviousWithContext applies a transformation function to each element in an input channel, creating a new channel with the transformed values, supporting context cancellation.

func MaxSince[T Number](c <-chan T, w int) <-chan T

MaxSince 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[T Number](c <-chan T, w int) <-chan T

MinSince returns a channel of T indicating since when (number of previous values) the respective value was the minimum.

func Multiply[T Number](ac, bc <-chan T) <-chan T

Multiply wraps MultiplyWithContext for backwards compatibility.

Deprecated: Use MultiplyWithContext instead.

func MultiplyBy[T Number](c <-chan T, m T) <-chan T

MultiplyBy wraps MultiplyByWithContext for backwards compatibility.

Deprecated: Use MultiplyByWithContext instead.

func MultiplyByWithContext[T Number](ctx context.Context, c <-chan T, m T) <-chan T

MultiplyByWithContext 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[T Number](ctx context.Context, ac, bc <-chan T) <-chan T

MultiplyWithContext 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 R

Operate wraps OperateWithContext for backwards compatibility.

Deprecated: Use OperateWithContext instead.

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

Operate3 wraps Operate3WithContext for backwards compatibility.

Deprecated: Use Operate3WithContext instead.

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

Operate3WithContext 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[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

Operate4 wraps Operate4WithContext for backwards compatibility.

Deprecated: Use Operate4WithContext instead.

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

Operate4WithContext 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[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

Operate5 wraps Operate5WithContext for backwards compatibility.

Deprecated: Use Operate5WithContext instead.

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

Operate5WithContext 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[A any, B any, R any](ctx context.Context, ac <-chan A, bc <-chan B, o func(A, B) R) <-chan R

OperateWithContext 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[T Number](c <-chan T, period int) <-chan T

PercentRank wraps PercentRankWithContext for backwards compatibility.

Deprecated: Use PercentRankWithContext instead.

func PercentRankWithContext[T Number](ctx context.Context, c <-chan T, period int) <-chan T

PercentRankWithContext 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[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 T

Pow wraps PowWithContext for backwards compatibility.

Deprecated: Use PowWithContext instead.

func PowWithContext[T Number](ctx context.Context, c <-chan T, y T) <-chan T

PowWithContext 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[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(t *testing.T, path string)

RemoveAll removes the files with the given path.

func RoundDigit[T Number](n T, d int) T

RoundDigit rounds the given float64 number to d decimal places.

Example:

n := helper.RoundDigit(10.1234, 2)
fmt.Println(n) // 10.12

func RoundDigits[T Number](c <-chan T, d int) <-chan T

RoundDigits wraps RoundDigitsWithContext for backwards compatibility.

Deprecated: Use RoundDigitsWithContext instead.

func RoundDigitsWithContext[T Number](ctx context.Context, c <-chan T, d int) <-chan T

RoundDigitsWithContext 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 T

Seq wraps SeqWithContext for backwards compatibility.

Deprecated: Use SeqWithContext instead.

func SeqWithContext[T Number](ctx context.Context, from, to, increment T) <-chan T

SeqWithContext generates a sequence of numbers, supporting context cancellation.

func Shift

func Shift[T any](c <-chan T, count int, fill T) <-chan T

Shift wraps ShiftWithContext for backwards compatibility.

Deprecated: Use ShiftWithContext instead.

func ShiftWithContext[T any](ctx context.Context, c <-chan T, count int, fill T) <-chan T

ShiftWithContext 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 T

Sign wraps SignWithContext for backwards compatibility.

Deprecated: Use SignWithContext instead.

func SignWithContext[T Number](ctx context.Context, c <-chan T) <-chan T

SignWithContext 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 R

Since 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 T

Skip wraps SkipWithContext for backwards compatibility.

Deprecated: Use SkipWithContext instead.

func SkipLast[T any](c <-chan T, count int) <-chan T

SkipLast wraps SkipLastWithContext for backwards compatibility.

Deprecated: Use SkipLastWithContext instead.

func SkipLastWithContext[T any](ctx context.Context, c <-chan T, count int) <-chan T

SkipLastWithContext skips the specified number of elements from the end of the given channel, supporting context cancellation.

func SkipWithContext[T any](ctx context.Context, c <-chan T, count int) <-chan T

SkipWithContext skips the specified number of elements from the given channel of type T, supporting context cancellation.

func SliceToChan[T any](slice []T) <-chan T

SliceToChan wraps SliceToChanWithContext for backwards compatibility.

Deprecated: Use SliceToChanWithContext instead.

func SliceToChanWithContext[T any](ctx context.Context, slice []T) <-chan T

SliceToChanWithContext converts a slice of type T to a channel of type T, supporting context cancellation.

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[T Number](c <-chan T, period int) <-chan T

SortedPercentRank wraps SortedPercentRankWithContext for backwards compatibility.

Deprecated: Use SortedPercentRankWithContext instead.

func SortedPercentRankWithContext[T Number](ctx context.Context, c <-chan T, period int) <-chan T

SortedPercentRankWithContext 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 T

Sqrt wraps SqrtWithContext for backwards compatibility.

Deprecated: Use SqrtWithContext instead.

func SqrtWithContext[T Number](ctx context.Context, c <-chan T) <-chan T

SqrtWithContext 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[T Number](ac, bc <-chan T) <-chan T

Subtract wraps SubtractWithContext for backwards compatibility.

Deprecated: Use SubtractWithContext instead.

func SubtractWithContext[T Number](ctx context.Context, ac, bc <-chan T) <-chan T

SubtractWithContext 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[T any](commonPeriod, period int, c <-chan T) <-chan T

SyncPeriod adjusts the given channel to match the given common period.

func Waitable[T any](wg *sync.WaitGroup, c <-chan T) <-chan T

Waitable wraps WaitableWithContext for backwards compatibility.

Deprecated: Use WaitableWithContext instead.

func WaitableWithContext[T any](ctx context.Context, wg *sync.WaitGroup, c <-chan T) <-chan T

WaitableWithContext 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 T

Window wraps WindowWithContext for backwards compatibility.

Deprecated: Use WindowWithContext instead.

func WindowWithContext[T any](ctx context.Context, c <-chan T, f func([]T, int) T, w int) <-chan T

WindowWithContext 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) bool

Contains 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() T

Max function returns the maximum value in the binary search tree.

func (*Bst[T]) Min

func (b *Bst[T]) Min() T

Min function returns the minimum value in the binary search tree.

func (*Bst[T]) Remove

func (b *Bst[T]) Remove(value T) bool

Remove 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) error

AppendToFile appends the provided rows of data to the end of the specified file, creating the file if it doesn't exist.

func (c *Csv[T]) AppendToFileWithContext(ctx context.Context, fileName string, rows <-chan *T) error

AppendToFileWithContext 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 (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 *T

ReadFromReader parses the CSV data from the provided reader, maps the data to corresponding struct fields, and delivers the resulting it through the channel.

func (c *Csv[T]) ReadFromReaderWithContext(ctx context.Context, reader io.Reader) <-chan *T

ReadFromReaderWithContext 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) error

WriteToFile 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) error

WriteToFileWithContext creates a new file with the given name and writes the provided rows of data to it, overwriting any existing content, supporting context cancellation.

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[T any](logger *slog.Logger) CsvOption[T]

WithCsvLogger sets the logger for the CSV instance.

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(title string, date <-chan time.Time) *Report

NewReport 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() int

AddChart 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) error

WriteToFile 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) error

WriteToWriter 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.

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) ReportColumn

NewAnnotationReportColumn returns a new instance of an annotation column for a report.

func NewNumericReportColumn[T Number](name string, values <-chan T) ReportColumn

NewNumericReportColumn 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) T

At 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() bool

IsEmpty checks if the current ring buffer is empty.

func (*Ring[T]) IsFull

func (r *Ring[T]) IsFull() bool

IsFull checks if the current ring buffer is full.

func (*Ring[T]) Put

func (r *Ring[T]) Put(t T) T

Put inserts the specified value into the ring and returns the value that was previously stored at that index.

Generated by gomarkdoc

Morty Proxy This is a proxified and sanitized view of the page, visit original site.