Skip to content

Commit fe84b92

Browse files
committed
remove exception-based TCO, rename fasttco -> tco
1 parent 24c8428 commit fe84b92

7 files changed

Lines changed: 389 additions & 521 deletions

File tree

‎setup.py‎

Lines changed: 105 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,118 @@
4040
We provide some missing features for Python from the list processing tradition,
4141
plus a few bonus haskellisms.
4242
43-
We place a special emphasis on clear, pythonic syntax, as far as possible without MacroPy.
44-
Other design considerations are simplicity, robustness, and minimal dependencies (currently none).
43+
We place a special emphasis on clear, pythonic syntax. Other design considerations
44+
are simplicity, robustness, and minimal dependencies (currently none).
4545
46-
Tail call optimization (TCO), TCO'd loops in FP style, call/ec, let & letrec,
47-
assign-once, multi-expression lambdas, def as a code block, dynamic assignment,
48-
memoize (also for generators and iterables), compose, folds and scans
49-
(left and right), unfold, lazy partial unpacking of iterables,
46+
Features include tail call optimization (TCO), TCO'd loops in FP style, call/ec,
47+
let & letrec, assign-once, multi-expression lambdas, def as a code block,
48+
dynamic assignment, memoize (also for generators and iterables), compose,
49+
folds and scans (left and right), unfold, lazy partial unpacking of iterables,
5050
functional sequence updates, pythonic lispy linked lists.
5151
52-
We also provide a curry that passes extra arguments through on the right,
53-
and calls a callable return value on the remaining arguments. This is now
54-
valid Python::
52+
We provide a curry that passes extra arguments through on the right, and calls
53+
a callable return value on the remaining arguments. This is now valid Python::
5554
5655
mymap = lambda f: curry(foldr, composerc(cons, f), nil)
5756
myadd = lambda a, b: a + b
5857
assert curry(mymap, myadd, ll(1, 2, 3), ll(2, 4, 6)) == ll(3, 6, 9)
5958
60-
For documentation and examples, see the project's GitHub homepage.
59+
Finally, we provide a set of macros for those not afraid to install MacroPy
60+
and venture beyond raw Python. For example::
61+
62+
# let, letseq, letrec with no boilerplate
63+
a = let((x, 17),
64+
(y, 23))[
65+
(x, y)]
66+
67+
# cond: multi-branch "if" expression
68+
answer = lambda x: cond[x == 2, "two",
69+
x == 3, "three",
70+
"something else"]
71+
assert answer(42) == "something else"
72+
73+
# do: imperative code in expression position
74+
y = do[localdef(x << 17),
75+
print(x),
76+
x << 23,
77+
x]
78+
assert y == 23
79+
80+
# autocurry like Haskell
81+
with curry:
82+
def add3(a, b, c):
83+
return a + b + c
84+
assert add3(1)(2)(3) == 6
85+
# actually partial application so these work, too
86+
assert add3(1, 2)(3) == 6
87+
assert add3(1)(2, 3) == 6
88+
assert add3(1, 2, 3) == 6
89+
90+
mymap = lambda f: foldr(composerc(cons, f), nil)
91+
myadd = lambda a, b: a + b
92+
assert mymap(myadd, ll(1, 2, 3), ll(2, 4, 6)) == ll(3, 6, 9)
93+
94+
with tco:
95+
assert letrec((evenp, lambda x: (x == 0) or oddp(x - 1)),
96+
(oddp, lambda x: (x != 0) and evenp(x - 1)))[
97+
evenp(10000)] is True
98+
99+
# lambdas with multiple expressions, local variables, and a name
100+
with multilambda, namedlambda:
101+
myadd = lambda x, y: [print("myadding", x, y),
102+
localdef(tmp << x + y),
103+
print("result is", tmp),
104+
tmp]
105+
assert myadd(2, 3) == 5
106+
assert myadd.__name__ == "myadd (lambda)"
107+
108+
# implicit "return" in tail position, like Lisps
109+
with autoreturn:
110+
def f():
111+
print("hi")
112+
"I'll just return this"
113+
assert f() == "I'll just return this"
114+
115+
def g(x):
116+
if x == 1:
117+
"one"
118+
elif x == 2:
119+
"two"
120+
else:
121+
"something else"
122+
assert g(1) == "one"
123+
assert g(2) == "two"
124+
assert g(42) == "something else"
125+
126+
# essentially call/cc for Python
127+
with continuations:
128+
stack = []
129+
def amb(lst, *, cc): # McCarthy's amb operator
130+
if not lst:
131+
return fail()
132+
first, *rest = lst
133+
if rest:
134+
ourcc = cc
135+
stack.append(lambda *, cc: amb(rest, cc=ourcc))
136+
return first
137+
def fail(*, cc):
138+
if stack:
139+
f = stack.pop()
140+
return f()
141+
142+
def pyth(*, cc):
143+
with bind[amb(tuple(range(1, 21)))] as z:
144+
with bind[amb(tuple(range(1, z+1)))] as y:
145+
with bind[amb(tuple(range(1, y+1)))] as x: # <-- the call/cc
146+
if x*x + y*y != z*z: # body is the cont
147+
return fail()
148+
return x, y, z
149+
x = pyth()
150+
while x:
151+
print(x)
152+
x = fail()
153+
154+
For documentation and full examples, see the project's GitHub homepage.
61155
"""
62156

63157
# Set up data files for packaging.
@@ -166,7 +260,7 @@
166260
#
167261
# e.g. the keywords your project uses as topics on GitHub, minus "python" (if there)
168262
#
169-
keywords = ["functional-programming", "lisp", "scheme", "racket", "haskell"],
263+
keywords = ["functional-programming", "syntactic-macros", "macros", "lisp", "scheme", "racket", "haskell"],
170264

171265
# Declare packages so that python -m setup build will copy .py files (especially __init__.py).
172266
#

‎unpythonic/__init__.py‎

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@
55
See ``dir(unpythonic)`` and submodule docstrings for more.
66
"""
77

