-
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.
- Loading branch information
Showing
14 changed files
with
185 additions
and
28 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
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
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
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
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
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,106 @@ | ||
package iter | ||
|
||
import "math" | ||
|
||
type Number interface { | ||
float32 | float64 | int | int32 | int64 | ||
} | ||
|
||
func Sum[T Number](i Iterator[T]) T { | ||
var sum T | ||
for { | ||
val, err := i.Next() | ||
if err != nil { | ||
break | ||
} | ||
sum += val | ||
} | ||
return sum | ||
} | ||
|
||
func Min[T Number](i Iterator[T]) T { | ||
var min T | ||
|
||
switch i.(type) { | ||
case Iterator[int]: | ||
max := math.MaxInt | ||
min = T(max) | ||
|
||
case Iterator[int32]: | ||
min = T(math.MaxInt32) | ||
|
||
case Iterator[int64]: | ||
max := math.MaxInt64 | ||
min = T(max) | ||
|
||
case Iterator[float32]: | ||
max := math.MaxFloat32 | ||
min = T(max) | ||
|
||
case Iterator[float64]: | ||
max := math.MaxFloat64 | ||
min = T(max) | ||
} | ||
|
||
for { | ||
val, err := i.Next() | ||
if err != nil { | ||
break | ||
} | ||
if min > val { | ||
min = val | ||
} | ||
} | ||
return min | ||
} | ||
|
||
func Max[T Number](i Iterator[T]) T { | ||
var max T | ||
|
||
switch i.(type) { | ||
case Iterator[int]: | ||
min := math.MinInt | ||
max = T(min) | ||
|
||
case Iterator[int32]: | ||
max = T(math.MinInt32) | ||
|
||
case Iterator[int64]: | ||
min := math.MinInt64 | ||
max = T(min) | ||
|
||
case Iterator[float32]: | ||
min := -math.MaxFloat32 | ||
max = T(min) | ||
|
||
case Iterator[float64]: | ||
min := -math.MaxFloat64 | ||
max = T(min) | ||
} | ||
|
||
for { | ||
val, err := i.Next() | ||
if err != nil { | ||
break | ||
} | ||
if max < val { | ||
max = val | ||
} | ||
|
||
} | ||
return max | ||
} | ||
|
||
func Average[T Number](i Iterator[T]) float64 { | ||
var sum T | ||
count := 0. | ||
for { | ||
val, err := i.Next() | ||
if err != nil { | ||
break | ||
} | ||
sum += val | ||
count++ | ||
} | ||
return float64(sum) / count | ||
} |
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 iter_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/EliCDavis/iter" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSum(t *testing.T) { | ||
assert.Equal(t, 10, iter.Sum(iter.Range(5))) | ||
} | ||
|
||
func TestAverage(t *testing.T) { | ||
assert.Equal(t, 2., iter.Average(iter.Range(5))) | ||
} | ||
|
||
func TestMin(t *testing.T) { | ||
assert.Equal(t, 3, iter.Min(iter.Array([]int{5, 4, 3, 4, 5}))) | ||
assert.Equal(t, -5, iter.Min(iter.Array([]int{-5, -4, -3, -4, -5}))) | ||
|
||
assert.Equal(t, int32(3), iter.Min(iter.Array([]int32{5, 4, 3, 4, 5}))) | ||
assert.Equal(t, int32(-5), iter.Min(iter.Array([]int32{-5, -4, -3, -4, -5}))) | ||
|
||
assert.Equal(t, int64(3), iter.Min(iter.Array([]int64{5, 4, 3, 4, 5}))) | ||
assert.Equal(t, int64(-5), iter.Min(iter.Array([]int64{-5, -4, -3, -4, -5}))) | ||
|
||
assert.Equal(t, float32(3), iter.Min(iter.Array([]float32{5, 4, 3, 4, 5}))) | ||
assert.Equal(t, float32(-5), iter.Min(iter.Array([]float32{-5, -4, -3, -4, -5}))) | ||
|
||
assert.Equal(t, 3., iter.Min(iter.Array([]float64{5, 4, 3, 4, 5}))) | ||
assert.Equal(t, -5., iter.Min(iter.Array([]float64{-5, -4, -3, -4, -5}))) | ||
} | ||
|
||
func TestMax(t *testing.T) { | ||
assert.Equal(t, 5, iter.Max(iter.Array([]int{3, 4, 5, 4, 3}))) | ||
assert.Equal(t, -3, iter.Max(iter.Array([]int{-3, -4, -5, -4, -3}))) | ||
|
||
assert.Equal(t, int32(5), iter.Max(iter.Array([]int32{3, 4, 5, 4, 3}))) | ||
assert.Equal(t, int32(-3), iter.Max(iter.Array([]int32{-3, -4, -5, -4, -3}))) | ||
|
||
assert.Equal(t, int64(5), iter.Max(iter.Array([]int64{3, 4, 5, 4, 3}))) | ||
assert.Equal(t, int64(-3), iter.Max(iter.Array([]int64{-3, -4, -5, -4, -3}))) | ||
|
||
assert.Equal(t, float32(5), iter.Max(iter.Array([]float32{3, 4, 5, 4, 3}))) | ||
assert.Equal(t, float32(-3), iter.Max(iter.Array([]float32{-3, -4, -5, -4, -3}))) | ||
|
||
assert.Equal(t, 5., iter.Max(iter.Array([]float64{3, 4, 5, 4, 3}))) | ||
assert.Equal(t, -3., iter.Max(iter.Array([]float64{-3, -4, -5, -4, -3}))) | ||
} |
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
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