Skip to content

pick

Input Stack:
n: Int
...
Output Stack:
item: Any
...

Copy an item from a specific position in the stack and place the copy on top. The original item remains in its position while a duplicate is added to the top of the stack. This allows non-destructive access to stack items at arbitrary depths.

Parameters

  • ...: The current stack contents with at least (n+1) items
  • n: Zero-based index of the item to copy (0 = top item, 1 = second item, etc.)

Behavior

  • Non-destructive copying: Original stack items remain in their positions
  • Index-based access: Uses zero-based indexing (0 = top, 1 = second from top, etc.)
  • Stack growth: Adds one more item (the copied item) to the stack
  • Bounds checking: Index must be valid for current stack depth

Examples

Picking the top item (index 0) - equivalent to :dup:

a,0,:pick
PosInputOutput
0 0 a
1 a a

Picking the top item from a two-item stack:

a,b,0,:pick
PosInputOutput
0 0 b
1 b b
2 a a

Picking the second item (index 1) - equivalent to :over:

a,b,1,:pick
PosInputOutput
0 1 a
1 b b
2 a a

Equivalences

  • 0,:pick:dup (copy top item)
  • 1,:pick:over (copy second item)
  • :dup - Copy top item (equivalent to 0,:pick)
  • :over - Copy second item (equivalent to 1,:pick)
  • :roll - Move (not copy) item to top of stack
  • :depth - Check stack size before picking
  • :swap - Exchange top two items

Since: 1.5.0