step, times

This commit is contained in:
Simon Forman
2022-09-12 16:07:21 -07:00
parent 8778f12b68
commit 1e3b2f76bb
2 changed files with 87 additions and 78 deletions
-68
View File
@@ -137,74 +137,6 @@ def app3(S, expression, dictionary):
return stack, expression, dictionary
@inscribe
@FunctionWrapper
def step(S, expression, dictionary):
'''
Run a quoted program on each item in a sequence.
::
... [] [Q] . step
-----------------------
... .
... [a] [Q] . step
------------------------
... a . Q
... [a b c] [Q] . step
----------------------------------------
... a . Q [b c] [Q] step
The step combinator executes the quotation on each member of the list
on top of the stack.
'''
(quote, (aggregate, stack)) = S
if not aggregate:
return stack, expression, dictionary
head, tail = aggregate
stack = quote, (head, stack)
if tail:
expression = tail, (quote, (S_step, expression))
expression = S_i, expression
return stack, expression, dictionary
@inscribe
@FunctionWrapper
def times(stack, expression, dictionary):
'''
times == [-- dip] cons [swap] infra [0 >] swap while pop
::
... n [Q] . times
--------------------- w/ n <= 0
... .
... 1 [Q] . times
-----------------------
... . Q
... n [Q] . times
------------------------------------- w/ n > 1
... . Q (n - 1) [Q] times
'''
# times == [-- dip] cons [swap] infra [0 >] swap while pop
(quote, (n, stack)) = stack
if n <= 0:
return stack, expression, dictionary
n -= 1
if n:
expression = n, (quote, (S_times, expression))
expression = concat(quote, expression)
return stack, expression, dictionary
# The current definition above works like this:
# [P] [Q] while