Skip to content

depth

Input Stack:
...
 
Output Stack:
count: Int
...

Push the current stack depth (number of items) onto the stack. This introspection operator allows you to inspect the stack state and make decisions based on how many items are present. The original stack contents remain unchanged.

Parameters

  • ...: The current stack contents (any number of items)

Behavior

  • Non-destructive: Original stack items remain in their positions
  • Count calculation: Counts all items currently on the stack
  • Stack growth: Adds one more item (the count) to the stack
  • Zero handling: Empty stack returns depth of 0

Examples

Empty stack depth:

,:depth
PosInputOutput
0 0

Single item stack depth:

a,:depth
PosInputOutput
0 a 1
1 a

Multiple items stack depth:

a,b,:depth
PosInputOutput
0 b 2
1 a b
2 a
  • :clear - Remove all items (sets depth to 0)
  • :drop - Remove single item (decreases depth by 1)
  • :ndrop - Remove N items (decreases depth by N)
  • :dup - Duplicate item (increases depth by 1)
  • :nlist - Create list from N items (requires depth >= N)

Since: 1.5.0