Coconut

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

  1. installing Python,
  2. opening a command-line prompt,
  3. 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:

x -> x + 1
"hello, world!" |> print
range(10) |> map$(.**2) |> list
match [head] + tail in [0, 1, 2, 3]:
    print(head, tail)
{"list": [0] + rest, **_} = {"list": [0, 1, 2, 3]}
import numpy as np
A = np.array([1, 2;; 3, 4])
AA = [A ; A]
product = reduce$(*)
first_five_words = .split() ..> .$[:5] ..> " ".join
@recursive_iterator
def fib() = (1, 1) :: map((+), fib(), fib()$[1:])
range(100) |> parallel_map$(.**2) |> list
def factorial(n, acc=1):
    match n:
        case 0:
            return acc
        case int(_) if n > 0:
            return factorial(n-1, acc*n)
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: