Coconut Programming Language
Coconut Programming Language




Coconut is a functional programming language that compiles to Python. Since all valid Python is valid Coconut, using Coconut will only extend and enhance what you're already capable of in Python.

Coconut aims to enhance the repertoire of Python programmers to include the tools of modern functional programming in as Pythonic a way as possible. Additionally, Coconut code runs the same on any Python version, meaning that even if you just use Coconut to write pure Python, you no longer have to worry about any core Python version differences: Coconut can backport almost all modern Python features as far back as Python 2.6.

Installing Coconut is as easy as


installing Python,
opening a command-line prompt,
and entering:


pip install coconut

which will give you access to all the features of Coconut, which adds to Python built-in, syntactical support for:


prettier lambdas



pipeline-style programming



partial application

range(10) |> map$(.**2) |> list


enhanced pattern-matching (on top of support for Python 3.10 pattern-matching on all Python versions)

match [head] + tail in [0, 1, 2, 3]:
    print(head, tail)


enhanced destructuring assignment

{"list": [0] + rest, **_} = {"list": [0, 1, 2, 3]}


multidimensional array literals/concatenation

import numpy as np
A = np.array([1, 2;; 3, 4])
AA = [A ; A]


operator functions



point-free programming

first_five_words = .split() ..> .$[:5] ..> " ".join


lazy evaluation

@recursive_iterator
def fib() = (1, 1) :: map((+), fib(), fib()$[1:])


easy parallelization

range(100) |> parallel_map$(.**2) |> list


tail call optimization

def factorial(n, acc=1):
    match n:
        case 0:
            return acc
        case int(_) if n > 0:
            return factorial(n-1, acc*n)


algebraic data types

data Empty()
data Leaf(n)
data Node(l, r)

def size(Empty()) = 0
addpattern def size(Leaf(n)) = 1
addpattern def size(Node(l, r)) = size(l) + size(r)



and much more!

Like what you see? Don't forget to star Coconut on GitHub!

Star

Ready to get started? Here are some links to help you out:



Coconut's tutorial will guide you through the process of starting to enhance your Python with Coconut in a straightforward, easy-to-follow way.

Coconut's documentation is an extensive catalog of information on all of Coconut's features for whenever you see something that you want more information about.

The Coconut FAQ should hopefully answer any questions you might have about who Coconut is built for and whether or not you should use it.

Creating a new issue is the best way for you to get help if you're having a problem with Coconut—just detail the problem in the issue and it will be addressed as soon as possible.

Coconut's chat room is a great place if you want to pose any general questions, concerns, or comments you have about Coconut to other Coconut developers.




