|
40 | 40 | We provide some missing features for Python from the list processing tradition, |
41 | 41 | plus a few bonus haskellisms. |
42 | 42 |
|
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). |
45 | 45 |
|
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, |
50 | 50 | functional sequence updates, pythonic lispy linked lists. |
51 | 51 |
|
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:: |
55 | 54 |
|
56 | 55 | mymap = lambda f: curry(foldr, composerc(cons, f), nil) |
57 | 56 | myadd = lambda a, b: a + b |
58 | 57 | assert curry(mymap, myadd, ll(1, 2, 3), ll(2, 4, 6)) == ll(3, 6, 9) |
59 | 58 |
|
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. |
61 | 155 | """ |
62 | 156 |
|
63 | 157 | # Set up data files for packaging. |
|
166 | 260 | # |
167 | 261 | # e.g. the keywords your project uses as topics on GitHub, minus "python" (if there) |
168 | 262 | # |
169 | | - keywords = ["functional-programming", "lisp", "scheme", "racket", "haskell"], |
| 263 | + keywords = ["functional-programming", "syntactic-macros", "macros", "lisp", "scheme", "racket", "haskell"], |
170 | 264 |
|
171 | 265 | # Declare packages so that python -m setup build will copy .py files (especially __init__.py). |
172 | 266 | # |
|
0 commit comments