-
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CommonPeriod and SyncPeriod added. (#213)
# Describe Request CommonPeriod and SyncPeriod are added to help synchronize channels with varying periods. Fixed #211 # Change Type New feature.
- Loading branch information
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) 2021-2024 Onur Cinar. | ||
// The source code is provided under GNU AGPLv3 License. | ||
// https://github.com/cinar/indicator | ||
|
||
package helper | ||
|
||
import "slices" | ||
|
||
// 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 CommonPeriod(periods ...int) int { | ||
return slices.Max(periods) | ||
} | ||
|
||
// SyncPeriod adjusts the given channel to match the given common period. | ||
func SyncPeriod[T any](commonPeriod, period int, c <-chan T) <-chan T { | ||
forwardPeriod := commonPeriod - period | ||
|
||
if forwardPeriod > 0 { | ||
c = Skip(c, forwardPeriod) | ||
} | ||
|
||
return c | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright (c) 2021-2024 Onur Cinar. | ||
// The source code is provided under GNU AGPLv3 License. | ||
// https://github.com/cinar/indicator | ||
|
||
package helper_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cinar/indicator/v2/helper" | ||
) | ||
|
||
func TestSync(t *testing.T) { | ||
input1 := helper.Skip(helper.SliceToChan([]int{0, 0, 0, 0, 1, 2, 3, 4}), 4) | ||
input2 := helper.Skip(helper.SliceToChan([]int{0, 0, 1, 2, 3, 4, 5, 6}), 2) | ||
input3 := helper.Skip(helper.SliceToChan([]int{0, 0, 0, 1, 2, 3, 4, 5}), 3) | ||
|
||
commonPeriod := helper.CommonPeriod(4, 2, 3) | ||
|
||
actual1 := helper.SyncPeriod(commonPeriod, 4, input1) | ||
expected1 := helper.SliceToChan([]int{1, 2, 3, 4}) | ||
|
||
actual2 := helper.SyncPeriod(commonPeriod, 2, input2) | ||
expected2 := helper.SliceToChan([]int{3, 4, 5, 6}) | ||
|
||
actual3 := helper.SyncPeriod(commonPeriod, 3, input3) | ||
expected3 := helper.SliceToChan([]int{2, 3, 4, 5}) | ||
|
||
err := helper.CheckEquals(actual1, expected1, actual2, expected2, actual3, expected3) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} |