Skip to main content

Simple, elegant, Pythonic functional programming.

Project description

Coconut is a variant of Python built for simple, elegant, Pythonic functional programming.

Coconut can be found on GitHub and PyPI, where it has been downloaded over 20,000 times.

A full introduction and tutorial of the Coconut programming language can be found in the HELP file. If you don’t know what you’re doing, you should start there.

The full Coconut documentation can be found in the DOCS file. If you’re looking for info about a specific feature, you should go there.

Why Coconut?

1. It’s just Python!

Love Python? So do I! All valid Python 3 is also valid Coconut. That means that not only does learning Coconut not require learning new libraries, it doesn’t even require learning a new core syntax! Integrating Coconut into your existing projects is as simple as replacing .py with .coc.

2. But… Coconut has powerful destructuring assignment.

Enjoy writing simple, readable code like a, b = get_two_items(), but wish you could do something like {"text": text, "tags": [first] + rest} = get_dict()? Coconut provides destructuring assignment that can accoplish all that and much, much more!

3. But… Coconut has nicer syntax.

Hate typing out lambda or return every time you want to create a one-line function? Love rhetorical questions and parallel grammatical structure? So do I! Coconut supports function definition syntax that’s as simple as (x) -> x or def f(x) = x.

4. But… Coconut has algebraic data types.

If you know Python, then you already know how useful immutable lists can be. Don’t believe me? They’re called tuples, of course! Python lets tuples hog all that immutability goodness, but wouldn’t it be nice if you could create arbitrary immutable data types? Coconut’s data statement allows you to create any sort of immutable data type that you wish!

5. But… Coconut has pattern matching.

If you’ve ever used a functional programming language before, you probably know how awesome pattern matching is. Coconut’s match statement brings all that to Python. Here’s just a taste of how powerful Coconut’s pattern-matching is:

>>> data point(x, y): pass
>>> my_point = point(3, 0)
>>> match point(a, 0) in my_point:
       print("x = " + str(a))
x = 3

6. But… Coconut supports lazy lists.

Common to functional programming, but missing from Python, are lazily-evaluated lists. Coconut’s powerful constructs for working with lazy lists allows for such cool things as:

>>> def natural_numbers(n=0) = (n,) :: natural_numbers(n+1)
>>> natural_numbers()$[0:5] |> list |> print
[0, 1, 2, 3, 4]

7. But… Coconut allows for truly Pythonic functional programming.

Not only can Coconut do all those awesome things, it also has syntactic support for partial application, function composition, infix calling, frozen set literals, unicode operators, tail call optimization, and a whole host of other constructs for you to explore.

Ready to give Coconut a try? Head over to the HELP file for a full tutorial to help (ha, get it?) you get started.

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

coconut-0.3.5.zip (34.6 kB view hashes)

Uploaded Source

Built Distribution

coconut-0.3.5-py2.py3-none-any.whl (32.1 kB view hashes)

Uploaded Python 2 Python 3

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