-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(stream): reverse and number stream methods
- Loading branch information
1 parent
0defea7
commit 52964cf
Showing
4 changed files
with
136 additions
and
19 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,67 @@ | ||
package streams | ||
|
||
import ( | ||
"sync/atomic" | ||
|
||
"golang.org/x/exp/constraints" | ||
) | ||
|
||
type NumberStream[T constraints.Integer | constraints.Float] struct { | ||
Stream[T] | ||
} | ||
|
||
func ToNumberStream[T constraints.Integer | constraints.Float](s *Stream[T]) *NumberStream[T] { | ||
return &NumberStream[T]{ | ||
Stream: Stream[T]{ | ||
data: s.data, | ||
run: s.run, | ||
ran: atomic.Bool{}, | ||
}, | ||
} | ||
} | ||
func (s *NumberStream[T]) Sum() (result T) { | ||
s.Run() | ||
for t := range s.data { | ||
result += t | ||
} | ||
return result | ||
} | ||
func (s *NumberStream[T]) Average() (result float64) { | ||
s.Run() | ||
var count int | ||
for t := range s.data { | ||
result += float64(t) | ||
count++ | ||
} | ||
if count == 0 { | ||
return 0 | ||
} | ||
return result / float64(count) | ||
} | ||
func (s *NumberStream[T]) Max() (result *T) { | ||
s.Run() | ||
for t := range s.data { | ||
if result == nil || t > *result { | ||
result = &t | ||
} | ||
} | ||
return result | ||
} | ||
|
||
func (s *NumberStream[T]) Min() (result *T) { | ||
s.Run() | ||
for t := range s.data { | ||
if result == nil || t < *result { | ||
result = &t | ||
} | ||
} | ||
return result | ||
} | ||
|
||
func (s *NumberStream[T]) Count() (result int64) { | ||
s.Run() | ||
for range s.data { | ||
result++ | ||
} | ||
return result | ||
} |
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,50 @@ | ||
package streams | ||
|
||
import "testing" | ||
|
||
func TestNumberStream_Average(t *testing.T) { | ||
s := New(1, 2, 3, 4, 5) | ||
ns := ToNumberStream(s) | ||
if ns.Average() != 3 { | ||
t.Fail() | ||
} | ||
} | ||
func TestNumberStream_AverageEmpty(t *testing.T) { | ||
s := New([]int{}...) | ||
ns := ToNumberStream(s) | ||
if ns.Average() != 0 { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestNumberStream_Count(t *testing.T) { | ||
s := New(1, 2, 3, 4, 5) | ||
ns := ToNumberStream(s) | ||
if ns.Count() != 5 { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestNumberStream_Max(t *testing.T) { | ||
s := New(1, 2, 3, 4, 5) | ||
ns := ToNumberStream(s) | ||
if *ns.Max() != 5 { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestNumberStream_Min(t *testing.T) { | ||
s := New(1, 2, 3, 4, 5) | ||
ns := ToNumberStream(s) | ||
if *ns.Min() != 1 { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestNumberStream_Sum(t *testing.T) { | ||
s := New(1, 2, 3, 4, 5) | ||
ns := ToNumberStream(s) | ||
if ns.Sum() != 15 { | ||
t.Fail() | ||
} | ||
} |
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
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