Lispy (and some haskelly) missing batteries for Python.
Project description
Python clearly wants to be an impure-FP language. A decorator with arguments is a curried closure - how much more FP can you get?
We provide some missing features for Python from the list processing tradition, plus a few bonus haskellisms.
We place a special emphasis on clear, pythonic syntax. Other design considerations are simplicity, robustness, and minimal dependencies (currently none).
Features include tail call optimization (TCO), TCO’d loops in FP style, call/ec, let & letrec, assign-once, multi-expression lambdas, def as a code block, dynamic assignment, memoize (also for generators and iterables), compose, folds and scans (left and right), unfold, lazy partial unpacking of iterables, functional sequence updates, pythonic lispy linked lists.
We provide a curry that passes extra arguments through on the right, and calls a callable return value on the remaining arguments. This is now valid Python:
mymap = lambda f: curry(foldr, composerc(cons, f), nil) myadd = lambda a, b: a + b assert curry(mymap, myadd, ll(1, 2, 3), ll(2, 4, 6)) == ll(3, 6, 9)
Finally, we provide a set of macros for those not afraid to install MacroPy and venture beyond raw Python. For example:
# let, letseq, letrec with no boilerplate a = let((x, 17), (y, 23))[ (x, y)] # cond: multi-branch "if" expression answer = lambda x: cond[x == 2, "two", x == 3, "three", "something else"] assert answer(42) == "something else" # do: imperative code in expression position y = do[localdef(x << 17), print(x), x << 23, x] assert y == 23 # autocurry like Haskell with curry: def add3(a, b, c): return a + b + c assert add3(1)(2)(3) == 6 # actually partial application so these work, too assert add3(1, 2)(3) == 6 assert add3(1)(2, 3) == 6 assert add3(1, 2, 3) == 6 mymap = lambda f: foldr(composerc(cons, f), nil) myadd = lambda a, b: a + b assert mymap(myadd, ll(1, 2, 3), ll(2, 4, 6)) == ll(3, 6, 9) with tco: assert letrec((evenp, lambda x: (x == 0) or oddp(x - 1)), (oddp, lambda x: (x != 0) and evenp(x - 1)))[ evenp(10000)] is True # lambdas with multiple expressions, local variables, and a name with multilambda, namedlambda: myadd = lambda x, y: [print("myadding", x, y), localdef(tmp << x + y), print("result is", tmp), tmp] assert myadd(2, 3) == 5 assert myadd.__name__ == "myadd (lambda)" # implicit "return" in tail position, like Lisps with autoreturn: def f(): print("hi") "I'll just return this" assert f() == "I'll just return this" def g(x): if x == 1: "one" elif x == 2: "two" else: "something else" assert g(1) == "one" assert g(2) == "two" assert g(42) == "something else" # essentially call/cc for Python with continuations: stack = [] def amb(lst, *, cc): # McCarthy's amb operator if not lst: return fail() first, *rest = lst if rest: ourcc = cc stack.append(lambda *, cc: amb(rest, cc=ourcc)) return first def fail(*, cc): if stack: f = stack.pop() return f() def pyth(*, cc): with bind[amb(tuple(range(1, 21)))] as z: with bind[amb(tuple(range(1, z+1)))] as y: with bind[amb(tuple(range(1, y+1)))] as x: # <-- the call/cc if x*x + y*y != z*z: # body is the cont return fail() return x, y, z x = pyth() while x: print(x) x = fail()
For documentation and full examples, see the project’s GitHub homepage.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file unpythonic-0.10.2.tar.gz
.
File metadata
- Download URL: unpythonic-0.10.2.tar.gz
- Upload date:
- Size: 126.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.4.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b37b561ed37cf038e78fe5579fce85f1da385dd9c0a969e1aba482ba0b6af6b |
|
MD5 | 5ab864d8bf0308feca192197c2ab01c7 |
|
BLAKE2b-256 | af88e3737e6624b5343e99875f83085836ba2eac11a5d62e8c7467b11a4b9b2d |
File details
Details for the file unpythonic-0.10.2-py3-none-any.whl
.
File metadata
- Download URL: unpythonic-0.10.2-py3-none-any.whl
- Upload date:
- Size: 158.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.4.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a2293f58e7a00d70c108f2d05fbe4f499b5868b6c023e747f6be8fc8b6056b4a |
|
MD5 | 9b2d686e2f718b3961bbcdefdc7a6228 |
|
BLAKE2b-256 | 7cfd3e6f1372481acdcc99f3bb5d9bc19eb1c28ba3525fb8aa0e509bdccc3406 |