nlist
Input Stack: |
⇨ |
Output Stack: |
Create a list from the top N items on the stack. Unlike :list which consumes the
entire stack, this operator only takes a specified number of items and leaves the rest of
the stack intact. This is useful for selective list creation without affecting other stack items.
Parameters
- ...: The stack items to potentially include in the list (at least N items must be present)
- n: Number of items to take from the top of the stack (non-negative integer)
Behavior
- Item selection: Takes the top N items from the stack (N = 0 creates an empty list)
- List creation: Creates a list preserving stack order (top item becomes last list element)
- Stack preservation: Items below the top N remain unchanged on the stack
- Boundary handling: If N exceeds available items, takes all available items
Examples
Creating an empty list (N = 0):
a,0,:nlist
Pos | Input | Output |
0 |
0 |
List() |
1 |
a |
a |
Taking 2 items from the stack:
a,b,c,2,:nlist
Pos | Input | Output |
0 |
2 |
List(b, c) |
1 |
c |
a |
2 |
b |
|
3 |
a |
|
Requesting more items than available (takes all 3 available):
a,b,c,4,:nlist
Pos | Input | Output |
0 |
4 |
List(a, b, c) |
1 |
c |
|
2 |
b |
|
3 |
a |
|
- :list - Create list from entire stack (consumes all items)
- :ndrop - Remove N items from stack (opposite operation)
- :each - Process lists created with :nlist
- :map - Transform lists created with :nlist
Since: 1.5.0