Skip to content

UpTo ​

A count specification meaning at most n, taking fewer when fewer are available.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageUpTo✓
UpTo(n)at most nn — never an error for asking for more than a collection holds. Never evaluated on its own — read by Take, Partition and Ordering.
Typed signatures
TypePackageApplies
(integer) -> unknowncombinatoricsalways

Domain: Collections

Details
  • Never reduces by itself: a count specification other heads read, not a value.
  • In Take, takes at most n elements with no error when fewer are available.
  • In Partition, allows a shorter final chunk instead of dropping the ragged remainder.
  • In Ordering, the same clamping a plain integer count already gives — at most n positions, fewer if the collection is shorter.

Examples

Basic #

Asking for up to 5 of 3 elements takes all 3, with no error

live ↗
InTake([1,2,3],UpTo(5))\mathrm{Take}(\bigl\lbrack1, 2, 3\bigr\rbrack, \mathrm{UpTo}(5))Out[1,2,3]\bigl\lbrack1, 2, 3\bigr\rbrack

When enough elements are available, exactly n is taken

live ↗
InTake([1,2,3,4,5,6],UpTo(2))\mathrm{Take}(\bigl\lbrack1, 2, 3, 4, 5, 6\bigr\rbrack, \mathrm{UpTo}(2))Out[1,2]\bigl\lbrack1, 2\bigr\rbrack

Asking for up to six from four elements returns all four, with no error

live ↗
InTake([a,b,c,d],UpTo(6))\mathrm{Take}(\bigl\lbrack a, b, c, d\bigr\rbrack, \mathrm{UpTo}(6))Out[a,b,c,d]\bigl\lbrack a, b, c, d\bigr\rbrack

When enough elements exist, it behaves like an ordinary count

live ↗
InTake([a,b,c,d],UpTo(2))\mathrm{Take}(\bigl\lbrack a, b, c, d\bigr\rbrack, \mathrm{UpTo}(2))Out[a,b]\bigl\lbrack a, b\bigr\rbrack

The final block may be short

live ↗
InPartition(1..10,UpTo(3))\mathrm{Partition}(1..10, \mathrm{UpTo}(3))Out[[1,2,3],[4,5,6],[7,8,9],[10]]\bigl\lbrack\bigl\lbrack1, 2, 3\bigr\rbrack, \bigl\lbrack4, 5, 6\bigr\rbrack, \bigl\lbrack7, 8, 9\bigr\rbrack, \bigl\lbrack10\bigr\rbrack\bigr\rbrack
Scope #

In Partition, a shorter final chunk is allowed

live ↗
InPartition([1,2,3,4,5,6],UpTo(4))\mathrm{Partition}(\bigl\lbrack1, 2, 3, 4, 5, 6\bigr\rbrack, \mathrm{UpTo}(4))Out[[1,2,3,4],[5,6]]\bigl\lbrack\bigl\lbrack1, 2, 3, 4\bigr\rbrack, \bigl\lbrack5, 6\bigr\rbrack\bigr\rbrack

In Ordering, at most 6 positions of a 5-element list

live ↗
InOrdering([2,6,1,9,2],UpTo(6))\mathrm{Ordering}(\bigl\lbrack2, 6, 1, 9, 2\bigr\rbrack, \mathrm{UpTo}(6))Out[3,1,5,2,4]\bigl\lbrack3, 1, 5, 2, 4\bigr\rbrack
live ↗
InCommonest([b,a,c,2,a,b,1,2],UpTo(6))\mathrm{Commonest}(\bigl\lbrack b, a, c, 2, a, b, 1, 2\bigr\rbrack, \mathrm{UpTo}(6))Out[b,a,c,2,1]\bigl\lbrack b, a, c, 2, 1\bigr\rbrack
live ↗
InPartition([a,b,c,d,k,f,g],UpTo(3))\mathrm{Partition}(\bigl\lbrack a, b, c, d, k, f, g\bigr\rbrack, \mathrm{UpTo}(3))Out[[a,b,c],[d,k,f],[g]]\bigl\lbrack\bigl\lbrack a, b, c\bigr\rbrack, \bigl\lbrack d, k, f\bigr\rbrack, \bigl\lbrack g\bigr\rbrack\bigr\rbrack
About
Examples9
Checked againstWolfram Language 9 agree
CrosswalkWolfram Language

See also: Partition, Ordering, Span