time sequence stuff

This commit is contained in:
Will Charczuk 2017-05-14 12:26:41 -07:00
parent 566d798b32
commit e39acdfb76
2 changed files with 37 additions and 0 deletions

View file

@ -137,6 +137,22 @@ func (s Seq) MinMax() (min, max float64) {
return
}
// First returns the value at index 0.
func (s Seq) First() float64 {
if s.Len() == 0 {
return 0
}
return s.GetValue(0)
}
// Last returns the value at index (len)-1.
func (s Seq) Last() float64 {
if s.Len() == 0 {
return 0
}
return s.GetValue(s.Len() - 1)
}
// Sort returns the seq sorted in ascending order.
// This fully enumerates the seq.
func (s Seq) Sort() Seq {

View file

@ -40,6 +40,27 @@ func TestTimeSeqSort(t *testing.T) {
assert.Equal(max, last)
}
func TestTimeSeqSortDescending(t *testing.T) {
assert := assert.New(t)
seq := Times(
parseTime("2016-05-14 12:00:00"),
parseTime("2017-05-14 12:00:00"),
parseTime("2015-05-14 12:00:00"),
parseTime("2017-05-13 12:00:00"),
)
sorted := seq.SortDescending()
assert.Equal(4, sorted.Len())
min, max := sorted.MinAndMax()
assert.Equal(parseTime("2015-05-14 12:00:00"), min)
assert.Equal(parseTime("2017-05-14 12:00:00"), max)
first, last := sorted.First(), sorted.Last()
assert.Equal(max, first)
assert.Equal(min, last)
}
func TestTimeSeqDays(t *testing.T) {
assert := assert.New(t)