-
-
Save Janiczek/032e95638351616d48bd4e7b57f80cbc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Expects `apl` on the $PATH (GNU APL) | |
# Usage: echo '{(+/W)%NmW},N1+?5 5S2' | tapl | |
import sys | |
import subprocess | |
from itertools import groupby | |
replacements = { | |
"Is": "←", # Assignment | |
# "+": "+", # Add | |
# "-": "-", # Subtract | |
"X": "×", # Multiply | |
"%": "÷", # Divide | |
# "*": "*", # Exponential | |
"Lo": "⍟", # Log | |
"Do": "⌹", # Domino | |
"Pi": "○", # Pi, Circle | |
# "!": "!", # Factorial | |
# "?": "?", # Roll | |
# "|": "|", # Magnitude | |
"Ce": "⌈", # Ceiling | |
"Fl": "⌊", # Floor | |
"B": "⊥", # Bottom | |
"T": "⊤", # Top | |
"L": "⊣", # Left | |
"R": "⊢", # Right | |
# "=": "=", # Equal | |
"Ne": "≠", # Not Equal | |
"Le": "≤", # Less or Equal | |
# "<": "<", # Less | |
# ">": ">", # Greater | |
"Ge": "≥", # Greater or Equal | |
"M": "≡", # Match | |
"Nm": "≢", # Not Match | |
"Or": "∨", # Or | |
"An": "∧", # And | |
"Na": "⍲", # Not And | |
"No": "⍱", # Not Or | |
"U": "↑", # Up | |
"D": "↓", # Down | |
"C": "⊂", # enClose | |
"P": "⊃", # Pick | |
"Pa": "⊆", # Partition | |
"Sq": "⌷", # SQuad | |
"Gu": "⍋", # Grade Up | |
"Gd": "⍒", # Grade Down | |
"I": "⍳", # Iota | |
"Wh": "⍸", # Where | |
"E": "∊", # Enlist | |
"F": "⍷", # Find | |
"Un": "∪", # Union | |
"In": "∩", # Intersection | |
# "~": "~", # Not / Without | |
# "/": "/", # Reduce | |
# "\\": "\\", # Scan | |
"Rf": "⌿", # Reduce First | |
"Sf": "⍀", # Scan First | |
# ",": "," # Ravel | |
"Ta": "⍪", # Table | |
"S": "⍴", # Shape | |
"Re": "⌽", # Reverse | |
"Ref": "⊖", # Reverse First | |
"Tr": "⍉", # Transpose | |
"Ea": "¨", # Each | |
"Co": "⍨", # Commute, Constant | |
"Pw": "⍣", # Power | |
# ".": ".", # Dot (Product) | |
"O": "∘", # Bind | |
"Rk": "⍤", # Rank | |
"Ov": "⍥", # Over | |
# "@": "@", # At | |
"Qq": "⍞", # Quote Quad | |
"Q": "⎕", # Quad | |
"Qc": "⍠", # Quad Colon | |
"K": "⌸", # Key | |
"St": "⌺", # Stencil | |
"Ib": "⌶", # I-Beam | |
"Ex": "⍎", # Execute | |
"Fo": "⍕", # Format | |
";": "⋄", # Statement Separator | |
"Nb": "⍝", # NB., Comment | |
"Go": "→", # Branch, GoTo | |
"W": "⍵", # Omega (right argument) | |
"A": "⍺", # Alpha (left argument) | |
"Rc": "∇", # ReCurse | |
# "&": "&", # Spawn | |
"N": "¯", # Negative | |
"Z": "⍬", # Zilde | |
} | |
def fixpoint(fn,val): | |
while True: | |
new_val = fn(val) | |
if val == new_val: | |
return val | |
val = new_val | |
key = lambda x: len(x[0]) | |
nkey = lambda x: -key(x) | |
by_length = [(n,dict(xs)) for n,xs in groupby(sorted(replacements.items(), key=nkey), key=key)] | |
def replace(s): | |
for n,group in by_length: | |
for term,replacement in group.items(): | |
s = s.replace(term,replacement) | |
return s | |
translate = lambda s: fixpoint(replace, s) | |
gnu_apl = subprocess.Popen(["apl", "-s", "-f", "-", "--OFF", "--noSV"], | |
stdin =subprocess.PIPE, | |
universal_newlines=True, | |
bufsize=0) | |
for line in sys.stdin: | |
translated_line = translate(line) | |
print(" " + translated_line, end="") | |
gnu_apl.stdin.write(translated_line + "\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
martinjaniczek@janiczekair ~ $ echo 'N1+?5 5S2' | ./tapl | |
¯1+?5 5⍴2 | |
0 0 0 1 1 | |
1 1 1 0 0 | |
0 1 1 1 0 | |
0 0 1 1 1 | |
0 1 1 1 1 | |
martinjaniczek@janiczekair ~ $ echo '{(+/W)%NmW},N1+?5 5S2' | ./tapl | |
{(+/⍵)÷≢⍵},¯1+?5 5⍴2 | |
0.6 | |
martinjaniczek@janiczekair ~ $ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment