Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

unpythonic-0.10.4.tar.gz (133.2 kB view details)

Uploaded Source

Built Distribution

unpythonic-0.10.4-py3-none-any.whl (165.8 kB view details)

Uploaded Python 3

File details

Details for the file unpythonic-0.10.4.tar.gz.

File metadata

  • Download URL: unpythonic-0.10.4.tar.gz
  • Upload date:
  • Size: 133.2 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

Hashes for unpythonic-0.10.4.tar.gz
Algorithm Hash digest
SHA256 a4bd7fb7a885919dd6ed008aff48276cbabf4968f5e909778714afeb75280dc6
MD5 423c2ddcb77d3afda2e93d2ccdee22e4
BLAKE2b-256 7e1033096cbc84cecf3d1339e913eeef97b7387667b330153b5d670e6e7f013d

See more details on using hashes here.

File details

Details for the file unpythonic-0.10.4-py3-none-any.whl.

File metadata

  • Download URL: unpythonic-0.10.4-py3-none-any.whl
  • Upload date:
  • Size: 165.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

Hashes for unpythonic-0.10.4-py3-none-any.whl
Algorithm Hash digest
SHA256 1880e989846dac2c18a62b545d95ad1172d8f7abd679c5453c5dccda92298ea7
MD5 e3f9aab1f348c7c086e1c5566cee4e5c
BLAKE2b-256 89c3056fdccc17a7fc31d9fd89bd557f0c40b215d6af22b56d7a0eece3a8e3e1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page