@@ -179,8 +179,8 @@ class IMAP
179
179
# - #include_star?: Returns whether the set contains <tt>*</tt>.
180
180
#
181
181
# <i>Minimum and maximum value elements:</i>
182
- # - #min: Returns the minimum number in the set.
183
- # - #max: Returns the maximum number in the set.
182
+ # - #min: Returns one or more minimum numbers in the set.
183
+ # - #max: Returns one or more maximum numbers in the set.
184
184
# - #minmax: Returns the minimum and maximum numbers in the set.
185
185
#
186
186
# <i>Accessing value by offset in sorted set:</i>
@@ -567,24 +567,44 @@ def disjoint?(other)
567
567
empty? || input_to_tuples ( other ) . none? { intersect_tuple? _1 }
568
568
end
569
569
570
- # :call-seq: max(star: :*) => integer or star or nil
570
+ # :call-seq:
571
+ # max(star: :*) => integer or star or nil
572
+ # max(count, star: :*) => SequenceSet
571
573
#
572
574
# Returns the maximum value in +self+, +star+ when the set includes
573
575
# <tt>*</tt>, or +nil+ when the set is empty.
574
576
#
575
- # Related: #min, #minmax
576
- def max ( star : :* )
577
- ( val = @tuples . last &.last ) && val == STAR_INT ? star : val
577
+ # When +count+ is given, a new SequenceSet is returned, containing only
578
+ # the last +count+ numbers. An empty SequenceSet is returned when +self+
579
+ # is empty. (+star+ is ignored when +count+ is given.)
580
+ #
581
+ # Related: #min, #minmax, #slice
582
+ def max ( count = nil , star : :* )
583
+ if count
584
+ slice ( -[ count , size ] . min ..) || remain_frozen_empty
585
+ elsif ( val = @tuples . last &.last )
586
+ val == STAR_INT ? star : val
587
+ end
578
588
end
579
589
580
- # :call-seq: min(star: :*) => integer or star or nil
590
+ # :call-seq:
591
+ # min(star: :*) => integer or star or nil
592
+ # min(count, star: :*) => SequenceSet
581
593
#
582
594
# Returns the minimum value in +self+, +star+ when the only value in the
583
595
# set is <tt>*</tt>, or +nil+ when the set is empty.
584
- def min ( star : :* )
585
- ( val = @tuples . first &.first ) && val == STAR_INT ? star : val
586
596
#
587
- # Related: #max, #minmax
597
+ # When +count+ is given, a new SequenceSet is returned, containing only
598
+ # the first +count+ numbers. An empty SequenceSet is returned when +self+
599
+ # is empty. (+star+ is ignored when +count+ is given.)
600
+ #
601
+ # Related: #max, #minmax, #slice
602
+ def min ( count = nil , star : :* )
603
+ if count
604
+ slice ( 0 ...count ) || remain_frozen_empty
605
+ elsif ( val = @tuples . first &.first )
606
+ val != STAR_INT ? val : star
607
+ end
588
608
end
589
609
590
610
# :call-seq: minmax(star: :*) => nil or [integer, integer or star]
0 commit comments