8-
__version__ = '0.9.3'
9-
10-
from . import rc
8+
__version__ = '0.10.0'
119

1210
from .amb import *
1311
from .arity import *
1412
from .assignonce import *
1513
from .dynscope import *
1614
from .ec import *
1715
from .fold import *
16+
from .fploop import *
1817
from .fun import *
1918
from .fup import *
2019
from .gmemo import *
@@ -30,48 +29,4 @@
3029
from .llist import *
3130
from .misc import *
3231
from .seq import *
33-
34-
# Jump through hoops to get a runtime-switchable TCO implementation.
35-
#
36-
# We manually emulate:
37-
# - making the submodule visible like __init__.py usually does
38-
# - from submod import *
39-
40-
def _starimport(module): # same effect as "from module import *"
41-
g = globals()
42-
for name in module.__all__:
43-
g[name] = getattr(module, name)
44-
45-
def _init_tco():
46-
global tco
47-
if rc._tco_impl == "exc":
48-
from . import tco
49-
elif rc._tco_impl == "fast":
50-
from . import fasttco as tco
51-
else:
52-
raise ValueError("Unknown TCO implementation '{}'".format(rc._tco_impl))
53-
_starimport(tco)
54-
55-
# Modules that require reloading because their module-level initialization
56-
# performs some from-imports from the TCO module.
57-
def _init_fploop(reload=False):
58-
global fploop
59-
from . import fploop
60-
if reload:
61-
from importlib import reload
62-
fploop = reload(fploop)
63-
_starimport(fploop)
64-
65-
_init_tco()
66-
_init_fploop()
67-
68-
def enable_fasttco(b=True):
69-
"""Switch the fast TCO implementation on/off.
70-
71-
It is 2-5x faster, but pickier about its syntax, hence not the default.
72-
See ``unpythonic.fasttco`` for details.
73-
"""
74-
rc._tco_impl = "fast" if b else "exc"
75-
76-
_init_tco()
77-
_init_fploop(reload=True)
32+
from .tco import *

0 commit comments

Comments
 (0)