diff --git a/seq/seq.go b/seq/seq.go index f95e6fc..606a118 100644 --- a/seq/seq.go +++ b/seq/seq.go @@ -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 { diff --git a/seq/time_seq_test.go b/seq/time_seq_test.go index 52f4057..b9cf0b9 100644 --- a/seq/time_seq_test.go +++ b/seq/time_seq_test.go @@ -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)