Skip to content

Commit 0f0c056

Browse files
authored
Merge pull request #87 from Thunno/reduces
Add some reducing commands
2 parents 294a277 + aaaec78 commit 0f0c056

File tree

6 files changed

+121
-1
lines changed

6 files changed

+121
-1
lines changed

docs/commands.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1538,7 +1538,7 @@ Usage: `Ƙ CODE ;`. Push the first `a` positive integers where the result of `CO
15381538

15391539
# `Ʋ` (Cumulative reduce by)
15401540

1541-
Usage: `Ʋ CODE ;`. Cumulatively reduce `a` by `CODE`
1541+
Usage: `Ʋ CODE ;`. Cumulative reduce (scanl) `a` by `CODE`
15421542

15431543
***
15441544

@@ -1971,4 +1971,28 @@ Usage: `µµ CODE ;`. Start recursive list generation, executing `CODE` to get e
19711971

19721972
* `any a`, `any b`: push `b ^ a`
19731973

1974+
***
1975+
1976+
# `µƲ` (Single function reduce by)
1977+
1978+
Reduce (foldl) the elements of `a` by the next command.
1979+
1980+
***
1981+
1982+
# `µɼ` (Single function right reduce by)
1983+
1984+
Right reduce (foldr) the elements of `a` by the next command.
1985+
1986+
***
1987+
1988+
# `µƇ` (Single function right cumulative reduce by)
1989+
1990+
Right cumulative reduce (scanr) the elements of `a` by the next command.
1991+
1992+
***
1993+
1994+
# `µʋ` (Right reduce by)
1995+
1996+
Usage: `Ʋ CODE ;`. Right reduce (foldr) `a` by `CODE`
1997+
19741998
***
0 Bytes
Binary file not shown.
Binary file not shown.
545 Bytes
Binary file not shown.

src/thunno2/interpreter.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,72 @@ def run(code, *, n, iteration_index):
567567
ctx.stack.push(r)
568568
else:
569569
ctx.stack.push([])
570+
elif desc == "single function reduce by":
571+
a = next(ctx.stack.rmv(1))
572+
x = listify(a)
573+
old_stack = Stack(copy.deepcopy(list(ctx.stack).copy()))
574+
func = info
575+
if func != Void:
576+
if x:
577+
r = [x.pop(0)]
578+
for i, j in enumerate(x):
579+
ctx.stack = Stack(copy.deepcopy(old_stack))
580+
ctx.stack.push(r[-1])
581+
ctx.stack.push(j)
582+
for k in func():
583+
r.append(k)
584+
ctx.stack.push(r[-1])
585+
else:
586+
ctx.stack.push([])
587+
elif desc == "single function right reduce by":
588+
a = next(ctx.stack.rmv(1))
589+
x = listify(a)[::-1]
590+
old_stack = Stack(copy.deepcopy(list(ctx.stack).copy()))
591+
func = info
592+
if func != Void:
593+
if x:
594+
r = [x.pop(0)]
595+
for i, j in enumerate(x):
596+
ctx.stack = Stack(copy.deepcopy(old_stack))
597+
ctx.stack.push(r[-1])
598+
ctx.stack.push(j)
599+
for k in func():
600+
r.append(k)
601+
ctx.stack.push(r[-1])
602+
else:
603+
ctx.stack.push([])
604+
elif desc == "single function right cumulative reduce by":
605+
a = next(ctx.stack.rmv(1))
606+
x = listify(a)[::-1]
607+
old_stack = Stack(copy.deepcopy(list(ctx.stack).copy()))
608+
func = info
609+
if func != Void:
610+
if x:
611+
r = [x.pop(0)]
612+
for i, j in enumerate(x):
613+
ctx.stack = Stack(copy.deepcopy(old_stack))
614+
ctx.stack.push(r[-1])
615+
ctx.stack.push(j)
616+
for k in func():
617+
r.append(k)
618+
ctx.stack.push(r)
619+
else:
620+
ctx.stack.push([])
621+
elif desc == "right reduce by":
622+
a = next(ctx.stack.rmv(1))
623+
x = listify(a)[::-1]
624+
old_stack = Stack(copy.deepcopy(list(ctx.stack).copy()))
625+
if x:
626+
r = [x.pop(0)]
627+
for i, j in enumerate(x):
628+
ctx.stack = Stack(copy.deepcopy(old_stack))
629+
ctx.stack.push(r[-1])
630+
ctx.stack.push(j)
631+
run(info, n=j, iteration_index=i)
632+
r.append(next(ctx.stack.rmv(1)))
633+
ctx.stack.push(r[-1])
634+
else:
635+
ctx.stack.push([])
570636
elif desc == "for loop":
571637
a = next(ctx.stack.rmv(1))
572638
x = listify(a)

src/thunno2/lexer.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,36 @@ def tokenise(code, expected_end=""):
147147
ret.append((y, "recursive environment", r))
148148
elif y == "µ£":
149149
ret.append((y, "print each", 0))
150+
elif y == "µƲ":
151+
index += 1
152+
cmd = code[index]
153+
if cmd in DIGRAPHS:
154+
index += 1
155+
cmd += code[index]
156+
func = get_a_function(cmd)
157+
ret.append((char + cmd, "single function reduce by", func))
158+
elif y == "µɼ":
159+
index += 1
160+
cmd = code[index]
161+
if cmd in DIGRAPHS:
162+
index += 1
163+
cmd += code[index]
164+
func = get_a_function(cmd)
165+
ret.append((char + cmd, "single function right reduce by", func))
166+
elif y == "µƇ":
167+
index += 1
168+
cmd = code[index]
169+
if cmd in DIGRAPHS:
170+
index += 1
171+
cmd += code[index]
172+
func = get_a_function(cmd)
173+
ret.append(
174+
(char + cmd, "single function right cumulative reduce by", func)
175+
)
176+
elif y == "µʋ":
177+
i, r = tokenise(code[index + 1 :], expected_end=";")
178+
index += i
179+
ret.append((y, "right reduce by", r))
150180
else:
151181
ret.append((y, "digraph", get_a_function(y)))
152182
except:

0 commit comments

Comments
 (0)